E7_Su2010_Solutions7_all

E7_Su2010_Solutions7_all - 7/29/2010 E7 Laboratory 7,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
E7 Laboratory 7, Solutions Contents Problem 1 f(x) = x^2-4 f(x) = sin(x) f(x) = log(x) f(x) = x^3 + 2x^2 -10x Problem 2 f(x) = x^2-4 f(x) = sin(x) f(x) = log(x) f(x) = x^3 + 2x^2 -10x Problem 3 (a) (b) Problem 4 (a) (b) (c) (d) Problem 5 (a) (b) (c) (d) (e) (f) Problem 6 Problem 7 (100 Trials) (1000 Trials) (5000 Trials) Problem 8 Problem 9 Problem 1 type my_bisection function [root,root_history,iter,error_value] = my_bisection. .. (fh,lower,upper,tol,error_type,max_iter) % Verify that the interval is valid if upper <= lower error('Lower bound must be less than the upper bound') 7/29/2010 E7 Laboratory 7, Solutions C:/…/lab07soln.html 1/21
Background image of page 1

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

View Full DocumentRight Arrow Icon
end if fh(lower)*fh(upper) > 0 error('Interval must contain one root') end % See if one of the endpoints is a root and initialize error_value if fh(lower) == 0 root = lower; error_value = 0; elseif fh(upper) == 0 root = upper; error_value = 0; else % Make sure we do at least one iteration error_value = tol + 1; end % Determine which error function to use switch error_type case 'a' error_func = @(new,old)( abs(new-old) ); case 'r' error_func = @(new,old)( abs(new-old)/abs(new) ); case 'f' error_func = @(new,old)( abs( fh(new) ) ); otherwise error('Invalid error type') end root_history = []; iter = 0; while (error_value > tol) && (iter < max_iter) iter = iter + 1; midpoint = (lower+upper)/2; if iter == 1 error_value = error_func(midpoint,lower); else error_value = error_func(midpoint,root_history(iter-1)); end if fh(lower)*fh(midpoint) > 0 lower = midpoint; elseif fh(midpoint)*fh(upper) > 0 upper = midpoint; end root = midpoint; root_history(end+1) = root; end f(x) = x^2-4 Find the root at -2 7/29/2010 E7 Laboratory 7, Solutions C:/…/lab07soln.html 2/21
Background image of page 2
f = @(x)( x^2 - 4 ); lower = -3; upper = 0.9; tol = 1e-6; error_type = 'a' ; max_iter = 100; [root,root_history,iter,error_value] = ... my_bisection(f,lower,upper,tol,error_type,max_iter); root iter error_value root = -2.0000 iter = 22 error_value = 9.2983e-007 Find the root at 2 lower = 0.5; upper = 5; tol = 1e-6; error_type = 'r' ; max_iter = 100; [root,root_history,iter,error_value] = ... my_bisection(f,lower,upper,tol,error_type,max_iter); root iter error_value root = 2.0000 iter = 22 error_value = 5.3644e-007 f(x) = sin(x) Find the root at pi f = @(x)( sin(x) ); lower = 3; upper = 4; 7/29/2010 E7 Laboratory 7, Solutions C:/…/lab07soln.html 3/21
Background image of page 3

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

View Full DocumentRight Arrow Icon
tol = 1e-6; error_type = 'f' ; max_iter = 100; [root,root_history,iter,error_value] = ... my_bisection(f,lower,upper,tol,error_type,max_iter); root iter error_value root = 3.1416 iter = 19 error_value = 6.2783e-007 Find the root at 2*pi lower = 6; upper = 7; tol = 1e-6; error_type = 'a' ; max_iter = 10; [root,root_history,iter,error_value] = ... my_bisection(f,lower,upper,tol,error_type,max_iter);
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/14/2010 for the course E 7 taught by Professor Patzek during the Summer '08 term at University of California, Berkeley.

Page1 / 21

E7_Su2010_Solutions7_all - 7/29/2010 E7 Laboratory 7,...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online