Description
1.3 
Basic MATLAB func1on using VECTORIZATION, and ERROR defini1ons 
x^{3} 
x^{5} 
x^{7} 
x^{9} 

6 pts 
You probably already know from calculus that you can write 
sin(x) = x − 
+ 
− 
+ 
− ! 

a) Create a MATLAB func>on called FUNC1_3.m to calculate 
3! 
5! 
7! 
9! 

N 
(k−1) 2 x^{k} 

the sum of the terms up to the power x^{N}. That is, evaluate 
S(x, N ) = 
∑ 
(−1) 

For example, S (2,11) = 2 − 
2^{3} 
2^{5} 
2^{7} 
2^{9} 
_{2}11 
= 0.909296136… ^{k}^{=}^{1,3,5,} ^{…} 
k! 

+ 
− 
+ 
− 

3! 
5! 
7! 
9! 
11! 

Your func>on FUNC1_3 must accept two inputs (x and N) and output one value represen>ng S(x,N). 

What’s the catch? NOWHERE in your func1on can you use FOR or WHILE loops, or IF 

commands!!! You must think how to do all calcula>ons in a “vectorized” way, as discussed in class. 

(Hint: maybe create a vector of just the odd values of k from 1 to N; then use MATLAB’s elementby 

element math operators to create a vector in which the elements are (−1)^{(}^{k}^{−}^{1) 2} xk 
k! ; then use the 
sum func>on to add up the terms of the vector. Or, maybe you have another way of doing it. Just don’t use forwhileif !!)

Use your func>on to demonstrate to yourself that the series converges (as N à ∞) to the limit 1 for x = π/2. (This should make sense to you because you know sin(π/2) = 1.)
Do the demonstra>on by using your new MATLAB func>on to calculate S(x,N) for x = pi/2 and N = 5, 11 and 17, and you should see the result ge[ng closer to 1 as N gets bigger. Let’s define the “error” as the diﬀerence between the “exact” sin(x) and the “approximate” S(x,N).
Now calculate the “errors” (i.e. diﬀerences between 1 and S(π/2, N)) for N = 5, 11 and 17. Use format long to be sure to capture each of the three “errors” to at least three significant figures. These are the three values you’re going to enter into Carmen. You should see these “errors” get closer to 0 as N gets larger.
For this problem, please submit the following:
• ONLINE (in HW1B assignment folder): Four things! Your documented func>on FUNC1_3.m, and your three values of error for S(π/2, 5), S(π/2, 11), S(π/2, 17), in the comment sec>on (clearly labeled).
By “Comment Sec6on” I mean type it right into the comment field beside where you upload files for your assignment on Canvas, just like you did in HW1.1. It is NOT suﬃcient to just type it as a part of your documenta6on inside your func6on FUNC1_3.m. I want the graders to be able to see your results before they even open up and run your code.
Also – don’t forget! You need to submit ALL the files for problems 1.3, 1.4 and 1.5 ALL AT ONCE to your HW1B assignment on Canvas. Like we talked about in the recita6ons, if you submit files
for 1.3 first, then try to add the files for 1.4 later, you will ERASE the files from 1.3, and you’ll end up with a grade of 0!
1.4 MATLAB script using FOR loops and IFTHEN statements
7 pts Create a MATLAB func>on called FUNC1_4.m which starts with the following line:
function C = FUNC1_4(A,B,option)
This func>on will take inputs A and B (vectors or matrices of any size) and option (scalar number from 1 to 3) and create the output matrix C , where

•
C=A+B,
if option = 1,
•
C
=
A
.* B,
if option = 2,
•
C
=
A
* B,
if option = 3.
However, this isn’t quite as easy at it sounds because you must follow the following two rules:
1. You can NOT use vectorized MATLAB opera>ons, like A+B, A.*B, A*B as shown above, which do all the addi>ons and mul>plica>on in one command. You MUST use mul>ple, nested for loops to break the problem down into the individual mul>plica>ons and/or addi>ons performed on just one pair of scalar numbers from A and B at a >me (sort of like how we first calculated the cannonball energies in Class 2).
2. You must check at the beginning of the func>on that the input matrices A and B have consistent sizes (depending on the value of op>on) to allow for a mathema>cally valid C. For example, if op>on = 2 then A and B must have iden>cal size (since A .* B only makes sense if A and B have the same size), but if op>on = 3 then there’s a diﬀerent requirement on the rela>ve size between A and B to ensure A*B makes sense.
If you detect an inconsistency in the size of A and B, such that C can not be validly evaluated, then have your func>on indicate that by se[ng C = ‘No good! Garbage.’ instead of calcula>ng an actual value for C.
Finally, it’s OK to assume that A and B will never be scalars, but rather true vectors or matrices, where at least one of the dimensions is greater than 1.
Be sure to test your func>on out to make sure it works!
HW1.4: Please upload your final documented func>on FUNC1_4.m, to HW1B in Carmen.
HW1B (1.3 – 1.5) Due by Beginning of Class, Friday
1.5 More advanced MATLAB script using VECTORIZATION and 3D PLOTTING
7 pts A systemsengineering assessment of a dollhead making machine has

determined that the daily produc>on of
s
s 30
2
heads can be modeled by the equa>on
P =10 s −
e
(
t −1
2 ×10^{6}
where
)
• P is the number of doll heads produced on day t.
• t is the day of opera>on (star>ng at day t = 1 when it’s first turned on),
• s is the machine seXng (percent of max opera>on, between 0 = oﬀ and 100 = max opera>on), Assume P can be any number ≥ 0. Even frac>ons are OK. But if you ever calculate P < 0 (“nega>ve produc>on??”) just assume P = 0 that day.
Aside: Just to make sure you understand the equa>on, first check that you agree with all the following:
• If s = 0 (0% max opera>on) then P = 0 for all >me t (i.e. no dolls are ever made). This makes sense because the machine is always oﬀ!
• At t = 1 (the first day you start opera>ng the machine) P = 10 s, so the machine can produce between 0 to 1000 heads that first day depending on the machine se[ng from s = 0 to 100.
• For a fixed se[ng s, as >me goes on, the daily rate of head produc>on P(t) drops, presumably because the machine gets old and doesn’t operate as eﬃciently. In fact, for higher se[ngs (as s goes to 100) the machine overheats faster and the rate of eﬃciency dropoﬀ can get so big that
P drops to 0! (See my plot of P(t) at right for s = 100)
^{• Once }^{P}^{ hits 0 or goes nega>ve, assume the machine }1000

is now broken, and can no longer product doll heads
for later >mes.
500
• The plot at right is of P(t) for s = 100, and shows the
trends I described above: See P starts at 1000 dolls/day at t = 1, then drops oﬀ rapidly over >me, finally “breaking” and producing no more heads (P = 0)
for t ≥ 846.
0
0 200 400 600 800 1000

0
Create a MATLAB script called DOLLHEAD.m that does all the following:
5
10
a) Creates a column vector s of all se[ngs from 0 to 100, skipping by 5, so s =
!
.
b) Creates a vector t of days from 1 to 1200, so t = [1 2 3 … 1200].
100

Uses a single, vectorized equa>on to create a matrix of values P for all the s and t values in the vectors above. You can NOT use any for or while loops to do this calcula>on! The equa>on must be cleverly vectorized to calculate P with a single equa>on using the en>re s and t vectors at once.

Overrides all values of P < 0 to be equal to zero (i.e. no produc>on when the machine is broken).
Now you should have a [21 x 120] matrix for P, where each row represents the daily produc>on of heads for one par>cular se[ng. You should doublecheck that the row corresponding to s = 100 (max opera>on) has exactly the shape of the plot I show above before moving on.
(If you choose to con>nue this adventure, please go to next page …)
HW1B (1.3 – 1.5) Due by Beginning of Class, Friday
1.5 con1nued …

Make a plot of all 21 P(t) lines, one for each se[ng s, on top of each other in one graph. I expect to see 21 lines on the same plot. This might be as easy as just using the command plot(t,P). Save this plot in pdf form called DHplot.pdf.
Now I want you to figure out WHICH se[ng s (between 0 and 100) produces the maximum number of doll heads over the machine’s life>me. For example, if you look at the plot you made in (e), running at a low se[ng (like s = 10) doesn’t produce very many doll heads each day, but at least the machine works for all 1200 days. But running at max se[ng (s = 100) produces the most doll heads ini6ally, but the machine breaks the soonest, so maybe there’s a middle se[ng s that is the best “overall”. To determine this op6mal se[ng do the following:

In your code, make a new column vector called TOTAL , for which each element in TOTAL represents the total number of dolls produced over the 1200 day life>me of the machine for each se[ng s. In otherwise, TOTAL should be a vector of length 21, with the answer for each se[ng in the vector s.

Look at the vectors s and TOTAL to figure out which value of the se[ng s produced the maximum number of doll heads. It’s fine to do this by eye outside your code, or you can try to automate this in MATLAB too if you like (there’s no bonus points for doing so, but maybe you like a challenge!).
HW1.5: Please submit the following 3 things ONLINE in the HW1B folder on Carmen:
• Upload your final documented main script DOLLHEAD.m, • Upload your labeled plot DHplot.pdf,
• Enter your value for the op>mal se[ng s from (g) in the COMMENT sec>on for this HW in
Carmen. Follow the format below:
“HW1.5: The op>mal se[ng for s is 20” (but replace 20 with whatever your # is)
HW1B Summary:
Since there’s three problemsworth of material (1.3, 1.4, 1.5) you’re uploading all at once, here’s a summary of everything you’re submi[ng to HW1B on :
HW1.3:
• your documented func>on FUNC1_3.m
• your three values of error for S(π/2, 5), S(π/2, 11), S(π/2, 17), in the comment sec>on (clearly labeled).
HW1.4:
• your documented func>on FUNC1_4.m.
HW1.5:
• your documented main script DOLLHEAD.m,
• your labeled plot DHplot.pdf,
• your value for the op>mal machine se[ng s in the comment sec>on