这个作业是用Matlab完成一系列数学问题

MCD4140 Practical Session 6

Basic Level Tasks: To be completed as a homework before the practical session.

Write Matlab programs to answer the following questions:

Task 1

Consider the matrix

3 5 6 9 8 12 17

6 7 11 19 21 6 13

7 8 9 6 12 15 3

8 5 12 13 25 9 12

A

a) Copy positive numbers into B . The new matrix should be the same size as A. Store 0

where the negative numbers are.

b) Copy negative numbers into C . The new matrix should be the same size as A. Store 0

where the positive numbers are.

[Hint: Use nested for loops]

Task 2

Using ‘if’, ‘elseif’ and ‘else’ statements write a MATLAB script that takes x and y coordinates as inputs

from the user and computes the angle with respect to the positive x‐axis (counter‐clockwise) as well

as the corresponding quadrant.

1. Angle in the first quadrant when both x and y are positive. 𝜃 ൌ tanିଵ ቀ

௬

௫

ቁ

2. Angle in second quadrant = 180o

‐ 𝜃 – when x is negative and y is positive.

3. Angle in third quadrant = 180o

+ 𝜃 – when x is negative and y is negative.

4. Angle in fourth quadrant = 360o

‐ 𝜃 – when x is positive and y is negative.

The command window should appear like the following:

E.g.

Enter x coordinate: ‐5

Enter y coordinate: ‐12

Required angle is 247.38 and is in the 3rd quadrant

E.g.

Enter x coordinate: 0

Enter y coordinate: 12

Required angle is 90 degrees and falls on the positive‐y axis

Test your code for following coordinates:

A. (x,y) = (5, 12)

B. (x,y) = (0, 12)

C. (x,y) = (‐5, ‐12)

D. (x,y) = (0, ‐12)

Caution: Inputs of x=0 and y=0 should return an `undefined’ error.

Average Level Tasks: To be completed during the practical session, but it is a good

idea to read and understand the questions before coming to the practical session.

Task 1

The circumference of a circle is given by 𝐶 ൌ 2𝜋𝑟 where r is the radius. The circumference of an ellipse

can be approximated by 𝐸ൌ𝜋 ቂ3ሺ𝑎𝑏ሻ െ ඥሺ3𝑎 𝑏ሻሺ𝑎 3𝑏ሻቃ where a and b are the minor and

major axis lengths.

A. Write an m‐file which prompts the user to specify whether they would like to calculate the

circumference of a circle or an ellipse. If the circle option is chosen, ask the user for a radius.

If the ellipse option is chosen, ask the user for the minor and major axis lengths.

B. Calculate the circumference of the chosen shape. Use fprintf to write a statement containing

the shape chosen, the relevant length(s) and the circumference.

C. Ask the user if they would like to repeat the calculation. If yes is chosen, go through steps A

and B again. If no is chosen, end the code.

Hint: Use a while loop to achieve part C.

Your code should look similar to the following:

Enter either circle (1) or ellipse (2): 1

Enter the radius length: 1

Circle was chosen with a radius of 1. Circumference = 6.283185e+00

Would you like to repeat the calculation? 1=yes, 0=no: 1

Task 2

The roots of the quadratic equation 𝑎𝑥ଶ 𝑏𝑥 𝑐 ൌ 0 are given by

𝑥 ൌ

െ𝑏 േ √𝑏ଶ െ 4𝑎𝑐

2𝑎

where the discriminant of the equation is defined to be ∆ൌ 𝑏ଶ െ 4𝑎𝑐.

Based on the sign of the discriminant, it is possible to characterise the nature of the roots according

to the following relationships:

1. ∆ > 0 : A pair of non‐equal real valued roots

2. ∆ = 0 : A pair of equal real valued roots

3. ∆ < 0 : A pair of non‐equal complex valued roots

a) Write a function that uses a, b and c as inputs to compute the discriminant and the roots of

the quadratic equation.

function [x1, x2, discriminant] = my_quadratic(a,b,c)

b) Write an m‐file that prompts the user to input values of a, b and c and uses the function in

written in part a) to output the roots and its characteristics. Print the roots to 3 decimal places.

Ensure that your program checks that the user enters a non‐zero value for the `a’ coefficient.

Note: You may need to separate the complex variable into real and imaginary terms to display them

as x = g + hi. Look up the real() and imag() functions.

Your output should look similar to the following:

Quadratic Program ‐ What are the quadratic coefficients?

a = 2

b = ‐1

c = 1

For quadratic coefficients a=2.00, b=‐1.00 and c=1.00

A pair of non‐equal complex valued roots exists and are x1=0.250 + 0.661i and x2=0.250 ‐ 0.661i

Higher Level Tasks: To be completed during the practical session, but it is a good idea

to read and understand the questions before coming to the practical session.

Task 1

Many real world objects travel in a jagged manner that is best represented by piecewise functions.

For example, the velocity of a rocket may change sharply when thrusters are enabled or disabled and

due to various external factors. The velocity of a rocket can be modelled by v(t) where t represents

the time.

Write a function that takes a start and end time as inputs and returns the vectors v and t. The vector

t should contain values between start_t and end_t at 0.01 increments. The vector v should contain

v(t) for each value in t.

function [t, v] = VPiecewise(start_t, end_t)

Use the function in an m‐file to plot the velocities for t=‐5 to 80.

Hint: Use if statements to break up the different profile conditions.

Task 2

Recall that the following Taylor series is used to approximate Cosine:

cosሺ𝑥ሻ ൌ ሺെ1ሻ𝑥ଶ

ሺ2𝑛ሻ!

ஶ

ୀ

You have been tasked with developing an M‐file that allows other engineers to quickly determine the

minimum n needed to reduce the truncation error below an error threshold. The truncation error is

the absolute error between the approximation and MATLAB’s cos() function.

Note: You may want to Google the Taylor series to gain a better understanding of it.

Your code should do the following:

1. Ask the user to enter an error threshold value (it should work with vectors)

2. Find the number of terms (minimum n) needed so that the truncation error is below the user

error threshold for x = ‐2π to 2π.

3. Print the minimum n value, the mean truncation error and the maximum truncation error

using fprintf.

4. Plot the truncation error against x.

Test your code with the following error thresholds: 0.1, 0.01, 1e‐10.

The answers for the thresholds above should be n=8, 9, 16, respectively. This is a reference to see

whether your code is working correctly.

Hint: Your code may include both while and for loops and/or a created function to calculate the

approximation of Cosine.

5 marks deducted for poor programming practices (missing comments,

unnecessary outputs, no axis labels, inefficient coding, etc.)

END OF ASSESSED QUESTIONS

The remainder of this document contains supplementary and exam‐type questions for extended

learning. Use your allocated lab time wisely!

## 发表评论