Uploaded by Sanket Wagh

Final NMO

advertisement
Contents
BISECTION ACCURACY .................................................................................................................................. 2
BISECTION ITERATION .................................................................................................................................. 3
NEWTON RAPHSON METOD ITERATION ..................................................................................................... 4
NEWTON RAPHSON ACCURACY................................................................................................................... 5
SUCCESSIVE APPROXIMATION ITERATION .................................................................................................. 6
SUCCESSIVE APPROXIMATION ACCURACY .................................................................................................. 7
GAUSS ELIMINATION.................................................................................................................................... 8
GAUSS SEIDAL METHOD ............................................................................................................................. 10
THOMAS ALGORITHM ................................................................................................................................ 12
TRAPEZOIDAL ............................................................................................................................................. 14
SIMPSONS 1/3RD ......................................................................................................................................... 15
SIMPSON 3/8th ............................................................................................................................................ 16
EULERS METHOD ........................................................................................................................................ 17
RK 4th ORDER ............................................................................................................................................. 18
RK 2 SIMULTANEOUS EQUATION .............................................................................................................. 20
MINIMIZATION ........................................................................................................................................... 21
LAPLACE EQUATION ................................................................................................................................... 22
Straight line ................................................................................................................................................ 23
POWER EQUATION (a*bx) .......................................................................................................................... 25
Power equation (axb) ................................................................................................................................. 27
QUADRATIC EQUATION ............................................................................................................................. 29
EXPONENTIAL ............................................................................................................................................. 31
LAGRANGES INTERPOLATION .................................................................................................................... 33
NEWTON FORWARD................................................................................................................................... 35
INVERSE INTERPOLATION .......................................................................................................................... 37
BISECTION ACCURACY
%program for bisection method by accuracy
f=inline('x*x-8*x+2');
x1=input('\n enter initial guess x1=');
x2=input('\n enter initial guess x2=');
acc=input('\n enter accuracy acc=');
y1=f(x1);
y2=f(x2);
while(y1*y2>0)
x1=input('\n enter initial guess x1=');
x2=input('\n enter initial guess x2=');
y1=f(x1);
y2=f(x2);
end
while(abs(x2-x1)>acc)
x3=(x1+x2)/2;
y3=f(x3);
if(y1*y3<0)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end
end
fprintf('\n the root of new equation=%f',x3);
OUTPUT
>>bisectionaccuracy
enter initial guess x1=0
enter initial guess x2=1
enter accuracy acc=0.01
the root of new equation=0.2578>>
SOLVER
>> x=fzero(inline(‘x*x-8*x+2),0);
>>x
x=
0.2578
BISECTION ITERATION
%program for bisection method by iteration
f=inline(' x*exp(x)-3.1*cos(x)');
x1=input('\n enter initial guess x1=');
x2=input('\n enter initial guess x2=');
n=input('\n enter noof iteration n=');
y1=f(x1);
y2=f(x2);
while(y1*y2>0)
x1=input('\n enter initial guess x1=');
x2=input('\n enter initial guess x2=');
y1=f(x1);
y2=f(x2);
end
for i=1:n
x3=(x1+x2)/2;
y3=f(x3);
if(y1*y3<0)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end
end
fprintf('\n the root of new equation=%f',x3);
OUTPUT
>bisectioniteration
enter initial guess x1=0
enter initial guess x2=1
entern of iteration n=8
the root of new equation=0.8554>>
solver
>> f=inline(‘x*exp(x)-3.1*cos(x)’);
>> x=fzero(f,2.8,3);
>>x
x = 0.8554
NEWTON RAPHSON METOD ITERATION
f=inline('x*x*x-5*x+3');
df=inline('3*x*x-5');
ddf=inline('6*x');
x1=input('\n enter initial guess x1=');
n=input('\n enter no of iteration n=');
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=(y1*y3)/(y2*y2);
while(abs(a)>1)
x1=input('\n enter initial guess x1=');
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=(y1*y3)/(y2*y2);
end
for i=1:n
x2=x1-(y1/y2);
x1=x2;
y1=f(x1);
y2=df(x1);
end
fprintf('\n the root of equation=%f',x2);
OUTPUT
>>newtonraphsoniteration
enter initial guess x1=0
enter no of iteration n=3
the root of equation=0.656619>>
SOLVER
>> f=inline('x*x*x-5*x+3');
>> x=fzero(f,0);
>>x
x=
0.6566
NEWTON RAPHSON ACCURACY
%program for newton raphson method for accuracy
f=inline('exp(x)*cos(x)-1.2');
df=inline('3*x*x+2*cos(x)*sin(x)');
ddf=inline('6*x+2*cos(x)*cos(x)-sin(x)*sin(x)');
x1=input('\n enter initial guess x1=');
acc=input('\n enter accuracy acc=');
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=(y1*y3)/(y2*y2);
while(abs(a)>1)
x1=input('\n enter initial guess x1=')
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=(y1*y3)/(y2*y2);
end
while(abs(x2-x1)>acc)
x2=x1-(y1/y2);
x1=x2;
y1=f(x1);
y2=df(x1);
end
fprintf('\n the new root of equation=%f',x2);
OUTPUT
>>newtonaccuracy
enter initial guess x1=0
enter accuracy acc=0.0001
the new root of equation=0.203087>>
SOLVER
>> f=inline('exp(x)*cos(x)-1.2');
>> x=fzero(f,1);
>>x
x=
0.203087
SUCCESSIVE APPROXIMATION ITERATION
%program or succesive approximation by iteration
g=inline('x-0.3*cos(x)-1');
dg=inline('-0.3*sin(x)');
x1=input('\n enter initial input x1=');
n=input('\n enter no of iteration n=');
a=dg(x1);
while(abs(a)>1)
x1=input('\n initial guess x1=');
a=dg(x1);
end
for i=1:n
x2=g(x1);
x1=x2;
end
fprintf('\n the new root of eqation =%f',x2);
OUTPUT
>>successiveiteration
enter initial input x1=0
enter no of iteration n=5
the new root of eqation =0.54407>>
SOLVER
>> f=inline('x-0.3*cos(x)-1');
>> x=fzero(f,1);
>>x
x=
0.54407
SUCCESSIVE APPROXIMATION ACCURACY
%program or succesive approximation by accuracy
g=inline('2*x-log(x)-7');
dg=inline('-0.3*sin(x)');
x1=input('\n enter initial input x1=');
acc=input('\n enter accuracy acc=');
a=dg(x1);
while(abs(a)>1)
x1=input('\n initial guess x1=');
a=dg(x1);
end
x2=g(x1);
while(abs(x1-x2)>acc)
x2=g(x1);
x1=x2;
end
fprintf('\n the new root of eqation =%d',x2);
OUTPUT
enter initial input x1=4
enter accuracy acc=0.0001
the new root of eqation =3.78928>>
SOLVER
>> f=inline('2*x-log(x)-7');
>> x=fzero(f,1)
x=
3.789278
GAUSS ELIMINATION
% gauss elimination programm
n=input('\ no of elements=');
for i=1:n
for j=1:n
ar(i,j)=input('\ enter no of matrix rowwise=');
end
end
for i=1:n
ar1(i)=input('\ enter constant');
end
for q=1:n
i=q;
d=ar(i,i);
for j=1:n
ar(i,j)=ar(i,j)/d;
end
ar1(i)=ar1(i)/d;
for i=q+1:n
s=ar(i,q);
for j=1:n
ar(i,j)=ar(i,j)-s*ar(q,j);
end
ar1(i)=ar1(i)-s*ar1(q);
end
end
disp(ar);
a(n)=ar1(n);
for w=n-1:-1:1
a(w)=ar1(w);
for e=w:n-1
a(w)=a(w)-a(e+1)*ar(w,e+1);
end
end
for j=1:n
fprintf('\n the value x%d=%f',j,a(j));
end
OUTPUT
>>Gausseliminationmethod
no of elements=3
enter no of matrix rowwise=2
enter no of matrix rowwise=-1
enter no of matrix rowwise=3
enter no of matrix rowwise=-4
enter no of matrix rowwise=-3
enter no of matrix rowwise=-2
enter no of matrix rowwise=3
enter no of matrix rowwise=1
enter no of matrix rowwise=-1
enter constant5
enter constant8
enter constant4
1.0000 -0.5 1.5
0 1.0000 8
0
0 1.0000
the value x1=2.895
the value x2=-0.20
the value x3=0.33>>
SOLVER
>> A=[2 -1 3; -4 -3 -2; 3 1 -1];
>> B=[5; 8; 4];
>> x=A\B
x=
2.895
-0.20
0.33
GAUSS SEIDAL METHOD
n=input('\n Enter Number of Equestion:');
for i=1:n
for j=1:n
ar(i,j)=input('\nEnter matrix elements row_wise :');
end
end
for i=1:n
arl(i)=input('\nEnter second matrix elements :');
end
n1=input('\nEnter number of iterations');
for i=1:n-1
for j=i+1:n
if(abs(ar(i,i))<abs(ar(j,i)))
for k=1:n
temp=ar(i,k);
ar(i,k)=ar(j,k);
ar(j,k)=temp;
temp=ar1(i);
ar1(i)=ar1(j);
ar1(j)=temp;
end
end
end
end
for i=1:n
x(i)=0;
end
for k=1:n1
for i=1:n
const=ar(i);
for j=1:n
if(i~=j)
const=const-(x(j)*ar(i,j));
end
end
x(i)=const/ar(i,i);
fprintf('x%d=%f',i,x(i));
end
fprintf('\n\n');
end
OUTPUT
>>gausssiedal
Enter Number of Equestionn=3
Enter matrix elements row_wise 4
Enter matrix elements row_wise 1
Enter matrix elements row_wise 1
Enter matrix elements row_wise 1
Enter matrix elements row_wise 6
Enter matrix elements row_wise 2
Enter matrix elements row_wise -1
Enter matrix elements row_wise -2
Enter matrix elements row_wise -5
Enter second matrix elements 5
Enter second matrix elements 19
Enter second matrix elements 10
Enter number of iterations n1=6
x1=1.500000x2=0.750000x3=-1.600000
x1=1.712500x2=1.247917x3=-1.841667
x1=1.648437x2=1.339149x3=-1.865347
x1=1.631549x2=1.349857x3=-1.866253
x1=1.629099x2=1.350568x3=-1.866047
x1=1.628870x2=1.350537x3=-1.865989
SOLVER
>> a=[4 1 1; 1 6 2; 1 -2 -5];
>> b=[5; 19; 10];
>> x=a\b
x=
1.0762
4.1333
-3.4381
THOMAS ALGORITHM
% program for tomas algorithm method
n=input('\n enter no of elements n=');
for i=1:n
for j=1:n
ar(i,j)=input('\n enter first matrix rowwise=');
end
end
for i=1:n
ar1=input('\n read constant matrix=');
end
for i=2:n
d=ar(i,i-1)/ar(i-1,i-1);
for j=1:n
ar(i-1,j)=ar(i-1,j)*d;
end
ar1(i-1)=ar1(i-1)*d;
for j=1:n
ar(i,j)=ar(i,j)-ar(i-1,j);
end
ar1(i)=ar1(i)-ar1(i-1);
end
for i=1:n
dig=ar(i,i);
for j=1:n
ar(i,j)=ar(i,j)/dig;
end
ar1(i)=ar1(i)/dig;
end
disp (ar);
a(n)=ar1(n);
for w=n-1:-1:1
a(w)=ar1(w);
for e=w:n-1
a(w)=a(w)-a(e+1)*ar(w,e+1);
end
end
for j=i:n
fprintf('\n the values of variable x%d=%f',j,a(j));
end
OUTPUT
enter
enter
enter
enter
enter
enter
enter
enter
no of
first
first
first
first
first
first
first
elements n=4
matrix rowwise=2.04
matrix rowwise=-1
matrix rowwise=0
matrix rowwise=0
matrix rowwise=-1
matrix rowwise=2.04
matrix rowwise=-1
enter first matrix rowwise=0
enter first matrix rowwise=0
enter first matrix rowwise=-1
enter first matrix rowwise=2.04
enter first matrix rowwise=-1
enter first matrix rowwise=0
enter first matrix rowwise=0
enter first matrix rowwise=-1
enter first matrix rowwise=2.04
read constant matrix=40.8
read constant matrix=0.8
read constant matrix=0.8
read constant matrix=200.8
x1=65.9
x2=93.71
x3=124.49
x4=159.45 >>
solver
>> a=[2.04 -1 0 0;-1 2.04 -1 0;0 -1 2.04 -1;0 0 -1 2.04];
>> b=[40.8;0.8;0.8;200.8];
>> x=a\b
x =
65.9
93.71
124.49
159.45
TRAPEZOIDAL
f=inline('4*x+2');
x0=input('\n initial value of x0=');
xn=input('\n final value of xn=');
n=input('\n no of strips n=');
h=(xn-x0)/n;
ans=0;
for i=1:n-1
ans=ans+f(x0+i*h);
end
ans=2*ans;
ans1=f(x0)+f(xn);
area=ans1+ans;
area=(h/2)*area;
fprintf('\n total area of strips =%f',area);
OUTPUT
initial value of x0=1
final value of xn=4
no of strips n=6
total area of strips =36.000000>>
SOLVER
>> Q=quadl('4*x+2',1,4);
>> q
Undefined function or variable 'q'.
Did you mean:
>> Q
Q=
36
SIMPSONS 1/3RD
% programme forsimpsons 1/3rd rule
f=inline('exp(x)');
x0=input('\n initial value of x0=');
xn=input('\n final value of xn=');
n=input('\n no of strips n=');
while (mod(n,2)~=0)
n=input('\n no of strips again n=');
end
h=(xn-x0)/n;
ans=0;
for i=1:n-1
if (mod(i,2)~=0)
ans=ans+4*(f(x0+i*h));
else
ans=ans+2*f(x0+i*h);
end
end
ans1=f(x0)+f(xn);
area=ans+ans1;
area=area*(h/3);
fprintf('\n total area =%f',area);
OUTPUT
initial value of x0=0
final value of xn=4
no of strips n=4
total area =53.863846>>
SOLVER
>> Q=quadl('exp(x)',0,4);
>> Q
Q =
53.5982
SIMPSON 3/8th
% programme forsimpsons 3/8th rule
f=inline('4*x-1');
x0=input('\n initial value of x0=');
xn=input('\n final value of xn=');
n=input('\n no of strips n=');
while (mod(n,3)~=0)
n=input('\n no of strips again n=');
end
h=(xn-x0)/n;
ans=0;
for i=1:n-1
if (mod(i,3)~=0)
ans=ans+3*(f(x0+i*h));
else
ans=ans+2*f(x0+i*h);
end
end
ans1=f(x0)+f(xn);
area=ans+ans1;
area=area*(3*h/8);
fprintf('\n total area =%f',area);
OUTPUT
initial value of x0=1
final value of xn=4
no of strips n=6
total area =21.937500>> sampson38
initial value of x0=1
final value of xn=4
no of strips n=6
total area =27.000000>>
SOLVER
>> Q=quadl('4*x-1',1,4);
>> Q
Q=
27.0000
EULERS METHOD
% flowchart for eulers method
f=inline('y*x*x-1.1*y');
x0=input('\n enter initial value of x0=');
y0=input('\n enter initial value of y0=');
xg=input('\n enter final value of xg=');
h=input('\n enter no of step size h=');
n=(xg-x0)/h;
for i=1:n
yg=y0+h*f(x0,y0);
x0=x0+h;
yg=y0;
end
fprintf('\n the final value of yg=%f',yg);
OUTPUT
enter initial value of x0=0
enter initial value of y0=1
enter final value of xg=2
enter no of step size h=0.5
the final value of yg=1.000000>>
SOLVER
>> f=inline('y*x*x-1.1*y');
>> x0=0;
>> xn=2;
>> y0=1;
>> h=0.5;
>> xspan=[x0,h,xn];
>> [xe,ye]=ode45(f,xspan,y0);
>> [xe,ye]
ans =
0 1.0000
0.5000 0.6015
2.0000 1.5947
RK 4th ORDER
% flow chart for runge kutta 4 order method
f=inline('sqrt(x*x+y)');
x0=input('\n enter initial value of xo=');
y0=input('\n enter initial value of y0=');
xg=input('\n enter final value of xg=');
h=input('\n enter the step size=');
n=(xg-x0)/h;
for i=1:n
k1=h*f(x0,y0);
k2=h*f(x0+h/2,y0+k1/2);
k3=h*f(x0+h/2,y0+k2/2);
k4=h*f(x0+h,y0+k3);
k=(k1+2*k2+2*k3+k4)/6;
yg=y0+k;
x0=x0+h;
y0=yg;
end
fprintf('\n the final value of yg=%f',yg);
OUTPUT
>> rk4order
enter initial value of xo=0
enter initial value of y0=1
enter final value of xg=0.4
enter the step size=0.2
the final value of yg=1.449537>>
SOLVER
>> f=inline('sqrt(x*x+y)');
>> x0=0;
>> xn=0.4;
>> y0=1;
>> h=0.2;
>> xspan=[x0,h,xn];
>> [xe,ye]=ode45(f,xspan,y0);
>> [xe,ye]
ans =
0 1.0000
0.2000 1.2113
0.4000 1.4495
RK 2 SIMULTANEOUS EQUATION
% flow chart for runge kutta 4 order method
f=inline('0*x+0*y+z');
g=inline('(9+3*x*z-9*y)/2');
x0=input('\n enter initial value of xo=');
y0=input('\n enter initial value of y0=');
z0=input('\n enter value of z0=');
xg=input('\n enter final value of xg=');
h=input('\n enter the step size=');
n=(xg-x0)/h;
for i=1:n
k1=h*f(x0,y0,z0);
l1=h*g(x0,y0,z0);
k2=h*f(x0+h,y0+k1,z0+l1);
l2=h*g(x0+h,y0+k1,z0+l1);
k=(k1+k2)/2;
l=(l1+l2)/2;
yg=y0+k;
zg=z0+l;
y0=yg;
z0=zg
end
fprintf('\n the final value of yg=%f',yg);
fprintf('\n the final value of zg=%f',zg);
OUTPUT
enter initial value of xo=0
enter initial value of y0=1
enter value of z0=-2
enter final value of xg=0.1
enter the step size=0.1
z0 =
-1.9700
the final value of yg=0.800000
the final value of zg=-1.970000>>
MINIMIZATION
>> f=[2 3];
>> a=[-2 -4; -4 -2];
>> b=[-80;-100];
>>lb=[0,0];
>> [X,val]=linprog(f,a,b,[],[],lb)
Optimization terminated.
X=
20.0000
10.0000
val =
70.0000
LAPLACE EQUATION
% profram for laplace eq
tu=input('\n enter upward boundary value tu=');
td=input('\n enter downward boundary value td=');
tl=input('\n enter leftside boundary value tl=');
tr=input('\n enter rightside boundary value tr=');
a=[4 -1 -1 0; -1 4 0 -1; -1 0 4 -1; 0 -1 -1 4];
b(1,1)=tl+tu;
b(2,1)=tr+tu;
b(3,1)=tl+td;
b(4,1)=tr+td;
v=linsolve(a,b);
fprintf('\n result=\n');
for i=1:4
fprintf('\n temp %d:%0.4f',i,v(i));
end
OUTPUT
enter upward boundary value tu=2000
enter downward boundary value td=500
enter leftside boundary value tl=1000
enter rightside boundary value tr=0
result=
temp 1:1187.5000
temp 2:937.5000
temp 3:812.5000
temp 4:562.5000>>
Straight line
% program for straight line equation
n=input('\n no of functions n=');
for i=1:n
x(i)=input('\n take functions of x(i)=');
y(i)=input('\n take functions of y(i)=');
end
s0=0;
s1=0;
s2=0;
s3=0;
for i=1:n
s0=s0+x(i);
s1=s1+y(i);
s2=s2+x(i)*y(i);
s3=s3+x(i)*x(i);
end
d=s0*s0-n*s3;
da=s1*s0-n*s2;
db=s0*s2-s1*s3;
a=da/d;
b=db/d;
fprintf('\n print value of a=%f',a);
fprintf('\n print value of b=%f',b);
fprintf('\n print straiht line equation=%fx+%f',a,b);
OUTPUT
> straightline
no of functions n=4
take functions of x(i)=0
take functions of y(i)=1.80
take functions of x(i)=5
take functions of y(i)=1.45
take functions of x(i)=10
take functions of y(i)=1.18
take functions of x(i)=15
take functions of y(i)=1
print value of a=-0.053400
print value of b=1.758000
print straiht line equation=-0.053400x+1.758000>>
solver
>> x=[0 5 10 15];
>> y=[1.80 1.45 1.18 1];
>> z=polyfit(x,y,1);
>> a=z(1);
>> b=z(2);
>> a
a=
-0.0534
>> b
b=
1.7580
POWER EQUATION (a*bx)
% program for straight line equation
n=input('\n no of functions n=');
for i=1:n
x(i)=input('\n take functions of x(i)=');
y(i)=input('\n take functions of y(i)=');
Y(i)=log(y(i));
X(i)=log(x(i));
end
s0=0;
s1=0;
s2=0;
s3=0;
for i=1:n
s0=s0+X(i);
s1=s1+Y(i);
s2=s2+X(i)*Y(i);
s3=s3+X(i)*X(i);
end
d=s0*s0-n*s3;
da=s1*s0-n*s2;
db=s0*s2-s1*s3;
A=da/d;
B=db/d;
fprintf('\n print value of A=%f',A);
fprintf('\n print value of B=%f',B);
a=exp(A);
b=B;
fprintf('\n print straiht line equation=%f*%f^x',a,b);
OUTPUT
no of functions n=7
enter value of x=0
enter value of y=32
enter value of x=1
enter value of y=47
enter value of x=2
enter value of y=65
enter value of x=3
enter value of y=92
enter value of x=4
enter value of y=132
enter value of x=5
enter value of y=190
enter value of x=6
enter value of y=275
print value of A=0.089099
print value of B=5.094962
print straiht line equation=1.227719*124440.513758^x>>
SOLVER
>> x=[0 1 2 3 4 5 6];
>> y=[32 47 65 92 132 190 275];
>> z=polyfit(x,y,1);
>> a=z(1);
>> b=z(2);
>> a
a=
38.6429
>> s
Undefined function or variable 's'.
>> b
b=
3.0714
Power equation (axb)
% program for straight line equation
n=input('\n no of functions n=');
for i=1:n
x(i)=input('\n take functions of x(i)=');
y(i)=input('\n take functions of y(i)=');
Y(i)=log(y(i));
X(i)=log(x(i));
end
s0=0;
s1=0;
s2=0;
s3=0;
for i=1:n
s0=s0+X(i);
s1=s1+Y(i);
s2=s2+X(i)*Y(i);
s3=s3+X(i)*X(i);
end
d=s0*s0-n*s3;
da=s1*s0-n*s2;
db=s0*s2-s1*s3;
A=da/d;
B=db/d;
fprintf('\n print value of A=%f',A);
fprintf('\n print value of B=%f',B);
a=exp(B);
b=A;
fprintf('\n print straiht line equation=%f*x^%f',a,b);
OUTPUT
no of functions n=4
take functions of x(i)=900
take functions of y(i)=89
take functions of x(i)=1500
take functions of y(i)=110
take functions of x(i)=2700
take functions of y(i)=120
take functions of x(i)=3000
take functions of y(i)=125
print value of A=0.263566
print value of B=2.722970
print straiht line equation=15.225467*x^0.263566>> power2
SOLVER
>> x=[900 1500 2700 3000];
>> y=[89 110 120 125];
>> z=polyfit(x,y,2);
>> a=z(1);
>> b=z(2);
>> a
a=
-9.6831e-06
>> b
b=
0.0534
QUADRATIC EQUATION
%program for qudratic equation
n=input ('\n enter the no of elements=');
for i=1:n
x(i)=input('\n enter the values of x=');
y(i)=input('\n enter the values of y=');
end
S0=0;
S1=0;
S2=0;
S3=0;
S4=0;
S5=0;
S6=0;
for i=1:n
S0=S0+x(i);
S1=S1+y(i);
S2=S2+x(i)*y(i);
S3=S3+x(i)*x(i);
S4=S4+x(i)*x(i)*y(i);
S5=S5+x(i)*x(i)*x(i);
S6=S6+x(i)*x(i)*x(i)*x(i);
end
a=[S3 S0 n; S5 S3 S0; S6 S5 S3;];
b=[S1;S2;S4;];
v=linsolve(a,b);
fprintf('a=%f b=%f c=%f',v(1),v(2),v(3));
fprintf('\n\t y=%f^2+%f*x+%f',v(1),v(2),v(3));
OUTPUT
>> qudraticequation
enter the no of elements=5
enter the values of x=0.1
enter the values of y=1.4
enter the values of x=0.2
enter the values of y=1.56
enter the values of x=0.3
enter the values of y=1.76
enter the values of x=0.4
enter the values of y=2
enter the values of x=0.5
enter the values of y=2.28
a=2.000000 b=1.000000 c=1.280000
y=2.000000^2+1.000000*x+1.280000>>
SOLVER
>> x=[10 12 15 23 20 80];
>> y=[14 17 23 25 21 100];
>> z=polyfit(x,y,2);
>> a=z(1);
>> b=z(2);
>> c=z(3);
>> a
a=
0.0084
>> b
b=
0.4485
>> c
c=
10.4871
EXPONENTIAL
%program for power equation (y=a*e^(bx))
n=input ('\n enter the no of elements=');
for i=1:n
x(i)=input('\n enter the values of x=');
temp=input('\n enter the values of y=');
y(i)=log(temp);
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+y(i);
S2=S2+x(i)*y(i);
S3=S3+x(i)*x(i);
end
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a1=da/d;
b1=db/d;
b=(a1);
a=exp(b1);
fprintf('\n y=(%f)e^(%f)x',a,b);
OUTPUT
>> powerequationforyaebx
enter the no of elements=4
enter the values of x=2
enter the values of y=25
enter the values of x=4
enter the values of y=38
enter the values of x=6
enter the values of y=56
enter the values of x=8
enter the values of y=84
y=(16.814818)e^(0.201179)x>>
SOLVER
>> x=[10 12 15 23 20 80];
>> y=[14 17 23 25 21 100];
>> z=polyfit(x,y,2);
>> a=z(1);
>> b=z(2);
>> c=z(3);
>> a
a=
0.0084
>> b
b=
0.4485
>> c
c=
10.4871
LAGRANGES INTERPOLATION
% program for lagranges interpolation
n=input('enter the values of n=');
for i = 1:n
x(i)=input('enter the values of x=');
y(i)=input('enter the values of y=');
end
xg=input('enter the values of xg=');
yg=0;
for j = 1:n
nu=1;
de=1;
for i =1:n
if(i~=j)
nu= nu*(xg-x(i));
de=de*(x(j)-x(i));
end
end
l(j)=nu/de;
yg=yg+l(j)*y(j);
fprintf('\n value of yg=%f',yg);
end
OUTPUT
>> langranges
enter the values of n=5
enter the values of x=5
enter the values of y=150
enter the values of x=7
enter the values of y=392
enter the values of x=11
enter the values of y=1452
enter the values of x=13
enter the values of y=2366
enter the values of x=17
enter the values of y=5202
enter the values of xg=9
value of yg=-16.666667
value of yg=192.400000
value of yg=1483.066667
value of yg=694.400000
value of yg=810.000000>>
SOLVER
>> x=[5 7 11 13 17];
>> y=[150 392 1452 2366 5202];
>> yg=interp1(x,y,9)
yg =
922
NEWTON FORWARD
n=input('\n enter no of elements n=');
for i=1:n
x(i)=input('\n enter value of x=');
y(i,1)=input('\n enter of value y=');
end
xg=input('\n enter value of xg=');
h=x(2)-x(1);
c=n-1;
for j=2:n
c=c-1;
for i=1:c+1
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
c=n;
for i=1:n-1
fprintf('\n\n');
c=c-1;
for j=2:c+1
fprintf('\nt %f',y(i,j));
end
end
u=(xg-x(1))/h;
ans=y(1,1)+u*y(1,2);
yg=0;
m=2;
u1=u;
for k=3:n
if (m<k)
u=u*(u1-(m-1));
end
fact=1;
for i=1:k-1
fact=fact*i;
end
yg=yg+((u/fact)*y(1,k));
m=m+1;
end
yg=ans+yg;
fprintf('\n final value of yg=%f',yg);
OUTPUT
>> newtonsforward
enter no of elements n=5
enter value of x=0
enter of value y=5
enter value of x=2
enter of value y=29
enter value of x=4
enter of value y=125
enter value of x=6
enter of value y=341
enter value of x=8
enter of value y=725
enter value of xg=1.5
t 24.000000
t 72.000000
t 48.000000
t 0.000000
t 96.000000
t 120.000000
t 48.000000
t 216.000000
t 168.000000
t 384.000000
final value of yg=18.125000>>
SOLVER
final value of yg=18.125000>>
>>
>> x=[0 2 4 6 8];
>> y=[5 29 125 341 725];
>> yg=interp1(x,y,1.5)
yg =
23
INVERSE INTERPOLATION
% program for inverse lagranges interpolation
n=input('enter the values of n=');
for i = 1:n
x(i)=input('enter the values of x=');
y(i)=input('enter the values of y=');
end
yg=input('enter the values of yg=');
xg=0;
for j = 1:n
nu=1;
de=1;
for i =1:n
if(i~=j)
nu= nu*(yg-y(i));
de=de*(y(j)-y(i));
end
end
l(j)=nu/de;
xg=xg+l(j)*x(j);
fprintf('\n value of xg=%f',xg);
end
OUTPUT
>> inverselangranges
enter the values of n=4
enter the values of x=20
enter the values of y=0.342
enter the values of x=25
enter the values of y=0.423
enter the values of x=30
enter the values of y=0.5
enter the values of x=35
enter the values of y=0.65
enter the values of yg=0.390
value of xg=4.788695
value of xg=29.029424
value of xg=22.259080
value of xg=22.840578>>
SOLVER
>> y=[0.342 0.423 0.5 0.65];
>> x=[20 25 30 35];
>> xg=interp1(y,x,0.396)
xg =23.3333
Download