0% found this document useful (0 votes)
16 views19 pages

Matlab Manual III (22-23)

inputNeurons = 2 hiddenLayerNeurons = 4 output Neurons = 2 iteration = 6000 input = np.random.randint(1, 5, inputNeurons) output = np.array([1.0, 0.0]) hidden_layer = np.random.rand(1, hidden Layer Neurons) hidden_biass = np.

Uploaded by

pofeya2801
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)
16 views19 pages

Matlab Manual III (22-23)

inputNeurons = 2 hiddenLayerNeurons = 4 output Neurons = 2 iteration = 6000 input = np.random.randint(1, 5, inputNeurons) output = np.array([1.0, 0.0]) hidden_layer = np.random.rand(1, hidden Layer Neurons) hidden_biass = np.

Uploaded by

pofeya2801
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/ 19

III Semester BE MATLAB Lab Manual

Manual
For III Sem BE(2022-23)

Prepared by:

Department of Mathematics
Vidyavardhaka College of Engineering
Mysuru

VVCE, Mysuru Page 1


III Semester BE MATLAB Lab Manual

Contents

SL.No. Chapters Page No.

1 Fourier and Harmonic Series 3-6

2 Fourier Transformation and Z Transformation 7-9

3 Numerical Solution of ODE 10 - 14

4 PDE and Numerical Solution of PDE 15 - 18

VVCE, Mysuru Page 2


III Semester BE MATLAB Lab Manual

MODULE 1:
Fourier and Harmonic Series
1. CONSTRUCTION OF FOURIER SERIES OF A FUNCTION

Q: Compute Fourier series expansion of 𝑒 in (- , ) up to 25 terms and plot to visualize the


same using MaTLab Code.

syms x
a=-pi; %lower limit of the interval
b=pi; %upper limit of the interval
I=[a b];
l=(b-a)/2 % the value of l
y = exp(x);
%Construction of Fourier Series
a0 = vpa((1/l)*int(y,x,a,b),3); %Formula for a0
i = 25; %Bigger the number more the accuracy
sum=0;
for n= 1:i
an = (1/l)*int(y*cos((n*pi*x)/l),x,I); %Formula for an
bn = (1/l)*int(y*sin((n*pi*x)/l),x,I); %Formula for bn
sum = sum + an*cos((n*pi*x)/l) + bn*sin((n*pi*x)/l);
end
F=vpa((a0/2)+sum,3) %The required Fourier Series
%Visualize by plotting
fplot(x,y,I)
grid on; hold on;
fplot(x,F,I)
title('Fourier Series of a function')

VVCE, Mysuru Page 3


III Semester BE MATLAB Lab Manual

legend('Function', 'Its Fourier Series')


hold off

OUTPUT

F= …

VVCE, Mysuru Page 4


III Semester BE MATLAB Lab Manual

2. CONSTRUCTION OF HARMONIC SERIES OF A FUNCTION

X=[0 pi/3 2*pi/3 pi 4*pi/3 5*pi/3];


y=[7.9 7.2 3.6 0.5 0.9 6.8];%Given data
h=pi/3; %Specify the common difference
N=length(X);
l=(N*h)/2;
a0 = (2/N)*sum(y); %Formula for a0
%Constructing the coefficients in the form of an array
for n=1:N
a(n)=(2/N)*sum(y.*cos((n*pi*X)/l)); %Formula for an
b(n)=(2/N)*sum(y.*sin((n*pi*X)/l)); %Formula for bn
end

syms x
for n=1:N
F=a0/2+sum((a(n)*cos(n*pi*x)/l))+sum((b(n)*sin(n*pi*x)/l));
end
disp(F)

%First harmonics
a1=a(1)
b1=b(1)

%Nth harmonics
aN=a(N)
bN=b(N)

scatter(X,y)
hold on;
fplot(x,F,[0,2*pi])
hold off
OUTPUT

F=

VVCE, Mysuru Page 5


III Semester BE MATLAB Lab Manual

VVCE, Mysuru Page 6


III Semester BE MATLAB Lab Manual

MODULE 2:
FOURIER AND Z TRANSFORMS
1. FOURIER TRANSFORM

Q: Compute the Fourier transform of 𝑒 | |


. And also plot to visualize.

clc
clear
syms t
f=exp(-1*abs(t))
F=fourier(f)
fplot(F)
%fourier(f,x) To specify the transformation variable as x
%fourier(f,t,y) To specify the variable t as y

F=

VVCE, Mysuru Page 7


III Semester BE MATLAB Lab Manual

2. Inverse Fourier Transform

Q: Compute the inverse Fourier transform of .


( )

clc
clear
syms t
f=2/(t^2+1)
F=ifourier(f)
fplot(F)
%fourier(f,x) To specify the transformation variable as x
%fourier(f,t,y) To specify the variable t as y

Also plot the function which is left as an excercise

F =

VVCE, Mysuru Page 8


III Semester BE MATLAB Lab Manual

3. Z - TRANSFORM AND ITS INVERSE

Q: Compute the Z-transform of n(1-(1/n)). And plot to visualize.

Code

clc
clear
syms n
f=n*(1-(1/n))
Z=ztrans(f)
fplot(Z)
%ztrans(f,x) To specify the transformation variable as x
%ztrans(f,t,y) To specify the variable t as y

Z=

Similarly use iztrans(F) , iztrans(F,transVar), iztrans(F,var,transVar) to compute the inverse Z


transformation of functions.

VVCE, Mysuru Page 9


III Semester BE MATLAB Lab Manual

MODULE 3:
NUMERICAL SOLUTION OF ODE
1. RK4 METHOD TO SOLVE AN IVP (FIRST ORDER ODE)

Q- Obtain the numerical solution of the first order ODE = 𝑦 (1 − 2𝑥) where
𝑦(0) = 1 with h=0.1. Find the solutions at points up to x=3 and plot to compare
with the analytical solution.
x0=0; xn = 3; y0=1; h=0.1;
f = @(x,y) (y^2)*(1-2*x); % Data extracted from the IVP

x = x0:h:xn; %Forms an array with cd=h


n = length(x)-1;% Number of intermediate points
y = zeros(1,n+1);% Creates a row matrix of zeros
y(1) = y0; %intial value of y

for i=1:n
K1 = h*f(x(i),y(i));
K2 = h*f(x(i)+0.5*h,y(i)+0.5*K1);
K3 = h*f(x(i)+0.5*h,y(i)+0.5*K2);
K4 = h*f(x(i)+h,y(i)+K3);
y(i+1) = y(i)+(1/6)*(K1+2*K2+2*K3+K4);
end
y01=y(2)%VAlue of y at x=0.1
y02=y(3)%VAlue of y at x=0.2

ye=eval(dsolve('Dy=(y^2)*(1-2*x) ','y(0)=1.0','x'));% Change according to the


problem
plot(x,y,'*',x,ye,'r')
grid on;
xlabel('x - axis')
ylabel('y - axis')
title('Solution of IVP using RK-4 method')
legend('Numerical solution', 'Exact Solution')

VVCE, Mysuru Page 10


III Semester BE MATLAB Lab Manual

OUTPUT

VVCE, Mysuru Page 11


III Semester BE MATLAB Lab Manual

2. MILNE’S PREDICTOR CORRECTOR METHOD

Q- Obtain the numerical solution of the first order ODE = − − 2 where


𝑦(1) = 1 with h=0.1. Find the solutions at points x=0.1, 0.2, 0.3 using RK-4
method and find solutions at all other points up to x=3 using Milne’s Predictor
Corrector formula and plot to compare with the analytical solution.
x0=0; xn = 3; y0=1; h=0.1;
f = @(x,y) (y^2)*(1-2*x); % Data extracted from the IVP

x = x0:h:xn; %Forms an array with cd=h


n = length(x)-1;% Number of intermediate points
y = zeros(1,n+1);% Creates a row matrix of zeros
y(1) = y0; %intial value of y

for i=1:3
K1 = h*f(x(i),y(i));
K2 = h*f(x(i)+0.5*h,y(i)+0.5*K1);
K3 = h*f(x(i)+0.5*h,y(i)+0.5*K2);
K4 = h*f(x(i)+h,y(i)+K3);
y(i+1) = y(i)+(1/6)*(K1+2*K2+2*K3+K4);
end
%Further values using predictor corrector formula
for i=4:n
y(i+1)=y(i-3)+(4*h/3)*(2*f(x(i),y(i)) - f(x(i-1),y(i-1))+2*f(x(i-2),y(i-
2)));%predictor formula
y(i+1)=y(i-1)+(h/3)*(f(x(i+1),y(i+1)) + 4*f(x(i),y(i)) + f(x(i-1),y(i-1))); %
Corrector formula
end

y4=y(5) % The value of y4


disp(y); % To display all values of y

ye=eval(dsolve('Dy=-2*y/x - y^2/x^2', 'y(1)=1.0','x'));% Change according to the


problem
plot(x,y,'*',x,ye,'r');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using Milne''s P-C Method');

VVCE, Mysuru Page 12


III Semester BE MATLAB Lab Manual

legend('Numerical solution', 'Exact Solution');

OUTPUT

VVCE, Mysuru Page 13


III Semester BE MATLAB Lab Manual

3. RK4 METHOD TO SOLVE SECOND ORDER ODE.

Q: Use the Runge Kutta Fourth Order method to find y(0.1) for the IVP 𝑦 − 𝑥 𝑦 − 2𝑥𝑦 = 1

syms x y z
x0=0; y0=1; z0=0 ;h=0.1; xn=3; f = @(z)z;
g=@(x,y,z)1+2*x*y+x^2*z % Data extracted from the IVP by replacng dy/dx by z
K1 = h*f(z0)
L1 = h*g(x0,y0,z0)
K2 = h*f(z0+L1/2)
L2 = h*g(x0+0.5*h,y0+0.5*K1,z0+0.5*L1)
K3 = h*f(z0+L2/2)
L3 = h*g(x0+0.5*h,y0+0.5*K2,z0+L2/2)
K4 = h*f(z0+L3)
L4 = h*g(x0+h,y0+K3,z0+L3)
y01 = y0+(1/6)*(K1+2*K2+2*K3+K4)% The vale of y at x0+h=0.1
z01 = z0+(1/6)*(L1+2*L2+2*L3+L4)% The vale of y' at x0+h=0.1

OUTPUT

VVCE, Mysuru Page 14


III Semester BE MATLAB Lab Manual

MODULE 4:
PDE AND NUMERICAL SOLUTION OF PDE
One dimensional Heat Equation

Q: Solve the 1-dimensional heat equation:


𝜕𝑢 𝜕 𝑢
=2 , 0 < 𝑥 < 5, 𝑡 > 0,
𝜕𝑡 𝜕𝑥
subject to the conditions:

𝑢(0, 𝑡) = 0, 𝑢(5, 𝑡) = 0, 𝑡 ≥ 0;
𝑢(𝑥, 0) = 𝑥 (25 − 𝑥 ), 0 ≤ 𝑥 ≤ 5.

by defining a function called FTCS_Heat in matlab code and then applying the given
conditions.

x0=0; xm = 5; tn = 0.25; h = 0.5; k=0.05; c2=2;


f = @(x) x.^2.*(25-x.^2);
[x,t,u] = FT_Heat(x0,xm,tn,h,k,c2,f);
fprintf('Numerical solution of the given equation is : \n');
disp(u);
plot(x,u);
grid on;
xlabel('x - values');
ylabel('Solution at specified t - values');
title('Numerical solution of 1-D Heat equation');
%Defining the function FTCS_Heat
function [x,t,u] = FT_Heat(x0,xm,tn,h,k,c2,f)
lambda=c2*k/h^2;
x=x0:h:xm;
n=length(x);
t=0:k:tn;
m=length(t);
u=zeros(n,m);
u(:,1)=f(x);
for j=1:m-1
for i=2:n-1
u(i,j+1)= lambda*u(i-1,j)+(1-2*lambda)*u(i,j)+lambda*u(i+1,j);
end
end
VVCE, Mysuru Page 15
III Semester BE MATLAB Lab Manual

end

OUTPUT

Numerical solution of the given equation is

VVCE, Mysuru Page 16


III Semester BE MATLAB Lab Manual

One dimensional Wave Equation

Q: Solve the 1-dimensional wave equation:


𝜕 𝑢 𝜕 𝑢
=4 , 0 < 𝑥 < 1, 𝑡 > 0,
𝜕𝑡 𝜕𝑥
subject to the conditions:

𝑢(0, 𝑡) = 0, 𝑢(1, 𝑡) = 0, 𝑡 ≥ 0;
𝑢(𝑥, 0) = sin 2𝜋𝑥, 𝑢 (𝑥, 0) = 0, 0 ≤ 𝑥 ≤ 1.

by defining a function called FT_Wave in matlab code and then applying the given
conditions.

x0=0; xn=1; t0=0; tm=0.5; h=0.05; k=0.1; c=1;


f= @(x) sin(2*pi*x); g = @(x) 0;
[x,t,u] = FT_wave(t0,tm,x0,xn,h,k,c,f,g);
fprintf('Numerical solution of the given equation is : \n');
disp(u);
plot(x,u);
grid on;
xlabel('x - values');
ylabel('Solution at specified t - values');
title('Numerical solution of 1-D wave equation');
%Defining the function FT_Wave
function [x,t,u] = FT_wave(t0,tm,x0,xn,h,k,c,f,g)
r = c*k/h;
x=x0:h:xn;
t=t0:k:tm;
n=length(x);
m=length(t);
u=zeros(n,m);
u(:,1)=f(x);
for i=2:n-1
u(i,2)=(1-r^2)*u(i,1)+0.5*(r^2*(u(i-1,1)+u(i+1,1)))+k*g(x(i));
end
for j=2:m-1
for i=2:n-1
u(i,j+1)=2*(1-r^2)*u(i,j)+r^2*(u(i-1,j)+u(i+1,j))-u(i,j-1);
end
end
end

VVCE, Mysuru Page 17


III Semester BE MATLAB Lab Manual

VVCE, Mysuru Page 18


III Semester BE MATLAB Lab Manual

VVCE, Mysuru Page 19

You might also like