Newton�s Method

Newton�s Method - f1 = f(x1); plot(x1,f1, 'bo' )...

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

View Full Document Right Arrow Icon
%Newton’s Method function [x]=Newton(a,b,approx,tol,iter) format long ; n = 100; h = ((b-a)/(n+1)); y = [a:h:b]; g = f(y); figure(1) hold; plot(y,g, 'r-' ); i = 1; x0=approx; f0=f(x0); plot(x0,f0, 'bo' ) pause; while (i <= iter) x1 = x0 - (f(x0)./deriv(x0)); f1 = f(x1); plot(x1,f1, 'bo' ) fprintf(1, '%13.7g\t %13.7g\t %13.7g\n' , x1, f1, abs(x1- x0)); if (abs(x1-x0) > tol) x0 = x1; i=i+1; else i=iter +2; end end x=x1; hold; %Newton’s Method with the better equation function [x]=BetterNewton(a,b,approx,tol,iter) format long ; n = 100; h = ((b-a)/(n+1)); y = [a:h:b]; g = f(y); figure(1) hold; plot(y,g, 'r-' );
Background image of page 1

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

View Full DocumentRight Arrow Icon
i = 1; x0=approx; f0=f(x0); plot(x0,f0, 'bo' ) pause; while (i <= iter) x1= x0 - ((f(x0).*deriv(x0))./(((deriv(x0)).^2) - (f(x0).*secondderiv(x0))));
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: f1 = f(x1); plot(x1,f1, 'bo' ) fprintf(1, '%13.7g\t %13.7g\t %13.7g\n' , x1, f1, abs(x1-x0)); if (abs(x1-x0) &gt; tol) x0 = x1; i=i+1; else i=iter +2; end end x=x1; hold; %for the f(x) function [y] = f(x) %y=cos(x+sqrt(2)) + x.*((x./2) + sqrt(2)); %equation for 1.b y=(x.^6) + (6.*(x.^5)) + (9.*(x.^4)) - (2.*(x.^3)) - (6.*(x.^2)) + 1; %y=1./x; %for the differentiated f(x) function [y] = deriv(x) %y=x - sin(x + 2.^(1/2)) + 2.^(1/2); %equation for 1.b y= 6.*x.^5 + 30.*x.^4 + 36.*x.^3 - 6.*x.^2 - 12.*x; %y=(-x.^(-2)); %for the second derivitive of f(x) function [y] = secondderiv(x) %y = 1 - cos(x + 2^(1/2)); %for the equation of 1.b y = 30.*x.^4 + 120.*x.^3 + 108.*x.^2 - 12.*x - 12;...
View Full Document

This note was uploaded on 08/28/2009 for the course CS 50 taught by Professor Staff during the Spring '08 term at UCSB.

Page1 / 2

Newton�s Method - f1 = f(x1); plot(x1,f1, 'bo' )...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online