Вы находитесь на странице: 1из 4

Homework 4 (v 1.0.

1)
ENSC 3213: Computer Based Systems
Oklahoma State University
Spring 2016 Semester
Due: March 4, 2016

More Complicated Programs for Computation


Note: for all MATLAB programs develop the M-file as a function.
1. The average daily temperatue for an area can be approximated by the following function:

= Tmean + (Tpeak Tmean ) cos( (t tpeak ))

where Tmean = the average annual temperature, Tpeak = the peak temperature, = the frequency of
the annual variation (2 /365)) and tpeak = day of the peak temperature ( 205 days). Parameters
from some U.S. towns are listed below in Table 1:
City
Miami, FL
Yuma, AZ
Bismark, ND
Seatle, WA
Boston, MA

Tmean [ C]
22.1
23.1
5.2
10.6
10.7

Tpeak [ C]
28.3
33.6
22.1
17.6
22.9

Table 1: Problem 1
Develop a M-file that computes the average temperature between two days of the year for a particular
city. Test it for (a) January-February in Yuma, AZ (t = 0 to 59) (b) July-August temperature in
Seattle, WA (t = 180 to 242).
2. The sine function can be evaluated by the following infinite series:

sin x = x

x3
x5
+
...
3!
5!

Create a M-file to implement this formula so that it computes and displays the values of sin x as each
eterm in the series is added. In other words, compute and display in sequence the values for:
sin x = x
sin x = x

x3
3!

sin x = x

x3
x5
+
3!
5!

..
.
up to the order term of your choosing. For each of the preceding, compute and display the percent
relative error as:

%Error =

true series approximation


100%
true

As a test case, employ the program to compute sin(0.9) for up to and including eight terms that is,
up to the term x15 /15!.
3. Two distances are required to specify the location of a point relative to the origin in two-dimensional
space. That is,
The horizontal and vertical distances (x, y) in Cartesian coordinates.
The radius and angle (r, ) in polar coordinates
It is a relatively straightforward to compute Cartesian coordinates (x, y) on the basis of polar coordinates (r, ). The reverse process is not so simple. The radius can be computed by the following
formula:
p
r = x2 + y 2
If the coordinates lie within the first and fourth coordinates (i.e., x > 0), then a simple formula can be
used to compute :
y
= tan1
x
The difficulty arise for the other cases. Table 2 summaries the possibilities:
x
<0
<0
<0
=0
=0
=0

y
>0
<0
=0
>0
<0
=0

tan (y/x) +
tan1 (y/x)

/2
/2
0
1

Table 2: Problem 3 : Ranges


Write a well-structured M-file using if . . . elseif structure to calculate r and as a function of x and
y. Express the final results for in degrees. Test your program by evaluating the following cases in
Table 3:
4. The divide and average method, an old-time method for approximating the square-root of any
positive number a, can be formulated as:
x =

x + a/x
2

x
2
2
0
-3
-2
-1
0
0
2

y
0
1
3
1
0
-2
0
-2
2

Table 3: Problem 3 : Results


Write a well-structured M-file function based on the while . . . break to implement this algorithm. Use
proper indentation, so that the structure is clear. At each step, estimate the error in your approximation
as:


xnew xold


 =

xnew
Repeat the loop until  is less than equal to a specified value. Design your program so that it returns
both the result and the error. Make sure that it can evaluate the square root of numbers that are
equal to and less than zero. For example, the square root of 4 would return 2i. Test your program
by evaluating a = 0, 2, 10 and 4 for  = 1 104 .
5. Develop a function function M-file that returns the difference between the passed functions maximum
and minimum value given a range of the independent variable. In addition, have the function generate
a plot of the function for the range. Test it for the following cases:
f (t) = 8 e0.25t sin(t 2) from t = 0 to 6 pi
f (x) = e4x sin(1/x) from x = 0.01 to 0.20
The built-in humps function for x = 0 to 2 Hint: you can call the humps function by passing
it with the @ symbol. For example, if you called the function for this problem as the following
funcrange(f,a,b,n) where f = the function to evaluated, a = the lower bound of the range, b =
the upper bound of the range, and n = the number of intervals, you might call it, as follows:
funcrange(@humps,0,2,1000)
You can also do this with defined functions as described in class. For example, if you had a
function (f = e4x ), you could define it and call it, as follows:
f @(x) = exp(4*x);
funcrange(f, 0.01, 0.2, 1000)
6. Write a function in C that computes the value of the following polynomial:

3 x5 + 2 x4 5 x3 x2 + 7 x 6
Write a program that asks the user to enter a value for x, calls the function to compute the value of
the polynomial, and then displays the value returned by the function.

7. The value of the mathematical constant e can be expressed as an infinite series:


e = 1 + 1/1! + 1/2! + 1/3! + . . .
Write a program in C that approximates e by computing the value of:
e = 1 + 1/1! + 1/2! + 1/3! + . . . + 1/n!
where n is an integer entered by the user.
8. The trajectory of an object can be modeled as:
y = (tan 0 ) x

2 v0

g
x2 + y0
cos2 0

where y = height [m], 0 = initial angle [radians], x = horizontal distance [m], g = gravitational
acceleration (= 9.81 m/s2 ), v0 = initial velocity [m/s], and y0 = initial height [m]. Use MATLAB to
find the trajectories for y0 = 0 and v0 = 28 m/s for initial angles ranging from 15 to 75 in increments
of 15 . Employ a range of horizontal distances from x = 0 to 80 m in increments of 5 m. The results
should be assembled in an array where the first dimension (rows) corresponds to the distances, and the
second dimension (columns) corresponds to the different initial angles. Use this matrix to generate a
single plot of the heights versus horizontal distances for each of the initial angles. Employ a legend to
distinguish among the different cases, and scale the plot so that the minimum height is zero using the
axis command.
Extra Credit: Develop a function to produce an animation of a particle moving in a circle in Cartesian coordinates
based on radial coordinates. Assume a constant radius, r, and allow the angle, , to increase from 0
to 2 in equal increments. The functions first lines should be:
function phasor(r, nt, nm)
% function to show the orbit of a phasor
% r = radius
% nt = number of increments for theta
% nm = number of movies

Test out your function with phasor(1, 256, 10)