08_Matlab Assignment 2

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

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 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 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;...
View Full Document

This note was uploaded on 11/26/2010 for the course MATH 110 taught by Professor Gordon during the Fall '10 term at NJIT.

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