{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14-440-127 Recitation 7 Problems- Solutions

# 14-440-127 Recitation 7 Problems- Solutions - 14:440:127...

This preview shows pages 1–4. Sign up to view the full content.

14:440:127- Introduction to Computers for Engineers Problems for students to attempt in Recitation 7- Solutions 1) Use a 'for loop' to calculate the sum of all prime numbers from 1000 to 2000. s = 0; for x= 1000:2000 if (isprime(x)) s=s+x; end end disp(s) 2) Use a 'while loop' to calculate the sum of all prime numbers from 1000 to 2000. s = 0; x=1000; %different while (x<=2000) %different if (isprime(x)) s=s+x; end x=x+1; %different end disp(s) 3) What sequence of numbers will be displayed by the following ‘for loop’? n = 10; for j = 1:n n = n-1 j end 9,1,8,2,7,3,6,4,5,5,4,6,3,7,2,8,1,9,0,10 4) Define values for three variables of your choosing. Write an anonymous function that switches the values of variables one and two and another that does so for variables two and three. switcheroo12 = @(x,y,z) deal(y,x,z) switcheroo23 = @(x,y,z) deal(x,z,y) (Note… this wasn’t a very good question since you needed to use the deal( ) function, which you haven’t seen before ) 5) Write a program to plot the curve y=sin(x)+cos(x) within [0,2 π ]. a) use fplot and a normal user-defined function. [In the mfile myfunc.m] function y = myfunc(x) y=sin(x)+cos(x); [In the workspace] fplot('myfunc(x)',0:2*pi)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
b) use fplot with an anonymous function that defines y. myanonfunc = @(x) sin(x)+cos(x); fplot(myanonfunc,0:2*pi) [Note that you don’t need to use quotes around myanonfunc, and in fact you cannot use quotes around myanonfunc. The reason is that Matlab stores what’s called a “function handle” in the variable myanonfunc, so you just pass fplot the function handle 6) The Fibonacci numbers are a sequence of numbers and defined as, The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself. Use a ‘for’ loop to calculate the 100 th number F 100 . Here’s one way to write the answer that saves only the final answer first=0; second=1; for x=2:100 temp = second; second = first+second; first = temp; end disp(second) Here’s a second way to write the solution. You end up with a vector v containing the first 100 numbers in the fibonacci sequence. v(1)=1; v(2)=1; for x=3:100 v(x) = v(x-1) + v(x-2); end disp(v(100)) 7) Use a ‘while’ loop to find the first Fibonacci number larger than 10,000. Then, try writing this as a ‘for’ loop (using break). first=0; second=1; while (second<=10000) temp = second; second = first+second; first = temp; end disp(second) first=0; second=1; for x=2:inf temp = second; second = first+second; first = temp; if (second>10000) break end end disp(second)
8) Create a 15 by 15 matrix of all one value (your choice), then write a nested ‘for loop’

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

14-440-127 Recitation 7 Problems- Solutions - 14:440:127...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online