for F(X)=x^3-2x and F'(x)=3x^2-2 with initial guess of x=1
if true % code
endx = 1;Tol = 0.0000001;count = 0;dx=1; f=-1; 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) count = count + 1; fprime = 3*x^2 - 2; xnew = x - (f/fprime); dx=abs(x-xnew); x = xnew; f = x^3 - 2*x; fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)end
Best Answer