{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2sol

# hw2sol - %problem 1 bungee problem clear all%input xl =...

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

%problem 1: bungee problem clear all; %input xl = 50; %initial guesses xu = 200; Ftol = 1e-4; %function tolerance Xtol = 1e-5; %root tolerance %bungee function f=inline('sqrt(m*9.81/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36'); %use bisection [iter1,m1] = bisection_store(f,xl,xu,Ftol,Xtol); %use false position [iter2,m2] =false_position(f,xl,xu,Ftol,Xtol); %plot the output plot(iter1,m1,'s-',iter2,m2,'rd-'); xlabel('Iterations');ylabel('mass');title('Bungee problem: mass Vs. iterations'); legend('Bisection','False position');

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

View Full Document
function [iter,xout] = bisection_store(f,xl,xu,Ftol,Xtol) %Bisection method %Input: % f is the inline function object, used to evalute the given function %xl is the lower x guess value %xu is the upper x guess value %Ftol function tolerance %Xtol x-value tolerance %ouput: %niter: number of iterations the routine took %xout : root at each intermediate array of size, niter %control parameters ITERMAX = 100; % max. no. of iterations allowed %Ftol = 1e-3; % function tolerance %Xtol = 1e-3; % x-value tolerance %set the output values to default: -1 denotes no convergence iter = -1; xout = 0; %preallocate the memory for intermediate soln %xinter = zeros(ITERMAX,1); fu = f(xu); fl = f(xl); if fl*fu>0 %if guesses do not bracket a sign change disp(['No bracket.' 'fl is ' num2str(fl) ' and fu is ' num2str(fu)]) %display an error message return %and terminate end %initial solution xr = xl; for i=1:ITERMAX fu = f(xu); fl = f(xl); %old soln xrold = xr; %next estimate for root xr = (xu+xl)/2; fr = f(xr); %store the intermediate solution at each iteration %xinter(i) = xr;
xout(i) = xr; iter(i) = i; %find which side of the root xr is on if(fr*fu) < 0 xl = xr; else xu = xr; end %convergence test xerror = abs((xr-xrold)/xr); % difference in x 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 / 11

hw2sol - %problem 1 bungee problem clear all%input xl =...

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

View Full Document
Ask a homework question - tutors are online