Matlab Manual III (22-23)
Matlab Manual III (22-23)
Manual
For III Sem BE(2022-23)
Prepared by:
Department of Mathematics
Vidyavardhaka College of Engineering
Mysuru
Contents
MODULE 1:
Fourier and Harmonic Series
1. CONSTRUCTION OF FOURIER SERIES OF A FUNCTION
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')
OUTPUT
F= …
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=
MODULE 2:
FOURIER AND Z TRANSFORMS
1. FOURIER TRANSFORM
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=
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
F =
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=
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
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
OUTPUT
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
OUTPUT
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
MODULE 4:
PDE AND NUMERICAL SOLUTION OF PDE
One dimensional Heat Equation
𝑢(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.
end
OUTPUT
𝑢(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.