{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

E7_Su2010_Solutions7_all

E7_Su2010_Solutions7_all - E7 Laboratory 7 Solutions E7...

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

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

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

View Full Document
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
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

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

View Full Document
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); root iter error_value root = 6.2822 iter = 10 error_value = 9.7656e-004 Here we ran out of iterations, so the tolerance is not met. f(x) = log(x) f = @(x)( log(x) ); lower = 0.1; upper = 3; 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); 7/29/2010 E7 Laboratory 7, Solutions C:/…/lab07soln.html 4/21
root iter error_value root = 1.0000 iter = 18 error_value = 3.8147e-007 f(x) = x^3 + 2x^2 -10x Find the negative root f = @(x)( x^3 + 2*x^2 - 10*x ); lower = -8; upper = -0.1; tol = 1e-6; error_type = 'a' ; max_iter = 100; [root,root_history,iter,error_value] = ...

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 / 21

E7_Su2010_Solutions7_all - E7 Laboratory 7 Solutions E7...

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

View Full Document
Ask a homework question - tutors are online