{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

E7_Su2010_Solutions4_all

# E7_Su2010_Solutions4_all - Homework 4 Solutions Homework 4...

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

Homework 4 Solutions Contents Problem 1 (a) (b) (c) (d) (e) Problem 2 Problem 3 (a) (b) Problem 4 Problem 5 Problem 1 (a) for k = 1:10 disp(k^2) end 1 4 9 16 25 36 49 64 81 100 (b) for k = 20:-1:13 disp(log(k)) end 2.9957 2.9444 2.8904 2.8332 2.7726 2.7081 2.6391 7/14/2010 Homework 4 Solutions C:/…/lab4soln.html 1/8

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

View Full Document
2.5649 (c) The my_factorial function must use a for loop. type my_factorial function nfac = my_factorial(n) nfac = 1; for k = 1:n nfac = nfac*k; end As announced on bSpace, they should test their my_factorial function on the following values: my_factorial(0) ans = 1 my_factorial(1) ans = 1 my_factorial(5) ans = 120 my_factorial(10) ans = 3628800 (d) Many students used their my_factorial function in this part, but we told them that they could also use the built-in factorial function. The assignment does not explicitly ask for a for loop, so it's okay if they solved it another way, as long as it's correct. 7/14/2010 Homework 4 Solutions C:/…/lab4soln.html 2/8
type exp_app function expApprox = exp_app(x,N) expApprox = 1; for n = 1:N expApprox = expApprox + x^n/factorial(n); end x = 2; N = [1 2 4 8 10]; approx = zeros(1,5); for k = 1:5 approx(k) = exp_app(x,N(k)); end plot(N,approx) (e) Create a magic square of order 6. They don't really need to explain what a magic square is or what

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 ]}