MATLAB CODES - try running on ur compilers

EXPERIMENT - 1A  

FOURIER SERIES

clc

clear all

close all

syms x

f=input("Enter the function of x: ")

I=input("Enter the interval [a,b]: ")

m=input('Enter the number of Harmonics required: ')

a=I(1);

b=I(2);

L=(b-a)/2;

a0=(1/L)*int(f,a,b);

Fx=a0/2;

for n=1:m

    figure;

    an(n)=(1/L)*int(f*cos(n*pi*x/L),a,b);

    bn(n)=(1/L)*int(f*sin(n*pi*x/L),a,b);

    Fx=Fx+an(n)*cos(n*pi*x/L)+bn(n)*sin(n*pi*x/L);

    Fx=vpa(Fx,4);

    ezplot(Fx,[a,b]);

    hold on

    ezplot(f,[a,b]);

    title(['Fourier Series with ',num2str(n),'harmonics']);

    legend('Fourier Series','Function Plot');

    hold off

end

disp(strcat('Fourier series with', num2str(n),'harmonics is: ',char(Fx)))

 

 

HARMONIC ANALYSIS

 

clear all

clc

syms t

x=input('Enter the equally spaced values of x: ');

y=input('Enter the values of y=f(x): ');

m=input('Enter the number of harmonics required: ');

n=length(x);

a=x(1);

b=x(n);

h=x(2)-x(1);

L=(b-a+h)/2;

theta=pi*x/L;

a0=(2/n)*sum(y);

Fx=a0/2; x1=linspace(a,b,100);

for i=1:m

    figure

    an=(2/n)*sum(y.*cos(i*theta));

    bn=(2/n)*sum(y.*sin(i*theta));

    Fx=Fx+an*cos(i*pi*t/L)+bn*sin(i*pi*t/L) ;

    Fx=vpa(Fx,4);

    Fx1=subs(Fx,t,x1);

    plot(x1,Fx1);

    hold on

    plot(x,y);

    title(['Fourier Series with ',num2str( i ),'harmonics'])

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

    hold off;

end

disp(strcat('Fourier series with ', num2str(i),' harmonics is:',char(Fx)));

 

_______________________________________________________________________________________________

 

EXPERIMENT - 2

 

EIGEN VALUES AND EIGEN VECTORS

clc

clear

A=input("Enter the Matrix: ");

%Characteristic Equation

cf=poly(A);

disp("Characteristic Equations")

disp(cf)

%Eigenvalues

EV=eig(A);

disp("Eigenvalues")

disp(EV)

%Eigenvectors

[P D]=eig(A);

disp("Eigenvectors")

disp(P)

 

 

CAYLEY-HAMILTON THEOREM

 

clc

clear

A=input("Enter the Matrix: ");

%Verification of Cayley-Hamilton theorem

cf=poly(A);

n=length(cf);

CHT=cf(1)*A^(n-1);

for i=2:n

CHT=CHT+cf(i)*A^(n-i);

end

disp("R.H.S of C-H Theorem: ")

disp(round(CHT))

%To find the inverse

INV=cf(1)*A^(n-2);

for i=2:n-1

INV=INV+cf(i)*A^(n-i-1);

end

INV=INV/(-cf(n));

disp("Inverse of A: ")

disp(INV)

 

 

DIAGONALISATION

 

clc

clear

A=input("Enter the matrix for diagonalization :");

[P D]=eig(A);

disp("Given Matrix (A) :")

disp(A)

disp("Modal Matrix (P):")

disp(P)

disp("Inverse of P :")

PI=inv(P);

disp(PI)

disp("Diagonal Matrix (D=Pˆ(-1)*A*P):")

DM=round(inv(P)*A*P, 2);

disp(DM)

 

_______________________________________________________________________________________________

 

EXPERIMENT - 3

 

METHOD OF VARIATION OF PARAMETERS

FOR NON-HOMOGENEOUS EQUATION

 

clc

clear all

close all

syms t

S=input("Enter the pair of independent solutions as [x1 x2]= ");

W=simplify(det([S(1) S(2); diff(S(1)) diff(S(2))]));

F(t)=input("Enter the nonhomogeneous part F(t)= ");

P=simplify(-S(1)*int(S(2)*F/W,t)+S(2)*int(S(1)*F/W,t));

disp(["Particular Integral = ",char(P)])

CAUCHY EULER METHOD

 

clc

clear all

close all

syms t

AE = input("Enter [ a b c ] for atˆ2D2x+btDx+cx=0: ");

r = roots([AE(1) AE(2)-AE(1) AE(3)]);

if imag(r)~=0

    x1(t) = exp(real(r(1))*t)*cos(imag(r(1))*t);

    x2(t) = exp(real(r(1))*t)*sin(abs(imag(r(1)))*t);

elseif r(1)==r(2)

    x1(t) = exp(r(1)*t);

    x2(t) = t*exp(r(1)*t);

else

    x1(t) = exp(r(1)*t);    x2(t) = exp(r(2)*t);

end

disp(['x = A ',char(x1(log(t))),' + B ',char(x2(log(t)))])

 

_______________________________________________________________________________________________

 

EXPERIMENT - 4

 

SOLUTON OF DIFFERENTIAL EQUATION USING LAPLACE TRANSFORM

 

clc

clear all

close all

syms s t real

assume(s,'positive')

n=input('Enter the order of linear equation: ');

b=input(['Enter the coefficients in 1 X ',num2str(n+1),' matrix: ']);

c=input(['Enter the initial condition in 1 X ',num2str(n),' matrix : ']);

N=0;D=0;

for i=1:n

    for j=1:n-i+1

        N=N+s^(n-j)*b(i)*c(j);

    end

    D=D+b(i)*s^(n-i+1);

end

D=D+b(n+1);

f(t)=input('Enter the RHS f(t) = ' );

F(s)=laplace(f);

Ys=(N+F)/D ;

y=simplify(ilaplace(Ys));

disp(['y(t)=',char(y)])

 

SOLUTON OF DIFFERENTIAL EQUATION USING MATRIX METHOD

 

syms t A c1 c2

A=input('Enter A for DX=AX: ')

[P D]=eig(A)

C=[c1;c2]

disp('The general solution of the system is ')

X=C(1,1)*P(:,1)*exp(D(1,1)*t)+C(2,1)*P(:,2)*exp(D(2,2)*t)

 

_______________________________________________________________________________________________

 

EXPERIMENT - 5

 

POWER SERIES

 

clear

syms x a0 a1 a2 a3

y = a0+a1*x+a2*x^2+a3*x^3 ;

dy = diff(y);

d2y = diff(dy);

gde = collect(d2y-2*dy-y,x) ;

cof = coeffs(gde,x);

A2 = solve(cof(1),a2);

A3 = solve(cof(2),a3);

y = subs(y,{a2,a3},{A2,A3});

y = coeffs(y,[a1 a0]);

disp('Solution is ')

disp(['y = A(',char(y(1)),'+...) + B(',char(y(2)),'+...)'])

 

 

Z-TRANSFORMS

 

clc

clear all

close all

syms n z y(n) Y

yn=y(n);

yn1=y(n+1);

yn2=y(n+2);

F = input('Input the coefficients [a,b,c]: ');

a=F(1);b=F(2);c=F(3);

nh = input('Enter the non-homogenous part f(n): ');

eqn=a*yn2+b*yn1+c*yn-nh;

ZTY=ztrans(eqn);

IC=input('Enter the initial conditions in the form [y0,y1]:');

y0=IC(1);y1=IC(2);

ZTY=subs(ZTY,{'ztrans(y(n),n,z)','y(0)','y(1)'},{Y,y0,y1});

eq=collect(ZTY,Y);

Y=simplify(solve(eq,Y));

yn=simplify(iztrans(Y));

disp('The solution of the difference equation yn=')

disp(yn);

m=0:20;

y=subs(yn,n,m);

stem(y)

title('Difference equation');

xlabel('n'); ylabel('y(n)');

 

_______________________________________________________________________________________________


Comments

Popular posts from this blog

ZEOLITE OR PERMUTIT PROCESS

BATTERIES AND ITS TYPES

REVERSE OSOMOSIS: ADVANTAGES AND DISADVANTAGES