0% found this document useful (0 votes)
164 views

Scilab Codes Sem III

This document discusses various numerical methods for solving ordinary differential equations (ODEs), including Euler's method, Runge-Kutta methods, and the midpoint method. It provides code examples to solve ODEs using these techniques and plots the numerical solutions. It also discusses Bessel functions and Legendre polynomials, demonstrating how to compute them manually and using built-in functions. Orthogonality properties of Legendre polynomials are explored through numerical integration.

Uploaded by

Sayan Mondal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

Scilab Codes Sem III

This document discusses various numerical methods for solving ordinary differential equations (ODEs), including Euler's method, Runge-Kutta methods, and the midpoint method. It provides code examples to solve ODEs using these techniques and plots the numerical solutions. It also discusses Bessel functions and Legendre polynomials, demonstrating how to compute them manually and using built-in functions. Orthogonality properties of Legendre polynomials are explored through numerical integration.

Uploaded by

Sayan Mondal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

//Upper triangular matrix & Lower Triangular MAtrix

a=[1 2 3;4 5 6;7 8 9]


disp(a,"The Original Matrix:")
[m,n]=size(a)
for i=1:m-1
for j=i+1:m
if a(i,i)==0 then
t=a(i,:)
a(i,:)=a(j,:)
a(j,:)=t
else
a(j,:)=a(j,:)-((a(j,i)*a(i,:))/a(i,i))
end
end
end
disp(a,"Upper Triangular:")
disp(a',"Lower Triangular") /*Okay! Transpose of an upper_tri square
matrix is lower trianguler!
//GAUSS ELIMINATION TO SOLVE LINEAR SIMULTANEOUS EQUATIONS

// GIVEN EQ~~ -3x +2y-z=-1 6x-6y+7z=-7 3x-4y+4z=-6

//GIVING AUGMENTED MATRIX AS INPUT

A=[1 1 1 4;2 1 3 7;3 4 -2 9]

disp(A,"Augmented Matrix:")

/*transforming ~THE REQUIRED PART OF THE AUGMENTED MATRIX ~


into upper trangular matrix*/

// U p p e r t r i a n g u l a r i z a t i o n
[m,n]=size(A)
for(i=1:m-1)
for(j=i+1:m)
if A(i,i)==0 then
t=A(i,:)
A(i,:)=A(j,:)
A(j,:)=t
else
A(j,:)=A(j,:)-(A(j,i)*A(i,:))/A(i,i); /*No idea whats goin'on here!!
But it works*/
end
end
end
mprintf("Upper triangularized the necessary elements of matrix:")
disp(A)

// back s u b s t i t u t i o n

x (3) =A (3 ,4) / A (3 ,3) ;

disp (x(3),"Z=" )
for (i =2: -1:1)
k =0
for (j = i+1:3)
k = k + A (i , j ) * x ( j )
end
x ( i ) =(1/ A (i , i ) ) *( A (i ,4) -k )
end
//mprintf("X2:")
disp(x(2),"Y=")
disp(x(1),"X=")
//GAUSS SEIDAL METHOD

A=[3 -0.1 -0.2;0.1 7 -0.3;0.3 -0.2 10]


B=[7.85;19.3;74.4]
disp(A,"Coefficient Matrix:")
disp(B,"Solution Matrix:")

[m,n]=size(A)

x=zeros(1,n)
//disp(x)
err=1
while(err>0.0001)
old_x=x
for(k=1:3)
sum=0
for(s=1:n)
if(k==s)then
sum=sum
else
sum=sum+A(k,s)*x(s)
end
x(k)=(B(k)-sum)/A(k,k)
end
end
err=norm(x-old_x)
disp(err,"Error")
disp(x,"X, Y, Z=")
end

disp(x,"X, Y, Z=")
//DE SOLUTION USING ODE

/* dy/dx=-2x-y Initial condition y(0)=-1. Find value at y(0.4)*/

function dy=f(x, y)
dy=-2*x-y
endfunction

x0=0
y0=-1
x=[0:0.001:0.4]

sol=ode(y0,x0,[0:0.001:0.4],f)

/*(y0,x0,xf,function) parameters of 'ode'*/

disp(sol(0.4/0.001),"Answe, at x=0 y=:")


plot(x,sol,3)
// 2nd Order DE using ODE

/* y"+y=0, initial codition y(0)=0 & y'(0)=0; Find y(5) and y'(5).
Ans: y(5)=0.28 & y'(5)=0.95*/

function dx=f(x, y)
dx(1)=y(2)
dx(2)=-y(1)
endfunction
//x=[0:0.1:5]
soln=ode([1;0],0,5,f)
disp(soln(1),"At x=5 value of y=")
disp(soln(2),"At x=5 value of dy/dx=")

/*Another Example
-----------------------------------------------------------------------
//2nd Order DE using ODE

/* 2*(d2y/dt2)-5*(dy/dt)+y=0, initial condition y(3)=6 & y'(3)=-1. Find y(4) and y'(4).

function dx=f(t, x)
dx(1)=x(2)
dx(2)=((5*x(2))-x(1))/2
endfunction
x=[0:0.1:4]
soln=ode([6;-1],3,4,f)
disp(soln(1),"y=")
disp(soln(2),"dy/dt=")

------------------------------------------------------------------------
*/
//Solveing DE using Euler Method

/* dy/dx=-2x-y Initial condition y(0)=-1. Find value at y(0.4)


Ans: y(0.4)=-0.81 */

function dy=f(x, y)
dy=-2*x-y
endfunction

x0=0
y0=-1
xf=0.5
h=0.001
n=(xf-x0)/h
X=[]
Y=[]
X=[X,x0]
Y=[Y,y0]
for (i=1:n)
y0=y0+h*f(x0,y0)
x0=x0+h;
X=[X,x0];
Y=[Y,y0];
end
plot(X,Y,3);
disp(Y(n),"Answe, at x=0 y=") //Value of y at x=0.4

/*
//ANOTHER EXAMPLE
---------------------------------------------------------------------------------------

// dy/dx=3*(e)^-x - 0.4y. Initial cond. y(0)=5. Find y(3).


Ans: y(3)=2.763

function dy=f(x, y)
dy=3*exp(-x)-(0.4*y)
endfunction

//initial condition
x0=0
y0=5
xf=3
h=0.0001
n=(xf-x0)/h

X=[]
Y=[]
X=[X,x0]
Y=[Y,y0]

//Euler loop
for i=0:n
y0=y0+f(x0,y0)*h
x0=x0+h
X=[X,x0]
Y=[Y,y0]
end
plot(X,Y,3)
disp(Y(n))

----------------------------------------------------------------------------
*/
//Euler Method for 2nd Order DE

/* y"+y=0, initial codition y(0)=0 & y'(0)=0; Find y(5) and y'(5).
Ans: y(5)=0.28 & y'(5)=0.95*/

function dy=f1(x, y, z)
dy=z
endfunction
function dz=f2(x, y, z)
dz=-y
endfunction

x0=0
y0=1
z0=0
xf=5
h=0.001
n=(xf-x0)/h

X=[]
Y=[]
Z=[]
X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

for i=0:n
y0=y0+h*f1(x0,y0,z0)
z0=z0+h*f2(x0,y0,z0)
x0=x0+h
X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

end
plot(X,Y,3)
plot(X,Z,"r")
disp(Y(n),"At x=5 value of y=")
disp(Z(n),"At x=5 value of z=")

/* Another example
-------------------------------------------------------------------------------------

/* 2*y"+3y'+5y=11e^(-x). Initial condition y(0)=7 and y'(0)=13. Find y(0.5) and y'(0.5).
And: y(0.5)=9.90
y'(0.5)=-0.45

function dy=f1(x, y, z)
dy=z
endfunction
function dz=f2(x, y, z)
dz=((11*exp(-x))-(3*z)-(5*y))/2
endfunction

x0=0
y0=7
z0=13
xf=0.5
h=0.001
n=(xf-x0)/h
X=[]
Y=[]
Z=[]
X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

for i=0:n
y0=y0+h*f1(x0,y0,z0)
z0=z0+h*f2(x0,y0,z0)
x0=x0+h
X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

end
plot(X,Y,3)
plot(X,Z,"r")
disp(Y(n),"At x=5 value of y=") //Actual value of y=9.90
disp(Z(n),"At x=5 value of z=")

----------------------------------------------------------------*/
//Modified Euler

/* dy/dx=-2x-y. Initial condition y(0)=-1. Find value at y(0.4)


Ans: y(0.4)=-0.81 */

function dy=f(x, y)
dy=-2*x-y
endfunction
x0=0
y0=-1
xf=0.4

h=0.001
n=(xf-x0)/h

X=[]
Y=[]
X=[X,x0]
Y=[Y,y0]
for i=0:n
k1=f(x0,y0)
k2=f(x0+h/2,y0+(h*k1)/2)
y0=y0+h*k2
x0=x0+h
X=[X,x0]
Y=[Y,y0]

end
plot(X,Y,3)
disp(Y(n),"At x=0.4 value of y=")
disp(X(n),"x")
//Runge-Kutta2 for DE

/* dy/dx=-2x-y. Initial condition y(0)=-1. Find value at y(0.4)


Ans: y(0.4)=-0.81 */

function dy=f(x, y)
dy=-2*x-y
endfunction
x0=0
y0=-1
xf=0.4

h=0.001
n=(xf-x0)/h

X=[]
Y=[]
X=[X,x0]
Y=[Y,y0]

for i=0:n
k1=h*f(x0,y0)
k2=h*f((x0+h),(y0+k1))
y0=y0+(k1+k2)/2
x0=x0+h

X=[X,x0]
Y=[Y,y0]

end

plot(X,Y,3)
disp(Y(n),"At x=0.4 value of y=")
disp(X(n),"x")
//Runge-Kutta2 for 2nd Order DE

/* y"+y=0, initial codition y(0)=0 & y'(0)=0; Find y(5) and y'(5).
Ans: y(5)=0.28 & y'(5)=0.95*/

function dy=f1(x, y, z)
dy=z
endfunction
function dz=f2(x, y, z)
dz=-y
endfunction

x0=0
y0=1
z0=0
xf=5

h=0.001
n=(xf-x0)/h
X=[]
Y=[]
Z=[]
X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

for i=0:n
k1=h*f1(x0,y0,z0)
m1=h*f2(x0,y0,z0)
k2=h*f1((x0+h),(y0+k1),(z0+m1))
m2=h*f2((x0+h),(y0+k1),(z0+m1))

y0=y0+(k1+k2)/2
z0=z0+(m1+m2)/2

x0=x0+h

X=[X,x0]
Y=[Y,y0]
Z=[Z,z0]

end

plot(X,Y,3)
plot(X,Z,"r")
disp(X(n),"x=")
disp(Y(n),"At x=0.5 value of y=")
disp(Z(n),"At x=0.5 value of z=")
//Mid-Point Method for DE

/* dy/dx=-2x-y Initial condition y(0)=-1. Find value at y(0.4)


Ans: y(0.4)=-0.81 */

function dy=f(x, y)
dy=-2*x-y
endfunction
x0=0
y0=-1
xf=0.4

h=0.001
n=(xf-x0)/h

X=[]
Y=[]
X=[X,x0]
Y=[Y,y0]

for i=0:n
k1=f(x0,y0)
k2=f((x0+h/2),(y0+(k1*h/2)))
y0=y0+k2*h
x0=x0+h
X=[X,x0]
Y=[Y,y0]

end

plot(X,Y,3)
disp(Y(n),"At x=0.4 value of y=")
disp(X(n),"x")
//BESSEL function

/*using inbuilt function 'besselj'*/

x=[0:0.01:20]'
n=0:5
y=besselj(n,x)
plot2d(x,y,leg="J0@J1@J2@J3@J4@J5");
xlabel("X")
ylabel("Yn(x)")

/*
------------------------------------------------------------

/*Manually computing*/

function y=bes(n)
m=100
y=0
for r=0:m
a=factorial(r)
b=gamma(n+r+1)
y=y+((-1)**r)*(1/(a*b))*(x/2)^(2*r+n)
end
endfunction
for n=0:5
x=0:0.1:20
plot(x,bes(n))
end
//LEGENDRE Function

/*using inbuilt*/

x=[-1:0.01:1]'
n=0:5
y=legendre(n,0,x)'
plot2d(x,y,leg="P0@P1@P2@P3@P4@P5")
xlabel("X")
ylabel("Pn(x)")

/*
---------------------------------------------
// Computing manually

function y=leg(n)
if(modulo(n,2)==0)
m=n/2;
else
m=(n-1)/2
end
y=0;
for(r=0:m)
a=factorial(r)
b=factorial(2*n-2*r)
c=factorial(n-r)
d=factorial(n-2*r)
y=y+((((-1)^r)*b*x^(n-2*r))/(2^n*a*c*d));
end
endfunction

for(n=0:5)
x=[-1:0.01:1];
plot2d(x,leg(n))
end

*/
//Orthogonality of Legendre Polynomial

x=[-1:0.01:1]
function y=p(n)
y=0
m=int(n/2)
for r=0:1:m

a=factorial(r)
b=factorial(2*n-2*r)
c=factorial(n-r)
d=factorial(n-2*r)
y=y+((((-1)^r)*b*x^(n-2*r))/(2^n*a*c*d));

end
endfunction
q=integrate('p(3)*p(2)','x',-1,1)
disp(q)
q1=integrate('p(3)*p(3)','x',-1,-1)
disp(q1)

You might also like