hw2sol

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

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

View Full Document Right Arrow Icon
%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');
Background image of page 1

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

View Full DocumentRight Arrow Icon
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;
Background image of page 2
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 if((abs(fr) < Ftol) && (xerror< Xtol)) % check if both function and change in x is below epsilon niter = i; % no. of iterations
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online