solution_hw6

# solution_hw6 - Solution to hw_6 2 Function file of secant.m...

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

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

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

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

View Full Document
3. the function file ‘Peng_Robinson_v2.m’ modified from previous
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/21/2010 for the course PGE 310 taught by Professor Klaus during the Spring '06 term at University of Texas.

### Page1 / 4

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

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

View Full Document
Ask a homework question - tutors are online