hw14_solutions

hw14_solutions - Homework #4 Solutions for problems from...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Homework #4 Solutions for problems from Section 2.5, Section 2.6 can be found in the solution to Homework #3. Section 3.1 1. () We have ( ) Three iterations are required. () The first iteration gives us: ( Therefore the new interval is [ ) [ The second iteration gives us: ( Therefore the new interval is [ ) [ The third iteration gives us: ( Therefore the new interval is [ 2a. ) () We have ( ) [ Desired accuracy is () The first iteration gives us: ( Therefore the new interval is [ ) [ Length of the interval therefore we need to continue. The second iteration gives us: ( Therefore the new interval is [ ) [ Length of the interval therefore we need to continue. The third iteration gives us: ( Therefore the new interval is [ ) Length of the interval [ therefore we need to continue. The fourth iteration gives us: ( ) [ Length of the interval Therefore the new interval is [ desired accuracy is achieved. It took 4 iterations to get accuracy of . This result coincides with formula ( ) ( ) () 3a, f. Here is the MATLAB code (2 functions): First m.file %Function funvalue returns value of function f at point x function [ f ] = funvalue(x) % function for problem 3a f=x^3-2; % function for problem 3f % f=1-2*x*exp(-x/2); end Second m.file % Function bisection finds a root in the interval [a,b] % with desired accuracy eps by means of bisection method. % It returns the interval [a,b] inside which the root is located % and the number of iterations k. function [a,b,k] = bisection( a,b,eps ) format long % Check if the number of input arguments is correct if nargin ~= 3 error ('Wrong number of input arguments') end % Check if function has different signs at a and b if funvalue(a)*funvalue(b)>0 error('Function should be of different signs') end % Check if a or b are roots of the function if funvalue(a)==0 disp(['root is found, x=', num2str(a,12)]) return end if funvalue(b)==0 disp(['root is found, x=', num2str(b,'%10.5e')]) return end % Check that interval is entered properly, i.e. a<b if a>=b error('Improper interval') end % Start counting the number of steps required for % desired accuracy k = 0; %Defining the stopping criteria for the number of steps %(formula (3.2) in the textbook) N=(log(b-a)-log(eps))/log(2); while k<=N %while the stopping criteria was not reached % find the middle point c = a+( b - a)/2; % Check if c is the exact root of the equaion if funvalue(c)==0 str = sprintf('root is found exactly: x= %4.16f',c); disp(str) return end % Depending on the sign of function in the middle point % obtain a new interval if sign(funvalue(c)) == sign(funvalue(b)) b = c; else a = c; end k = k + 1; end %Displaying the result str = sprintf('root is located inside interval [ %4.16f , %4.16f ]', a, b); disp(str); end Calling functions from command line: Part a: >> [a,b,k]=bisection(0,2,10^(-6)) root is located inside interval [ 1.2599201202392578 , 1.2599210739135742 ] a= 1.259920120239258 b= 1.259921073913574 k= 21 Part f: [a,b,k]=bisection(0,2,10^(-6)) root is located inside interval [ 0.7148056030273438 , 0.7148065567016602 ] a= 0.714805603027344 b= 0.714806556701660 k= 21 ...
View Full Document

This note was uploaded on 01/15/2012 for the course MAE 107 taught by Professor Rottman during the Spring '08 term at UCSD.

Ask a homework question - tutors are online