solution_hw6 - Solution to hw_6 2. Function file of...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Solution to hw_6 2. Function file of ‘secant.m’ function [xr] = secant(fun,x1,x2) tol=1.0E-9; % declare a low tolerance f1 = feval(fun, x1); % calculate function value at x1 f2 = feval(fun, x2); % calculate function value at x2 xr=x2-f2*(x1-x2)/(f1-f2); % calculate the new guess x fr = feval(fun,xr); iter=1; while ( abs(fr) > tol ) x1=x2; x2=xr; f1=f2; f2=fr; xr=x2-f2*(x1-x2)/(f1-f2); % update xr using x1, x2, f1, f2 fr = feval(fun,xr); % update fr correspondingly iter=iter+1; % count the ieration number end %...output result. .... fprintf( 'root of function is: %f \n' ,xr); fprintf( 'total iteration number: %d \n' , iter); function file of ‘test.m’ which is the function in problem 1 function [f] = test(x) % just a randomly selected test function where we can make sure our % programing works f=-0.9*x^2+1.7*x+2.5; testing the secant file in command window, we have: >> secant('parachute', 0.5,0.6) root of function is: 14.780204 total iteration number: 7 >> secant('test', 0.5,0.6) root of function is: -0.971216 total iteration number: 9
Background image of page 3

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

View Full DocumentRight Arrow Icon
3. the function file ‘Peng_Robinson_v2.m’ modified from previous
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

solution_hw6 - Solution to hw_6 2. Function file of...

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