You are on page 1of 10

Langrange:

clear all
clc
format long
X(1)=0.6;
X(2)=0.7;
X(3)=0.8;
X(4)=1.0;
Y(1)=-0.17694460;
Y(2)=0.01375227;
Y(3)=0.22363362;
Y(4)=0.65809197;
x=0.9;
P1 = (x-X(2))/(X(1)-X(2))*Y(1) + (x-X(1))/(X(2)-X(1))*Y(2);
P2 = (x-X(2))*(x-X(3))/((X(1)-X(2))*(X(1)-X(3)))*Y(1) + ...
MATLAB (x-X(1))*(x-X(3))/((X(2)-X(1))*(X(2)-X(3)))*Y(2) + ...
CODE (x-X(1))*(x-X(2))/((X(3)-X(1))*(X(3)-X(2)))*Y(3);
P3 = (x-X(2))*(x-X(3))*(x-X(4))/((X(1)-X(2))*(X(1)-X(3))*(X(1)-X(4)))*Y(1) +
...
(x-X(1))*(x-X(3))*(x-X(4))/((X(2)-X(1))*(X(2)-X(3))*(X(2)-X(4)))*Y(2) +
...
(x-X(1))*(x-X(2))*(x-X(4))/((X(3)-X(1))*(X(3)-X(2))*(X(3)-X(4)))*Y(3) +
...
(x-X(1))*(x-X(2))*(x-X(3))/((X(4)-X(1))*(X(4)-X(2))*(X(4)-X(3)))*Y(4);
%%ERROR%%%%
fexp = sin(exp(x)-2);

d1err=abs(P1-fexp);
d2err=abs(P2-fexp);
d3err=abs(P3-fexp);
fprintf('Lagrange interpolating polynomial of f(0.9)\n')
fprintf('Lagrange interpolating polynomial of Degree 1 is= %f\n',P1)
fprintf('And Absolute error for degree 1 = %f\n\n', d1err)
fprintf('Lagrange interpolating polynomial of Degree 2 is= %f\n',P2)
fprintf('And Absolute error for degree 2 = %f\n\n', d2err)
fprintf('Lagrange interpolating polynomial of Degree 3 is= %f\n',P3)
fprintf('And Absolute error for degree 3 = %f\n\n', d3err)

GAUSS FORWARD
clc
clear all
format long

x0=1940;
MATLAB x1=1950;
x2=1960;
CODE
x3=1970;
x4=1980;
x5=1990;
x=1946;
h=x1-x0;
p=(x-x0)/h;
y0=17;
y1=3;
y2=4;
y3=-6;
y4=7;
y5=-9;

y=y0+p*y1+(p-1)*p*y2/2+(p-1)*p*(p+1)*y3/6+(p-2)*(p-
1)*p*(p+1)*y4/24+(p-2)*(p-1)*p*(p+1)*(p+2)*y5/120;

fprintf('The value of y at f(1946) is = %f\n',y)

GAUSS BACKWARD
clc
clear all

x0=1940;
x1=1950;
x2=1960;
x3=1970;
x4=1980;
x5=1990;
x=1976;
h=x1-x0;
p=(x-x3)/h;

fx0=17;
fx1=20;
fx2=27;
fx3=32;
fx4=36;
fx5=38;

f10=fx1-fx0;
f11=fx2-fx1;
MATLAB f12=fx3-fx2;
CODE f13=fx4-fx3;
f14=fx5-fx4;
f20=f11-f10;
f21=f12-f11;
f22=f13-f12;
f23=f14-f13;
f30=f21-f20;
f31=f22-f21;
f32=f23-f22;
f40=f31-f30;
f41=f32-f31;
f50=f41-f40;

y0=32;
y1=5;
y2=-1;
y3=1;
y4=-2;
y5=-9;

y=y0+p*y1+(p+1)*p*y2/2+(p+1)*p*(p-1)*y3/6+(p+2)*(p-
1)*p*(p+1)*y4/24+(p-2)*(p-1)*p*(p+1)*(p+2)*y5/120;
fprintf('The value of y at f(1976) is = %f\n',y)

FIVE POINT
clear all
format long
x=[2.1 2.2 2.3 2.4 2.5 2.6];
fx=[-1.709847 -1.373823 -1.119214 -0.9160143 -0.7470223 -
0.6015966];

h=x(2)-x(1);
for i=1:2
fpx(i)=(-25*fx(i)+48*fx(i+1)-36*fx(i+2)+16*fx(i+3)- 3*fx(i+4))/(12*h);
end
for i=3:4
fpx(i)=(fx(i-2)-8*fx(i-1)+8*fx(i+1)-fx(i+2))/(12*h);
end

for i=5:6
fpx(i)=(-25*fx(i)+48*fx(i-1)-36*fx(i-2)+16*fx(i-3)-3*fx(i- 4))/(-12*h);
end
x=[2.1 2.2 2.3 2.4 2.5 2.6];
fpx=[fpx(1) fpx(2) fpx(3) fpx(4) fpx(5) fpx(6)]; for i=1:6
fprintf('The values of derivatives at x = %f is %f\n', x(i),fpx(i));
i=i+1;
end
%To calculate Absolute Error
fpax=[sec(x(1))^2 sec(x(2))^2 sec(x(3))^2 sec(x(4))^2 sec(x(5))^2 sec(x(6))^2];
for i=1:6
g=fpax-fpx;
fprintf('The absolute error of derivatives at x = %f is
%f\n', x(i),g(i)) i=i+1;
End

Newton open formula:


clc

clear all

format long

a=2.718282;

b=3.718282;

fprintf('\t\tBy Using Open Cotes Formula\t\t')

n=0;

h=(b-a)/(n+2);

xi=a;

x0=xi+h;

x1=x0+h;

x2=x1+h;

x3=x2+h;

fx0=(1/((x0)*log(x0)));

fx1=(1/((x1)*log(x1)));

fx2=(1/((x2)*log(x2)));

fx3=(1/((x3)*log(x3)));

y=((3*h)/2)*[fx0+fx1]
n1=1;

h1=(b-a)/(n1+2);

x1i=a;

x10=x1i+h1;

x11=x10+h1;

x12=x11+h1;

x13=x12+h1;

f1x0=(1/((x10)*log(x10)));

f1x1=(1/((x11)*log(x11)));

f1x2=(1/((x12)*log(x12)));

f1x3=(1/((x13)*log(x13)));

y1=((3*h1)/2)*[f1x0+f1x1]

n2=2;

h2=(b-a)/(n2+2);

x2i=a;

x20=x2i+h2;

x21=x20+h2;

x22=x21+h2;

x23=x22+h2;

f2x0=(1/((x20)*log(x20)));

f2x1=(1/((x21)*log(x21)));

f2x2=(1/((x22)*log(x22)));

f2x3=(1/((x23)*log(x23)));

y2=((4*h2)/3)*[(2*f2x0)-f2x1+(2*f2x2)]

n3=3;

h3=(b-a)/(n3+2);
x3i=a;

x30=x3i+h3;

x31=x30+h3;

x32=x31+h3;

x33=x32+h3;

f3x0=(1/((x30)*log(x30)));

f3x1=(1/((x31)*log(x31)));

f3x2=(1/((x32)*log(x32)));

f3x3=(1/((x33)*log(x33)));

y3=((5*h3)/24)*[(11*f3x0)+f3x1+f3x2+(11*f3x3)]

NEWTON CLOSED n=1


clc
clear all

e=2.718282;
a=e;
b=e+1;

syms x;
n=1;
h=(b-a)/n;
x=[a a+h];
for i=1:length(x)
fx(i)=1/(x(i)*log(x(i)));
end
intf=(h*(fx(1)+fx(2)))/2;
fprintf('The integral of the function within the given range using
newton closed cotes formula for n=1 is %f\n',intf)

NEWTON CLOSED n=2


clc
clear all

e=2.718282;
a=e;
b=e+1;

syms x;
n=2;
h=(b-a)/n;
x=[a a+h a+(2*h)];
for i=1:length(x)
fx(i)=1/(x(i)*log(x(i)));
end
intf=(h*(fx(1)+4*fx(2)+fx(3)))/3;
fprintf('The integral of the function within the given range using
newton closed cotes formula for n=2 is %f\n',intf)

NEWTON CLOSED n=3

clc
clear all

e=2.718282;
a=e;
b=e+1;

syms x;
n=3;
h=(b-a)/n;
x=[a a+h a+(2*h) a+(3*h)];
for i=1:length(x)
fx(i)=1/(x(i)*log(x(i)));
end
intf=(3*h*(fx(1)+3*fx(2)+3*fx(3)+fx(4)))/8;
fprintf('The integral of the function within the given range using
newton closed cotes formula for n=3 is %f\n',intf)

NEWTON CLOSED n=4


clc
clear all

e=2.718282;
a=e;
b=e+1;

syms x;
n=4;
h=(b-a)/n;
x=[a a+h a+(2*h) a+(3*h) a+(4*h)];
for i=1:length(x)
fx(i)=1/(x(i)*log(x(i)));
end
intf=(2*h*(7*fx(1)+32*fx(2)+12*fx(3)+32*fx(4)+7*fx(5)))/45;
fprintf('The integral of the function within the given range using
newton cosed cotes formula for n=4 is %f\n',intf)

EULER
MATLAB clc
CODE clear all
syms y t
t0=1;
y=[0];
yexact(1)=0;
h=0.2;
yp=1+(y/t)+(y/t)^2;
t=1:h:3;
for i=1:length(t)-1
f=1+(y(i)/t(i))+(y(i)/t(i))^2;
y(i+1)=y(i)+h*f;
yexact(i+1)=t(i+1)*tan(log(t(i+1)));
err(i)=abs(y(i)-yexact(i));
fprintf('The approximated solution of the given IVP after i=%d
itterations is y= %f\n',i,y(i));

fprintf('The actual solution of the given IVP after i=%d


itterations is yexact= %f\n',i,yexact(i));

fprintf('The Error between actual and approximated solutions


after err= %f\n\n',err(i));
end
plot(t,y)
hold on
plot(t,yexact)

TAYLOR
clc
clear all
syms y(t)
h = 0.2;
a = 1;
b = 3;
t = a:h:b;
y = sym('y', [1, length(t)]);
y(1) = 0;
MATLAB CODE for i = 1:length(t)
ti = sym('ti');
w = diff(1 + (y(i)/ti) + (y(i)/ti)^2, ti);
w_val = subs(w, ti, t(i));
v(i)=t(i)*tan(log(t(i)));
y(i+1) = (1 + (y(i)/t(i)) + (y(i)/t(i))^2) + h * ((1 +
(y(i)/t(i)) + (y(i)/t(i))^2) + (h/2)*(w_val)); fprintf('The
values of y=%f at t=%f\n', double(y(i)),
t(i));
fprintf('The actual solution is %f\n',v(i))
end

You might also like