19BCE1102 Brijesh Kumar Lab Fat Matlab Prof. Somnath Bera
19BCE1102 Brijesh Kumar Lab Fat Matlab Prof. Somnath Bera
Brijesh Kumar
LAB FAT MATLAB
PROF. SOMNATH BERA
QUE. 1
Input:
clc
clear all
format compact
syms x r c1 c2
p1 = input("Enter the coefficient of D2y : ")
p2 = input("Enter the coefficient of Dy : ")
p3 = input("Enter the coefficient of y : ")
eq = p1*r^2+p2*r+p3
r = solve(eq,r)
p = real(r(1))
q = imag(r(1))
if q~=0
y1 = exp(p*x)*cos(q*x)
y2 = exp(p*x)*sin(abs(q)*x)
else if r(1) == r(2)
y1 = exp(r(1)*x)
y2 = x*exp(r(1)*x)
else
y1 = exp(r(1)*x)
y2 = exp(r(2)*x)
end
end
y_h = c1*y1+c2*y2
W = simplify(y1*diff(y2)-y2*diff(y1))
f = input("Enter the non-homogenous part : ")
y_p = -y1*int((y2*f)/W)+y2*int((y1*f)/W)
y1 = simplify(y_h+y_p);
disp("The general solution of the given ODE is : ")
disp(y1)
Output:
Working:
QUE 2:
Input:
clc
clear all
close all
syms t
n= input('Enter the number of data points n : ');
x_0= input('Enter the starting value of x : ');
count = input('type 0 if the unit of x is deg. type a non-zero number otherwise');
s=input('Enter the length of the spacing between successive values of x :');
n1= input('Enter the number of harmonic of the series n1 : ');
for i=1:n
x(i)=x_0+(i-1)*s;
end
if (count == 0)
x=x*pi/180;
s=s*pi/180;
il=pi;
else
il=0.5*(x(n)+s-x(1));
end
y = input('Enter the y values (as a row vector) :')
% if x data is not in degree %%%% assume the interval is (0,2l)
%il=pi;%%%%% if x data is in degree
a_0= (2/n)*sum(y)
for i=1:n1
yc=y.*cos(i*pi*x/il);
ys=y.*sin(i*pi*x/il);
a(i)=(2/n)*sum(yc);
b(i)=(2/n)*sum(ys);
end
F_s=a_0/2;
for i=1:n1
subplot(ceil(n1/2),2,i)
plot(x,y,'r*'); %%%% given data points
hold on
F_s = F_s+a(i).*cos(i*pi*t/il)+b(i).*sin(i*pi*t/il);
%subplot(ceil(n1/2),2,i)
ezplot(vpa(F_s,4), [x(1) x(n)])
end
disp('Fourier series :')
vpa(F_s,4)
[cfs,trms] = coeffs(F_s); % Coeffients & Terms
Result = cfs(trms == sin(2.0*t))
disp('Coefficient of sin(2*t): ')
Result = vpa(Result)
OUTPUT
The coefficient of sin(2*t) is 2.1169 which is given in
output also.
Working: