0% found this document useful (0 votes)
658 views36 pages

Mathematics SciLab

This document contains code written in Scilab to solve various problems involving partial differential equations. The problems include: 1. Solving the 1D wave equation for a string with initial displacement given by a function and boundary conditions of zero displacement. 2. Solving the 1D heat equation for a rod with an initial temperature distribution given by a function and boundary conditions of zero temperature. 3. Code is provided to calculate Fourier series coefficients and plot the Fourier series approximation of a given function over an interval. Specific values are used for constants in the problems and code evaluates the solutions at sample points to numerically solve the PDEs.

Uploaded by

Mihir Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
658 views36 pages

Mathematics SciLab

This document contains code written in Scilab to solve various problems involving partial differential equations. The problems include: 1. Solving the 1D wave equation for a string with initial displacement given by a function and boundary conditions of zero displacement. 2. Solving the 1D heat equation for a rod with an initial temperature distribution given by a function and boundary conditions of zero temperature. 3. Code is provided to calculate Fourier series coefficients and plot the Fourier series approximation of a given function over an interval. Specific values are used for constants in the problems and code evaluates the solutions at sample points to numerically solve the PDEs.

Uploaded by

Mihir Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Mathematics Lab

Sumukh Santhosha Kesarla


Enrolment Number: 200031101611057

Date:

clc
clear
A = [4 5 8 6]
disp(A)

B = [4;7;2;6]
disp(B)

C = [1 2 3 ; 2 4 6 ; 3 6 9 ]
disp(C)

D = [4 5 8; 2 3 6; 9 5 7]
disp(D)

disp(C(:,3))

disp(det(C))

disp(inv(C))

disp(rank(C))

disp(trace(C))

E=C+D
disp(E)

F=C*D
disp(F)

disp(6*C)

—-------------------------------------------------
clc

clear
v1 = [2 2 1]
v2 = [1 -1 1]
v3 = [1 0 1]
v = [v1; v2; v3]
disp(v)

r = rank(v)

if r == 3 then
disp "it is Linearly Inependent"
else
disp "It is Lineraly dependent"
end
—--------------------------------------------------
clc
clear
v1 = [1 2 3 1]
v2 = [2 1 -1 1]
v3 = [4 5 5 3]
v4 = [5 4 1 3]

v = [v1; v2; v3; v4]


disp(v)

[row c] = size(v)

r = rank(v)

if r == row then
disp "it is Linearly Inependent"
else
disp "It is Lineraly dependent"
end
—------------------------------------------------------
clc
clear

v1 = [2 -3 1]
v2 = [1 -1 2]
v3 = [2 1 -3]

v=[v1; v2; v3]


disp(v)
c = [-2;3;-2]
disp(c)
X = inv(v) * c
Y = v*X - c

if Y == zeros(Y) then
disp(X)
else
disp("System is inconsistent")
end
—---------------------------------------
Date: 05/04/2022

1. By using SCILAB to determine whether or not the vectors


(1, − 2, 1), (2, 1, − 1), (7, − 4, 1)are linearly dependent and solve the following
system of linear equations:
2x-3y+z=-2, x-y+2z=3, 2x+y-3z=-2.
clc
clear
v1 = [1 -2 1]
v2 = [2 1 -1]
v3 = [7 -4 1]

v = [v1; v2; v3]

ra = rank(v)
[row c] = size(v)

if ra == row then
disp("Vectors are Linearly Independent")
else
disp("Vectors are Linearly Dependent")
end

clc
clear
a1 = [2 -3 1]
a2 = [1 -1 2]
a3 = [2 1 -3]
a = [a1; a2; a3]

b = [-2; 3; -2]

x = inv(a) * b

disp(x)

2. Write the given system of equation in matrix form.


2x1+2x2-5x3=1, -2x1+4x2+3x3=6, -x1+3x2+2x3=5 and solve it for finding the values of
xi’s.
clc
clear

a1 = [2 2 -5]

a2 = [-2 4 3]

a3 = [-1 3 2]

a = [a1; a2; a3]

b = [1; 6; 5]

x = inv(a) * b

disp(x)

3. For a given matrix A= , find (a)Inverse(A) (b)Det.(A) (c)Trace (d) rank(A)


clc
clear

a = [1 2 3; 4 5 6; 2 4 1]
disp("inverse of A is: " )
disp(inv(a))
disp("Determinant of A is: " )
disp(det(a))
disp("Trace of A is: " )
disp(trace(a))
disp("Rank of A is: " )
disp(rank(a))
4. Write the given system of equation in matrix form.
2x1+2x2-5x3=1, -2x1+4x2+3x3=6, -x1+3x2+2x3=5 and solve it for finding the values

ofxi’s.
clc

clear

a1 = [2 2 -5]

a2 = [-2 4 3]

a3 = [-1 3 2]

a = [a1; a2; a3]

b = [1; 6; 5]

x = inv(a) * b

disp(x)
5. Given two Matrices as
A=[1 2 3; 4 5 6; 2 4 1], B=[2 3 4; 6 7 8; 9 7 4]
Write (a)inverse (b)rank (c) trace and determinant of a matrices.
clc
clear
a=[1 2 3; 4 5 6; 2 4 1]
b=[2 3 4; 6 7 8; 9 7 4]
disp("inverse of A is: " )
disp(inv(a))
disp("Determinant of A is: " )
disp(det(a))
disp("Trace of A is: " )
disp(trace(a))
disp("Rank of A is: " )
disp(rank(a))

disp("inverse of B is: ")


disp(inv(b))
disp("Determinant of B is: ")
disp(det(b))
disp("Trace of B is: ")
disp(trace(b))
disp("Rank of B is: ")
disp(rank(b))
6. Solve the system of equation with the help of SciLab, finding the values of x,y,z-
2𝑥 + 3𝑦 + 4𝑧 = 11, 𝑥 + 5𝑦 + 7𝑧 = 15, 3𝑥 + 11𝑦 + 13𝑧 = 25.
clc
clear
a1 = [2 3 4]
a2 = [1 5 7]
a3 = [3 11 13]
a = [a1; a2; a3]

b = [11; 15; 25]

x = inv(a) * b

disp(x)
7. Check for Linear independency/ dependency for the given set of vectors using SciLab-
[1,2,3] [3,4,4] [7,10,12]
clc

clear

a1 = [1,2,3]

a2 = [3,4,4]

a3 = [7,10,12]

a = [a1; a2; a3]

ra = rank(a)

[row c] = size(a)

if ra == row then

disp("Vectors are Linearly Independent")

else

disp("Vectors are Linearly Dependent")

End

8. Find trace, determinant and rank of matrix A=[1, 2, 3; 2, 0,-1; 0, 0, 3].


clc
clear
A = [1, 2, 3; 2, 0,-1; 0, 0, 3]
disp("Trace of B is: ")
disp(trace(A))
disp("Determinant of B is: ")
disp(det(A))
disp("Rank of B is: ")
disp(rank(A))
9. Solve the following system of linear equations:

(i)2x-3y+z=-2, x-y+2z=3, 2x+y-3z=-2


clc

clear

a1 = [2 -3 1]

a2 = [1 -1 2]

a3 = [2 -1 3]

a = [a1; a2; a3]

b = [-2; 3; -2]

x = inv(a) * b

disp(x)

(ii)X+4y+7z=1, 2x+5y+8z=2, x+2y+3z=1


clc

clear
a1 = [1 4 7]

a2 = [2 5 8]

a3 = [1 2 3]

a = [a1; a2; a3]

b = [1; 2; 1]

x = inv(a) * b

disp(x)

(iii)x-4y+7z=8, 3x+8y-2z=6, 7x-8y+26z=3


clc

clear

a1 = [1 -4 7]

a2 = [3 8 -2]

a3 = [7 -8 26]

a = [a1; a2; a3]

b = [8; 6; 3]

x = inv(a) * b

disp(x)
0.
Date: 12/04/2022

1. To solve: (dy/dx)=x+y, y(0)=1, when x=[0,1]

clc
clf
clear

function [ydot]=myfuntion(x, y)
ydot = x+y
endfunction
x1 = 0
y1 = 1
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)

2. Write a scilab code to find solution of the first order initial value problem at x=0.8:
3. Solve and Plot the solution: (dx/dt)+(tant)x=cost, y(0)=0 ; in interval [0,1]

clc
clf
clear

function [tdot]=myfuntion(x, t)
tdot = cos(t) - x*tan(t)
endfunction
x1 = 0
y1 = 0
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)

4.

clc
clf
clear
function [ydot]=myfuntion(x, y)
ydot = -2*x - y
endfunction
x1 = 0
y1 = -1
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)

5.

clc
clf
clear
function [ydot]=myfuntion(x, y)
ydot = 2*x*y
endfunction
x1 = 0
y1 = 1.8
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)

6.

clc
clf
clear
function [ydot]=myfuntion(x, y)
ydot = 4*x
endfunction
x1 = 0
y1 = 3
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)

7.

clc
clf
clear

function [ydot]=myfuntion(x, y)
ydot = -4*x
endfunction
x1 = 0
y1 = 3
x = linspace(0,1,100)
y = ode(y1, x1, x, myfuntion)
disp(x,y)
plot(x,y)
Date: 26/04/2022

clear
clc
clf

//solution of y^2-5y^1+y=0
function dx=f(t, x)
dx(1)=x(2)
dx(2)=(-1/2)*x(1)+5/2*x(2)
endfunction
sol=ode([6;-1],3,4,f)
//disp(sol(1))
t=4:.5:10;
sol=ode([6;-1],3,t,f)
plot(t,sol(1,:))

To be corrected:
clc
clear
clf

function zdash=f(c, z)
zdash = c*z
endfunction

c = [0 0 1; exp(x) -6 -5]
z = [1; 0; 1]

z0 = [0 1]
x = linspace(0,1,100)
y = ode( [0;1],0, x,f)
disp(x,y)

plot(x,y)
Date: 24/05/2022

Code for Fourier series

clc
clear
clf
deff('a=f(x)','a=x*x')

function [a0, A, B]=myfourier(l, n, f)


a0 = (1/l)*integrate('f(x)','x',-l,l)
for i=1:n
function an=f1(x, f)
an = f(x)*cos(i*%pi*x/l)
endfunction

function bn=f2(x, f)
bn = f(x)*sin(i*%pi*x/l)
endfunction

A(i) = (1/l)*integrate('f1(x)','x',-l,l)
B(i) = (1/l)*integrate('f2(x)','x',-l,l)

end

x = -5*l:0.1:5*l
series = a0/2;

for i=1:n
series = series + A(i)*cos(i*%pi*x/l) + B(i)*sin(i*%pi*x/l)
end

plot(x,series)
endfunction

myfourier(2,6,f)
Write the SciLab code for the solution of one dimensional wave equation subjected to
u(0,t)=u(L,t)=0, with initial velocity zero, if the initial conditions for displacement is given by
f(x)= sin3x . Use the values of c=1, L=1. Also find the displacement in the string when x=0.5
and t=0.075

clc
clear
clf

l = input("Enter length of rod: ")


k = input("Enter value of coefficient k: ")
n = input("Enter number of terms required in solution: ")

function w=f(x)
w = sin(3*%pi*x)
endfunction

for i=1:n
function w1=f1(x)
w1 = f(x)*sin(i*%pi*x/l)
endfunction
b(i) = (2/l)*integrate('f1(x)','x',0,l)
end

function u=f2(x, t)
u=0
for i=1:n
u = u + b(i)*sin(i*%pi*x/l)*cos(i*%pi*k*t/l)
end
endfunction

x = 0:0.05:1
t = 0:0.025:0.25

f1 = feval(x,t,f2)
plot3d(x,t,f1,'x@t@f2(x,t)')

scf(1)
plot(x',f1(:,1),'m.*',x',f1(:,3),'g.-',x',f1(:,6),'b.+',x',f1(:,9),'r.x')
Date: 31/05/2022

Write the SciLab code for the solution of one dimensional heat equation
subjected to u(0,t)=u(L,t)=0, if the initial conditions are given by u(x,0)=f(x)=4*(x/L)*(1-
x/L). Use the values of k=1, L=1. Also find the temperature in rod at x=0.4,t=0.2

clc
clear
clf

l = input("Enter length of rod: ")


k = input("Enter value of coefficient k: ")
n = input("Enter number of terms required in solution: ")

function w=f(x)
w = 4*x/l*(1-x/l)
endfunction

for i=1:n
function w1=f1(x)
w1 = f(x)*sin(i*%pi*x/l)
endfunction
b(i) = (2/l)*integrate('f1(x)','x',0,l)
end

function u=f2(x, t)
u=0
for i=1:n
u = u + b(i)*sin(i*%pi*x/l)*exp(-i^2*%pi^2*k*t/l^2)
end
endfunction

x = 0:0.05:1
t = 0:0.025:0.25

f1 = feval(x,t,f2)
plot3d(x,t,f1,'x@t@f2(x,t)')

scf(1)
plot(x',f1(:,1),'m.*',x',f1(:,3),'g.-',x',f1(:,6),'b.+',x',f1(:,9),'r.x')
Write the SciLab code for the solution of Two dimensional Laplace equation subjected to
u(x,0)=0, u(x,H)= 100*x*(L-x)^3, u(0,y)=u(L,y)=0. Use the values of H=1, L=2.

clc
clear
clf

function w=f(x)
w = 100*x*(l-x)^3
endfunction

function w1=f1(x)
w1 = f(x)*sin(n*%pi*x/l)
endfunction

function a=a1(n)
a = 2*intg(0,l,f1,0.0001)/(l*sinh(n*%pi*h/l))
endfunction

l=2
h=1
a = []

for n=1:20
a = [a a1(n)]
end

function u=u1(x, y)
u=0
for j=1:20
u = u + a(j)*sin(j*%pi*x/l)*sinh(j*%pi*y/l)
end
endfunction

x = 0:l/40:l;
y = 0:h/20:h;

u2 = feval(x,y,u1)

plot3d(x,y,u2,25,5,'x@t@u(x,t)')

scf(1)
contour(x,y,u2,15)
xtitle('Contour plots for Laplace equation','x','y')
To verify Rank theorem, rank+ nullity=No. of columns in the matrix, for an m X n matrix A.

clc
clear

A = [4 5 9 -2; 6 5 1 12; 3 4 8 -3]

r = rank(A)
disp(r)

k = kernel(A)
nullity = size(k,2)
disp(nullity)

n = size(A,2)
disp(n)

if (r + nullity) == n then
disp("Rank Nullity Theorem Verified")
end

B = [2 5 -3 -4 8; 4 7 -4 -3 9; 6 9 -5 2 4; 0 -9 6 5 -6]

r = rank(B)
disp(r)

k = kernel(B)
nullity = size(k,2)
disp(nullity)

n = size(B,2)
disp(n)

if (r + nullity) == n then
disp("Rank Nullity Theorem Verified")
end
For the system of linear equations: X+4y+7z=1, 2x+5y+8z=2, x+2y+3z=1
Does the solution exist?
How many free variables are there in solution?
Find the solution.

clc
clear

A = [1 4 7; 2 5 8; 1 2 3]
//X = [x1; x2; x3]
B = [1; 2; 1]

X = inv(A)*B
disp(X)

k = kernel(A)
nullity = size(k,2)
disp(nullity)
Show that the set of vectors (2,2,1), (1,-1,1), (1,0,1) forms a basis for space R3 ?
(ii)Find the coordinates of the vector (4,2,1) w.r.t. the basis set given in (i)

clc
clear

A = [2 1 1; 2 -1 0; 1 1 1]
//X = [x1; x2; x3]
B = [4; 3; 1]

X = inv(A)*B
disp(X)

X = inv(A')*B
disp(X)
Date: 07/06/2022

To write a script file for Gram-Schmidt orthogonalization process.


clc
clear

u1 = [3 0 4]
u2 = [-1 0 7]
u3 = [2 9 11]

U = [u1; u2; u3]

[r, c] = size(U)

//[m n]= size (a);


q = zeros (r,c);
r = zeros (c,c);
for j=1: c
v= U(:,j)
for i =1:j -1
r(i,j)=q(:,i)'*U(:,j)
v=v-r(i,j)*q(:,i)
end
r(j,j)= norm(v)
q(:,j)=v/r(j,j)
disp(v, ' v=' )
disp(q, ' q=' )
end
2. Determine if the following set of vectors form an orthogonal basis for
R2 with the standard inner product. u=(2,5,-1), v=(-2,1,1)
clc
clear
u = [2 5 -1]
v = [-2;1;1]
V=u*v
disp(V)
if V == 0 then
disp("It is orthogonal")
else
disp("It is not orthogonal")
end
Find orthonormal basis in part (ii)

clc
clear
u = [2;5;-1]
v = [-2;1;1]

w1 = u / norm(u)
disp(w1)

w2 = v / norm(v)
disp(w2)

You might also like