Description
Introduction to Programming with MATLAB
Lesson 5
• Unless otherwise indicated, your function should not print anything to the Command Window, but your function will not be counted incorrect if it does.
• Note that you are not required to use the suggested names of input variables and output variables, but you must use the specified function names.
• Also, read the instructions on the web page on how to test your functions with the auto-grader program provided, and what to submit to Coursera to get credit.
• Note that starred problems, marked by ***, are harder than usual, so do not get discouraged if you have difficulty solving them.
• We have not covered loops yet, so they are neither necessary nor allowed!
• You need MATLAB r2012a or newer or MATLAB Online to run the grader! Older versions are not supported.
2. Write a function called fare that computes the bus fare one must pay in a given city based on the distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Miles are rounded to the nearest integer other than the first mile which must be paid in full once a journey begins. Children 18 or younger and seniors 60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no discount.
4. Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the difference between the ages of the two children in days. Make sure to check that the input values are of the correct types and they represent valid dates. If they are erroneous, return -1. An example call to the function would be
>> dd = day_diff(1,30,2,1);
which would make dd equal 2. You are not allowed to use the built-in function datenum or datetime.
Hint: store the number of days in the months of 2015 in a 12-element vector (e.g., 31, 28, 31, 30 …) and use it in a simple formula.
6. Write a function called poly_val that is called like this p = poly_val(c0,c,x), where c0 and x are scalars, c is a vector, and p is a scalar. If c is an empty matrix, then p = c0. If c is a scalar, then p = c0 + c*x . Otherwise, p equals the polynomial,
𝑐𝑐0 + 𝑐𝑐(1)𝑥𝑥1 + 𝑐𝑐(2)𝑥𝑥2 + ⋯ + 𝑐𝑐(𝑁𝑁)𝑥𝑥𝑁𝑁 ,
>> format long
>> p = poly_val(-17,[],5000) p = -17
>> p = poly_val(3.2,[3,-4,10],2.2) p =
96.920000000000030
>> p = poly_val(1,[1,1,1,1],10) p = 11111
7. *** Write a function called exp_average that computes the “exponentially weighted moving average,” or
“exponential average” for short, of a sequence of scalars. The input sequence is provided to the function one element at a time and the function returns the current average each time. If we denote the nth element of the input sequence, that is, the function input at the nth invocation, by inn, then the rule for calculating the corresponding average outn that is to be returned by the function is:
out1 = in1
outn = b ∙ inn + (1 – b) ∙ outn-1
where b is a coefficient between 0 and 1. You do not need to check b. In plain English, the current average depends on the current input and the previously computed average weighted by b and (1 – b), respectively. Here is how the function is expected to work. When called by two input arguments, the input sequence is reset, the first input argument is considered to be in1 and the second input argument is the value of the coefficient b. When called with a single input argument, it is considered to be int, that is, the current value of the input sequence. In both cases, the output is calculated according to the formula above. If the function is called for the very first time with a single input argument, the value of the coefficient b must default to 0.1. Hint: you should use two persistent variables to store the output and the coefficient b.
2
8. Write a function that is called like this: mbd = spherical_mirror_aberr(fn,D), where all arguments are scalars, fn is the “f-number” of a concave spherical mirror, D is its diameter in millimeters, and mbd is the mean blur diameter in millimeters. The f-number equals the focal length f of the mirror divided by its diameter. Ideally, all the rays of light from a distant object, illustrated by the parallel lines in the figure, would reflect off the mirror and then converge to a single focal point. The magnified view shows what actually happens. The light striking a vertical plane at a distance f from the mirror is spread over a circular disk. The light from the center of the mirror strikes the center of the disk, but light arriving from a point a distance x from the center of the mirror strikes the plane on a circle whose diameter d is equal to 2𝑓𝑓 tan(2𝜃𝜃) cos𝜃𝜃 − 1, where
𝑥𝑥 𝑥𝑥
𝜃𝜃 = arcsin 2𝑓𝑓 , which is the angle whose sine equals 2𝑓𝑓 . The function calculates d for all values of x in the vector x = 0:delta_x:D/2, where delta_x = 0.01. Then it calculates the mean
blur diameter using this weighted formula: mbd 8 ∗ delta_x
= D𝟐𝟐 ∗ 𝑥𝑥𝑛𝑛𝑑𝑑𝑛𝑛 ,
where the sum includes all x(n)d(n), and returns it.
Here are three example runs:
>> format long
>> mbd = spherical_mirror_aberr(8,152) mbd =
0.029743954651679
>> mbd = spherical_mirror_aberr(8,300) mbd =
0.058695642823892
>> mbd = spherical_mirror_aberr(10,300) mbd =
0.037543964166654
3




Reviews
There are no reviews yet.