Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

aodf

;fourier series
clear all
close all
clc
syms x
f =input('Enter the function of x: ');
I=input('Enter the interval of [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)));
;eigen value,vector
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)
;product determinant eigen
clc
clear
Page 3
A=input(’Enter the Matrix: ’);
%Determinant
detA=det(A);
disp(’Determinant of A:’)
disp(detA)
%Eigenvalues
EV=eig(A);
disp(’Eigenvalues:’)
disp(EV)
%Product of eigenvalues
prev=prod(EV);
disp(’Product of Eigenvalues:’)
disp(prev)
;cayley-hamilton
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)
;solving differential equation using variation of parameters
clear all
close all
clc
syms c1 c2 x m
F=input('Enter the coefficients [a,b,c]: ');
f=input('Enter the RHS function f(x): ');
a=F(1);b=F(2);c=F(3);
AE=a*m^2+b*m+c; % Auxilliary Equation
m=solve(AE);
m1=m(1); m2=m(2);
D=b^2-4*a*c;
if(D>0) % Roots are real and different
y1=exp(m1*x);y2=exp(m2*x);
elseif (D==0)% Roots are real and equal
 y1=exp(m1*x);y2=x*exp(m1*x);
else % Roots are complex
 alfa=real(m1);beta=imag(m1);
 y1=exp(alfa*x)*cos(beta*x);
 y2=exp(alfa*x)*sin(beta*x);
end
yc=c1*y1+c2*y2; % Complimentary Solution
%%% Particular Integral by Method of variation of parameters.
fx=f/a;
W=y1*diff(y2,x)-y2*diff(y1,x); %%% Wronskian%%%
u=int(-y2*fx/W,x);
v=int(y1*fx/W,x);
yp=y1*u+y2*v; %%%Particular Integral%%%
y_gen=yc+yp; %%%General Solution%%%
check=input('If the problem has initial conditions then enter
1 else enter 2: ');
if(check==1)
cn=input('Enter the initial conditions [x0, y(x0), Dy(x0)]:');
dy_gen=diff(y_gen);
eq1=(subs(y_gen,x,cn(1))-cn(2));
eq2=(subs(dy_gen,x,cn(1))-cn(3));
[c1 c2]=solve(eq1,eq2);
y=simplify(subs(y_gen));
disp('The complete solution is');
disp(y);
ezplot(y, [cn(1),cn(1)+2]);
else
y=simplify(y_gen);
disp('The General Solution is ');
disp(y);
end
;solution of differential equation using laplace transform
clear all
clc
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),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(x): ');
eqn=a*d2y(t)+b*dy(t)+c*y(t)-nh;
LTY=laplace(eqn,t,s);
IC = input('Enter the initial conditions in the form [y0,Dy(0)]: ');
y0=IC(1);dy0=IC(2);
LTY=subs(LTY,{'laplace(y(t), t, s)','y(0)','D(y)(0)'},{Y,y0,dy0});
eq=collect(LTY,Y);
Y=simplify(solve(eq,Y));
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=')
disp(yt);
;
ezplot(yt,[y0,y0+2]);
;solution of first order and second order by matrix method
clc
clear
syms t C1 C2
A=input(’Enter A: ’);
[P,D]=eig(A);
L1=D(1);L2=D(4);
y1=C1*exp(L1*t);y2=C2*exp(L2*t);
Y=[y1;y2];
X=P*Y;
Cond=input(’Enter the initial conditions [t0, x10,x20]: ’);
t0=Cond(1);x10=Cond(2);x20=Cond(3);
eq1=subs(X(1)-x10,t0);eq2=subs(X(2)-x20,t0);
[C1, C2] = solve(eq1,eq2);
X=subs(X);
;z-transform and their application for solving differential equation
clear all
clc
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)');
;solution of homogenous equations
clear all
clc
syms n k1 k2 L
F = input('Input the coefficients [a,b,c]: ');
a=F(1);b=F(2);c=F(3);
ch_eqn=a*L^2+b*L+c; %Characteristic equation
L=solve(ch_eqn);
L1=L(1);L2=L(2);
D=b^2-4*a*c;
if(D>0) % Roots are real and different
y1=L1^n;
y2=L2^n;
elseif (D==0)% Roots are real and equal
 y1=L1^n;
 y2=n*L1^n;
else % Roots are complex
 rho=abs(L1); t=angle(L1);
 y1 = (rho^n)*cos(n*t);
 y2 = (rho^n)*sin(n*t);
end
yn = k1*y1+k2*y2;
check=input('If initial conditions are known, then enter 1 else
enter 0: ');
if (check == 1)
 IC=input('Enter the initial conditions [y(0),y(1)]');
 eq1=(subs(yn,n,0)-IC(1));
 eq2=(subs(yn,n,1)-IC(2));
 [k1,k2]=solve(eq1,eq2);
 yn=simplify(subs(yn));
 m=0:20;
y=subs(yn,n,m);
stem(y)
title('Difference equation');
xlabel('n'); ylabel('y(n)');
end
disp('The Solution of the given Homogeneous equation is y_n= ');
disp(collect(collect(yn,y1),y2)) 
;power series
L = 11;
num = [1 2];
den = [1 0.4 −0.12];
u = [1 zeros(1,L−1)];
x = filter(num,den,u);
disp('Coefficients of the power series expansion: ');
disp(x);
;similarity transformation
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)
;orthogonal transofrmation
clc
clear
A=input(’Enter the symetric matrix for diagonalization :’);
[P D]=eig(A);
disp(’Given Matrix (A) :’)
disp(A)
disp(’Modal Matrix (P):’)
disp(P)
NP=normc(P);
disp(’Normalized Modal Matrix (N):’)
disp(NP)
disp(’Diagonal Matrix (D=Nˆ T*A*N) :’)
DM=round(NP’*A*NP,2);
disp(DM)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.