matlab program for newton raphson

2
% Matlab program for Newton’s forward difference interpolation method clc; clear; n=input('\n Enter the no. of data entries( data points)='); % Loop to enter the given values of x and y ordinates for i=1:1:n x(i)=input('\n Enter the given value of x ordinate='); y(i,1)=input('\n Enter the given value of y ordinate='); end xr=input('\n Enter the value of xr at which value of function is to be interpolated='); % Loop to calculate forward differences in NFDT k=n; for j=2:1:n k=k-1; for i=1:1:k y(i,j)=y(i+1,j-1)-y(i,j-1); end end % Loop to print Newton’s forward difference table k=n; fprintf('\n The Newton’s forward difference table is as follows'); for i=1:1:n fprintf('\n %3.3f',x(i)); for j=1:1:k fprintf('\t %3.3f',y(i,j)); end k=k-1; end h=x(2)-x(1); r=(xr-x(1))/h; fprintf('\n The value h=%f',h); fprintf('\n The value of r=%f',r); % Loop to calculate value of yr at x=xr using Newton’s forward polynomial equation sum=0; for t=2:1:n fy=1; facto=1; for m=0:1:t-2 fy=fy*(r-m); facto=facto*(m+1); end fy=(fy/facto)*y(1,t); sum=sum+fy; end yr=sum+y(1,1);

Upload: sharvin-ghodekar

Post on 27-Oct-2015

51 views

Category:

Documents


5 download

DESCRIPTION

Matlab Program for Newton

TRANSCRIPT

% Matlab program for Newton’s forward difference interpolation methodclc;clear;n=input('\n Enter the no. of data entries( data points)=');% Loop to enter the given values of x and y ordinatesfor i=1:1:n x(i)=input('\n Enter the given value of x ordinate='); y(i,1)=input('\n Enter the given value of y ordinate=');endxr=input('\n Enter the value of xr at which value of function is to be interpolated=');% Loop to calculate forward differences in NFDTk=n;for j=2:1:n k=k-1; for i=1:1:k y(i,j)=y(i+1,j-1)-y(i,j-1); endend% Loop to print Newton’s forward difference tablek=n;fprintf('\n The Newton’s forward difference table is as follows');for i=1:1:n fprintf('\n %3.3f',x(i)); for j=1:1:k fprintf('\t %3.3f',y(i,j)); end k=k-1;endh=x(2)-x(1);r=(xr-x(1))/h;fprintf('\n The value h=%f',h);fprintf('\n The value of r=%f',r);% Loop to calculate value of yr at x=xr using Newton’s forward polynomial equationsum=0;for t=2:1:n fy=1; facto=1; for m=0:1:t-2 fy=fy*(r-m); facto=facto*(m+1); end fy=(fy/facto)*y(1,t); sum=sum+fy;endyr=sum+y(1,1);fprintf('\n The value of y=yr at x=xr=%f is yr=%f',xr,yr);% To plot graph between given x and y ordinatesplot(x,y,'b*-');xlabel('X');ylabel('Y');title('Graph between X and Y');