Starting from:
$30

$24

HW1B (1.3 – 1.5) Solution




HW1B (1.3 – 1.5) Solution






1.3
Basic MATLAB func1on using VECTORIZATION, and ERROR defini1ons


x3


x5




x7






x9


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





+
− !












a) Create a MATLAB funcon called FUNC1_3.m to calculate
3!
5!
7!




9!




N




(k−1) 2 xk




the sum of the terms up to the power xN. That is, evaluate
S(x, N ) =

(−1)




























For example, S (2,11) = 2 −
23


25


27


29


211


= 0.909296136... k=1,3,5, …










k!




+

+

































































3!
5!
7!
9!
11!


































Your funcon FUNC1_3 must accept two inputs (x and N) and output one value represenng 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 calculaons 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 element-by-


element math operators to create a vector in which the elements are (−1)(k−1) 2 xk


k! ; then use the



sum funcon to add up the terms of the vector. Or, maybe you have another way of doing it. Just don’t use for-while-if !!)




Use your funcon 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 demonstraon by using your new MATLAB funcon 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 difference between the “exact” sin(x) and the “approximate” S(x,N).




Now calculate the “errors” (i.e. differences 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 funcon FUNC1_3.m, and your three values of error for S(π/2, 5), S(π/2, 11), S(π/2, 17), in the comment secon (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 sufficient 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!
HW1B (1.3 – 1.5)




1.4 MATLAB script using FOR loops and IF-THEN statements




7 pts Create a MATLAB funcon called FUNC1_4.m which starts with the following line:




function C = FUNC1_4(A,B,option)




This funcon 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 operaons, like A+B, A.*B, A*B as shown above, which do all the addions and mulplicaon in one command. You MUST use mulple, nested for loops to break the problem down into the individual mulplicaons and/or addions performed on just one pair of scalar numbers from A and B at a me (sort of like how we first calculated the cannon-ball energies in Class 2).




2.  You must check at the beginning of the funcon that the input matrices A and B have consistent sizes (depending on the value of opon) to allow for a mathemacally valid C. For example, if opon = 2 then A and B must have idencal size (since A .* B only makes sense if A and B have the same size), but if opon = 3 then there’s a different requirement on the relave 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 funcon indicate that by se[ng C = 'No good! Garbage.' instead of calculang 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 funcon out to make sure it works!
















HW1.4: Please upload your final documented funcon 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 3-D PLOTTING







7 pts A systems-engineering assessment of a doll-head making machine has




determined that the daily producon of


s


s 30






2








heads can be modeled by the equaon
P =10 s −


e




(
t −1


2 ×106




where








)





•  P is the number of doll heads produced on day t.

•  t is the day of operaon (starng at day t = 1 when it’s first turned on),

•  s is the machine seXng (percent of max operaon, between 0 = off and 100 = max operaon), Assume P can be any number ≥ 0. Even fracons are OK. But if you ever calculate P < 0 (“negave producon??”) just assume P = 0 that day.







Aside: Just to make sure you understand the equaon, first check that you agree with all the following:

•  If s = 0 (0% max operaon) then P = 0 for all me t (i.e. no dolls are ever made). This makes sense because the machine is always off!




•  At t = 1 (the first day you start operang 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 producon P(t) drops, presumably because the machine gets old and doesn’t operate as efficiently. In fact, for higher se[ngs (as s goes to 100) the machine overheats faster and the rate of efficiency drop-off 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 negave, 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 off 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 equaon 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 calculaon! The equaon must be cleverly vectorized to calculate P with a single equaon using the enre s and t vectors at once.



Overrides all values of P < 0 to be equal to zero (i.e. no producon when the machine is broken).






Now you should have a [21 x 120] matrix for P, where each row represents the daily producon of heads for one parcular se[ng. You should double-check that the row corresponding to s = 100 (max operaon) has exactly the shape of the plot I show above before moving on.




(If you choose to connue 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 lifeme. 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 lifeme 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 opmal se[ng s from (g) in the COMMENT secon for this HW in




Carmen. Follow the format below:




“HW1.5: The opmal se[ng for s is 20” (but replace 20 with whatever your # is)
















HW1B Summary:




Since there’s three problems-worth 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 funcon FUNC1_3.m




•  your three values of error for S(π/2, 5), S(π/2, 11), S(π/2, 17), in the comment secon (clearly labeled).




HW1.4:




•  your documented funcon FUNC1_4.m.




HW1.5:




•  your documented main script DOLLHEAD.m,




•  your labeled plot DHplot.pdf,




•  your value for the opmal machine se[ng s in the comment secon

More products