{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08_Matlab Assignment 2

# 08_Matlab Assignment 2 - Initial Guess of 1 for Part 1...

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

Initial Guess of 1 for Part 1: Matlab Code: x = 1; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=-1; % because f(-2)=-13 fprintf( 'step x dx f(x)\n' ) fprintf( '---- ----------- --------- ----------\n' ) fprintf( '%3i %12.8f %12.8f %12.8f\n' ,count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = x^3 -2*x; xnew = x - (f/fprime); % compute the new value of x dx=abs(x-xnew); % compute how much x has changed since last step x = xnew; f = x^3 + 3*x + 1; % compute the new value of f(x) fprintf( '%3i %12.8f %12.8f %12.8f\n' ,count,x,dx,f) end % This produces the following output: step x dx f(x) ---- ----------- --------- ---------- 0 1.00000000 1.00000000 -1.00000000 1 0.00000000 1.00000000 1.00000000 2 -Inf Inf -Inf 3 NaN NaN NaN

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

View Full Document
Initial Guess of -1 for Part 1: Matlab Code: x = -1; Tol = 0.0000001; count = 0; dx=1; %this is a fake value so that the while loop will execute f=1; % because f(-2)=-13 fprintf( 'step x dx f(x)\n' ) fprintf( '---- ----------- --------- ----------\n' ) fprintf( '%3i %12.8f %12.8f %12.8f\n' ,count,x,dx,f) xVec=x;fVec=f; while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed count = count + 1; fprime = x^3 -2*x; xnew = x - (f/fprime);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

08_Matlab Assignment 2 - Initial Guess of 1 for Part 1...

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

View Full Document
Ask a homework question - tutors are online