Professional Documents
Culture Documents
function raphson
clc;
clear;
s=input('\n ENTER F(x)=','s');
f=inline(s);
s=input('\n ENTER dF(x)=','s');
df=inline(s);
x=input('\n ENTER INITIAL GUESS:');
n=100;
acc=input('\n ENTER ACCURACY:');
fprintf('\n
x
f(x)');
for i=1:1:n
x=x-f(x)/df(x);
fprintf('\n %f
%f',x,f(x));
if(abs(f(x))<acc)
fprintf('\n ROOT=%f',x);
break;
end
end
if(i>=n)
fprintf('\n ROOT WILL NOT CONVERGE WITH GIVEN GUESS');
end
end
OUTPUT
ENTER F(x)=-(h^3)+9*(h^2)-(90/pi)
ENTER dF(x)=-3*(h^2)+18*h
ENTER INITIAL GUESS:2
ENTER ACCURACY:0.000001
x
f(x)
2.026995
0.002167
2.026906
0.000000
ROOT=2.026906>>
OUTPUT
ENTER F(x)=-(h^3)+9*(h^2)-(90/pi)
ENTER dF(x)=-3*(h^2)+18*h
ENTER INITIAL GUESS:2
ENTER ACCURACY:0.000001
x
f(x)
2.026995
0.002167
2.026905
-0.000014
2.026906
0.000000
ROOT=2.026906>>
OUTPUT
ENTER g(x):(sqrt((h^3)+(90/pi)))/3
ENTER f(x):-(h^3)+9*(h^2)-(90/pi)
ENTER INITIAL GUESS:2
ENTER NUMBER OF ITERATIONS TO BE PERFORMED100
ENTER ACCURACY:0.000001
x
g(x)
f(x)
2.000000
2.017917
-0.216932
2.017917
2.023880
-0.073066
2.023880
2.025885
-0.024658
2.025885
2.026561
-0.008327
2.026561
2.026789
-0.002813
2.026789
2.026866
-0.000950
2.026866
2.026892
-0.000321
2.026892
2.026901
-0.000108
2.026901
2.026904
-0.000037
2.026904
2.026905
-0.000012
2.026905
2.026906
-0.000004
2.026906
2.026906
-0.000001
2.026906
2.026906
-0.000000
ROOT OF EQUATION2.026906>>
OUTPUT
ENTER THE FUNCTION:5+0.25*x^2
ENTER NUMBER OF MAJOR STRIPS:10
ENTER INITIAL VALUE:0
ENTER FINAL VALUE:11
Integration=165.916667>>
OUTPUT
ENTER THE FUNCTION:5+0.25*x^2
ENTER NUMBER OF MAJOR STRIPS:9
ENTER INITIAL VALUE:0
ENTER FINAL VALUE:11
Integration=165.916667>>
OUTPUT
ENTER THE FUNCTION:5+0.25*x^2
ENTER NUMBER OF MAJOR STRIPS:11
ENTER INITIAL VALUE:0
ENTER FINAL VALUE:11
Integration=166.375000>>
OUTPUT
ENTER FUNCTION:5+0.25*x^2
ENTER INITIAL LIMIT:0
ETER FINAL LIMIT:11
area =165.916667 >>
OUTPUT
ENTER FUNCTION:5+0.25*x^2
ENTER INITIAL LIMIT:0
ETER FINAL LIMIT:11
area =165.916667 >>
OUTPUT
ENTER NUMBER OF DATA:5
x=1
y=10
x=2
y=26
x=3
y=50
x=4
y=82
x=5
y=122
xx=2.5
y(2.500000)=37.000000>>
OUTPUT
Enter number of data to be entered:5
x:1
y:10
x:2
y:26
x:3
y:50
x:4
y:82
x:5
y:122
ENTER XX:2.5
1.000000
10.000000
16.000000
8.000000
0.000000
0.000000
2.000000
26.000000
24.000000
8.000000
0.000000
0.000000
3.000000
50.000000
32.000000
8.000000
0.000000
0.000000
4.000000
82.000000
40.000000
0.000000
0.000000
0.000000
5.000000
122.000000
0.000000
0.000000
0.000000
0.000000
y(2.500000)=37.000000>>
OUTPUT
enter number of data:5
x1=1
y1=10
x2=2
y2=26
x3=3
y3=50
x4=4
y4=82
x5=5
y5=122
1.000000
10.000000
0.000000
0.000000
0.000000
0.000000
2.000000
26.000000
16.000000
0.000000
0.000000
0.000000
3.000000
50.000000
24.000000
8.000000
0.000000
0.000000
4.000000
82.000000
32.000000
8.000000
0.000000
0.000000
5.000000
122.000000
40.000000
8.000000
0.000000
0.000000
y(2.500000)=37.000000>>
OUTPUT
ENTER No. of points:5
x:0
y:2
x:1
y:5
x:2
y:10
x:3
y:22
x:4
y:34
0.000000
3.000000
2.000000
5.000000
1.000000
5.000000
7.000000
-7.000000
2.000000
12.000000
0.000000
3.000000
12.000000
4.000000
At x=0.000000 dy/dx=6.666667>>
-12.000000
OUTPUT
ENTER No. of points:4
ENTER VALUE OF x:1
ENTER VALUE OF y:1
1.000000
1.000000
2.000000
4.000000
3.000000
3.000000
9.000000
5.000000
2.000000
4.000000
16.000000 7.000000
2.000000
At x=4.000000 dy/dx=8.000000>>
0.000000
%
BACK SUBSTITUTION
function [x]=back(a,c)
n=length(c);
for i=n:-1:1
temp=c(i);
for j=i+1:1:n
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
end
OUTPUT
ENTER x of first point:2
ENTER y of first point:0.69315
ENTER yd of first point:0.5
ENTER x of second point:2.5
ENTER y of second point:0.91629
ENTER yd of second point:.4
a=
8.0000 4.0000 2.0000 1.0000
15.6250 6.2500 2.5000 1.0000
12.0000 4.0000 1.0000
c=
0.6932 0.9163 0.5000 0.4000
y=0.029760x^3+-0.300880x^2+1.346400x+-1.034210
ENTER x for which y is to be determined:2.2
at x=2.200000 y is 0.788495>>
a(k,j)=a(k,j)-temp*a(i,j);
end
c(k)=c(k)-temp*c(i);
end
end
end
%
BACK SUBSTITUTION
function [x]=back(a,c)
n=length(c);
for i=n:-1:1
temp=c(i);
for j=i+1:1:n
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
end
OUTPUT
Enter number of Equations3
a[1][1]= 130
a[1][2]= -30
a[1][3]= 0
c[1][1]= 500
a[2][1]= 1
a[2][2]= -1
a[2][3]= 0
c[2][1]= 0
a[3][1]= 40
a[3][2]= 60
a[3][3]= -120
c[3][1]= -200
a=
130 -30
1 -1
0
0
40 60 -120
c=
500
0
-200
a=
130 -30
40 60 -120
1 -1
c=
500
-200
0
a=
130.0000 -30.0000
0 69.2308 -120.0000
0
0 -1.3333
c=
500.0000
-353.8462
-7.7778
x = 5.0000 5.0000 5.8333>>
OUTPUT
enter initial x value=0
enter initial y value=1
final value of x=.5
increment in x=0.1
enter function:-2*x^3+12*x^2-20*x+8.5+0*y
X
0.100000
1.850000
0.200000
2.511800
0.300000
3.008200
0.400000
3.360800
0.500000
3.590000
>>
OUTPUT
enter initial x value=0
enter initial y value=1
final value of x=0.5
increment in x=0.1
enter function:X-(Y^2)
ENTER REQUIRED ACCURACY0.000001
X
0.000000
1.000000
0.100000
0.913295
0.200000
0.850428
0.300000
0.806726
0.400000
0.778855
0.500000
0.764315
>>
OUTPUT
ENTER INITIAL VALUE OF x=0
ENTER INITIAL VALUE OF y=1
ENTER FINAL VALUE OF x=0.5
ENTER INCREMENT OF x=0.1
ENTER FUNCTION=X-Y^2
k1=-0.100000 k2=-0.071000
AT x=0.100000 y=0.914500
k1=-0.073631 k2=-0.050706
AT x=0.200000 y=0.852331
k1=-0.052647 k2=-0.033950
AT x=0.300000 y=0.809033
k1=-0.035453 k2=-0.019843
AT x=0.400000 y=0.781385
k1=-0.021056 k2=-0.007810
AT x=0.500000 y=0.766952
>>
OUTPUT
ENTER INITIAL VALUE OF x=0
ENTER INITIAL VALUE OF y=1
ENTER FINAL VALUE OF x=1
ENTER INCREMENT OF x=0.5
ENTER FUNCTION=3*X+Y/2
OUTPUT
ENTER F(x)=0*X+0*Y+1*Z
ENTER G(x)=-(4*Y+Z)/4+0*X
ENTER INITIAL VALUE OF x=0
ENTER INITIAL VALUE OF y=1
ENTER INITIAL VALUE OF z=dy/dx :0
ENTER THE VALUE OF x FOR WHICH y IS TO BE DETERMINED=2.5
ENTER STEP SIZE=0.5
VALUE OF y for x=2.500000 is-0.520282>>
OUTPUT
ENTER THE FUNCTION f(x,y):X+Y
ENTER FIRST 4 POINTS
ENTER VALUE OF x=0
ENTER VALUE OF y=1
yp=1.583627
At x=0.400000 y=1.583641>>
OUTPUT
ENTER THE FUNCTION f(x,y):X+Y
ENTER FIRST 4 POINTS
yp=1.583614
At x=0.400000 y=1.583630>>
OUTPUT
ENTER FUNCTION:exp(x+y)
ENTER MAJOR STRIPS in x:2
ENTER MAJOR STRIPS in y:2
ENTER INITIAL LIMITS in x:0
ENTER FINAL LIMITS in x:1
ENTER INITIAL LIMITS in y:0
ENTER FINAL LIMITS in y:1
area=3.000000>>
OUTPUT
ENTER FUNCTION:exp(x+y)
ENTER MAJOR STRIPS in x:2
ENTER MAJOR STRIPS in y:2
ENTER INITIAL LIMITS in x:0
ENTER FINAL LIMITS in x:1
ENTER INITIAL LIMITS in y:0
ENTER FINAL LIMITS in y:1
area=2.923611>>
OUTPUT
number of data=7
x(i)=0
y(i)=0
x(i)=2
y(i)=22
x(i)=4
y(i)=30
x(i)=6
y(i)=27
x(i)=8
y(i)=18
x(i)=10
y(i)=7
x(i)=12
y(i)=0
INTEGRATION=213.333333>>
OUTPUT
NUMBER OF DATA=10
x(1)= 2
y(1)= 166.7
x(2)= 4
y(2)= 300.06
x(3)= 6
y(3)= 416.75
x(4)= 8
y(4)= 483.43
x(5)= 10
y(5)= 533.44
x(6)= 12
y(6)= 333.4
x(7)= 14
y(7)= 183.37
x(8)= 16
y(8)= 83.35
x(9)= 18
y(9)= 33.34
x(10)= 20
y(10)= 0
INTEGRATION=4900.980000>>
OUTPUT
number of data=7
x(i)=1
y(i)=81
x(i)=2
y(i)=75
x(i)=3
y(i)=80
x(i)=4
y(i)=83
x(i)=5
y(i)=78
x(i)=6
y(i)=70
x(i)=7
y(i)=60
integration =456.000000>>
q(i,1)=a(i,i);
for k=i+1:1:n
if(k==i+1)
p(k,1)=a(k,i);
end
if(i+1==k)
r(i,1)=a(i,k);
end
end
end
p(1,1)=0;
r(n,1)=0;
end
%
MAIN
function [a,c]=change(a,c)
n=length(c);
for i=1:1:n-1
for k=i+1:1:n
m=a(k,i)/a(i,i);
a(k,k)=a(k,k)-m*a(i,k);
c(k)=c(k)-m*c(i);
end
end
end
%
BACK SUBSTITUTION
function [x]=back(a,c)
n=length(c);
for i=n:-1:1
temp=c(i);
for j=i+1:1:n
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
end
OUTPUT
Enter number of equations3
a[1][1]=1
a[1][2]=2
a[1][3]=0
c[1]=3
a[2][1]=2
a[2][2]=3
a[2][3]=1
c[2]=4
a[3][1]=0
a[3][2]=2
a[3][3]=-1
c[3]=1
a=
1
2 -1
c=
3
4
1
a=
1
2 -1
c=
3
4
1
p=
0
2
2
q=
1
3
-1
r=
2
1
0
y=
5
-1
-3
ans =
5 -1 -3>>
OUTPUT
INPUT VALUE OF a=2
left boundary condition=0
right boundary condition=0
Enter initial condition=X*(4-X)
Enter length=4
Enter length of time interval=5
Enter h=1
u=
>>
0 -2 -3 -2
0 -3 -4 -3
0 -2 -3 -2
10
11
0.5
1.5
2.5
3.5
4.5
5.5
n=length(c);
for i=1:1:n-1
for k=i+1:1:n
if(abs(a(i,i))<abs(a(k,i)))
for j=1:1:n
temp=a(i,j);
a(i,j)=a(k,j);
a(k,j)=temp;
end
temp=c(i);
c(i)=c(k);
c(k)=temp;
end
end
end
end
function[a,c]=upper(a,c)
n=length(c);
for i=1:1:n-1
for k=i+1:1:n;
temp=a(k,i)/a(i,i);
for j=1:1:n;
a(k,j)=a(k,j)-temp*a(i,j);
end
c(k)=c(k)-temp*c(i);
end
end
end
function[x]=back(a,c,x1)
n=length(c);
for i=n:-1:1
temp=c(i);
for j=i+1:1:n;
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
x=x';
end
OUTPUT
Enter value of y : 118
Enter value of x : 25
Enter value of y : 147
Enter value of x : 40
Enter value of y : 180
Enter value of x : 60
Enter value of y : 230
Enter value of x for which y is to be determined : 5
a=
15986161
300721
6181
300721
6181
151
6181
151
5
c=
1247483
27503
769
constant a = -0.013406
constant b = 3.583565
constant c = 62.149088
at x = 5.000000, y = 79.731758>>
OUTPUT
INPUT VALUE OF a=2
left boundary condition=0
right boundary condition=0
Enter initial condition=x*(4-x)
Enter length=4
Enter length of time interval=5
Enter h=1
Enter value of LAMBDA=0.5
u=
>>
6
0.5
1.5
2.5
3.5
4.5
5.5
OUTPUT
BOTTOM TEMPERATURE=0
TOP TEMPERATURE=0
LEFT TEMPERATURE=0
RIGHT TEMPERATURE=0
ENTER F(X)=-10*(x^2+y^2+10)
length=3
width=3
mesh size=1
t=
0
0 67.5000 75.0000
0 75.0000 82.5000
0
>>
3.5
2.5
1.5
1.5
2.5
3.5
s
while(i<=n-1)
aa(i)=(s(i+1)-s(i))/(6*h(i));
bb(i)=s(i)/2;
cc(i)=(y(i+1)-y(i))/h(i)-(2*h(i)*s(i)+h(i)*s(i+1))/6;
dd(i)=y(i);
i=i+1;
end
fprintf('\n First coefficient of all polynmial:');
aa
fprintf('\n Second coefficient of all polynmial:');
bb
fprintf('\n Third coefficient of all polynmial:');
cc
fprintf('\n Forth coefficient of all polynmial:');
dd
fprintf('\n value of y between first two data:');
yy=aa(1)*((3.5-x(1))^3)+bb(1)*((3.5-x(1))^2)+cc(1)*(3.5-x(1))+dd(1)
fprintf('\n value of y between second two data:');
yy=aa(2)*((5-x(2))^3)+bb(2)*((5-x(2))^2)+cc(2)*(5-x(2))+dd(2)
fprintf('\n value of y between third two data:');
yy=aa(3)*((7.5-x(3))^3)+bb(3)*((7.5-x(3))^2)+cc(3)*(7.5-x(3))+dd(3)
end
OUTPUT
ENTER NUMBER OF DATA:4
ENTER x:3
ENTER y:2.8
ENTER x:4.5
ENTER y:1.2
ENTER x:7
ENTER y:2.5
ENTER x:9
ENTER y:0.5
a matrix of TDMA
a=
0 2.5000
b matrix of TDMA
b=
8
c matrix of TDMA
c=
2.5000
constant matrix of TDMA
f=
9.5200 -9.1200
Second derivatives are
s=
0 1.6499 -1.4716
OUTPUT
no. of data points=5
value of x=2.4
value of y=4.34381
value of x=3.4
value of y=4.60517
value of x=7
value of y=5.22036
value of x=11.1
value of y=5.47646
value of x=19.6
value of y=5.65249
ENTER VALUE of x for which y is to be determined:5
constant a=0.072258
constant b=4.431014
y at x=5.000000 is 4.792304>>
6
5.8
5.6
5.4
5.2
5
4.8
4.6
4.4
4.2
10
12
14
16
18
20
n=length(c);
for i=1:1:n
x(i)=0;
end
err=1;
acc=input('\n ENTER ACCURACY:');
while(abs(err)>acc)
for i=1:1:n
temp=c(i);
for j=1:1:n
if(i~=j)
temp=temp-a(i,j)*x(j);
end
end
err=x(i)-temp/a(i,i);
x(i)=temp/a(i,i);
end
end
end
OUTPUT
Enter number of Equations=3
a[1][1]= 130
a[1][2]= -30
a[1][3]= 0
c[1][1]= 500
a[2][1]= 90
a[2][2]= -90
a[2][3]= 0
c[2][1]= 0
a[3][1]= 40
a[3][2]= 60
a[3][3]= -120
c[3][1]= -200
a=
130 -30
90 -90
40 60 -120
c=
500
0
-200
a=
130 -30
90 -90
40 60 -120
c=
500
0
-200
ENTER ACCURACY:0.000001
x=
5.0000 5.0000 5.8333
>>
for i=1:1:n
temp=c(i);
for j=1:1:n
if(i~=j)
temp=temp-a(i,j)*x(j);
end
end
err=b*(x(i)-temp/a(i,i));
x(i)=b*temp/a(i,i)+(1-b)*x(i);
end
end
end
OUTPUT
Enter number of Equations=3
a[1][1]= 130
a[1][2]= -30
a[1][3]= 0
c[1][1]= 500
a[2][1]= 90
a[2][2]= -90
a[2][3]= 0
c[2][1]= 0
a[3][1]= 40
a[3][2]= 60
a[3][3]= -120
c[3][1]= -200
a=
130 -30
90 -90
40 60 -120
c=
500
0
-200
a=
130 -30
90 -90
40 60 -120
c=
500
0
-200
ENTER ACCURACY:0.000001
ENTER RELAXATATION FACTOR=0.9
x=
5.0000 5.0000 5.8333
>>
OUTPUT
Enter bottom temperature:100
Enter left temperature:30
Enter top temperature:100
Enter length:3
Enter width:3
Enter mesh size:1
Enter number of iterations:100
t=
0 100.0000 100.0000 100.0000
30.0000 72.7778 88.3333 92.2222
30.0000 72.7778 88.3333 92.2222
0 100.0000 100.0000 100.0000
>>
3.5
2.5
1.5
1.5
2.5
3.5
OUTPUT
Enter left temperature:100
Enter right temperature:0
Enter length:3
Enter width:3
Enter mesh size:1
Enter number of iterations:100
t=
100.0000 66.6667 33.3333
>>
3.5
2.5
1.5
1.5
2.5
3.5