Matlab 2
Matlab 2
clc
clear
close all
%Rectangle 1
t1=-1:0.01:1;
y1=rectpuls(t1);
subplot(321)
plot(t1,y1,'r','Linewidth',2),
grid on
title('Rectangle usig rectpuls')
%Unit step
t4=0:0.01:1;
y5=[zeros(1,20),ones(1,81)];
subplot(322)
plot(t4,y5,'k','Linewidth',2),
grid on
title('Unit step')
%Rectangle 2
t2=0:0.01:1;
y2=[zeros(1,25),ones(1,50),zeros
(1,26)];
subplot(323)
plot(t2,y2,'k','Linewidth',2),
grid on
title('Rectangle using Unit
step')
%Triangle
t3=-1:0.01:1;
y3=tripuls(t3);
subplot(324)
plot(t3,y3,'r','Linewidth',2),
grid on
title('Triangle using tripuls')
%Unit impulse
t4=0:0.01:1;
y4=[zeros(1,20),3,zeros(1,80)];
subplot(325)
plot(t4,y4,'k','Linewidth',2),
grid on
title('Unit impulse')
Representing Signal
clc
clear
close all
x=[1 3 5 7 9 7 5 3 1];
stem(x,'r','Linewidth',2)
Representing Signal With Sin Wave
clc
clear
close all
t=0:0.01:1;
f=2;
y=sin(2*pi*f*t);
subplot(211)
plot(t,y)
subplot(212)
stem(t,y)
Signal genertion and Save as ASCII
clc
clear
close all
t=0:0.01:2;
f=2;
x=rand(1,length(t));
y=sin(2*pi*f*t);
y2=y+x;
plot(t,y,'r--',t,y2,'b-
','linewidth',3), grid on
title('Sinusoidal Function')
xlabel('Time[sec]')
ylabel('Amplitude')
legend('Sin','Sin + Noise',0)
save test.dat t y y2 -ascii
clc
clear
close all
load test.dat;
t2=test(1,:);
x2=test(2,:);
y2=test(3,:);
plot(t2,x2,'k--',t2,y2,'m-
','Linewidth',3), grid on
title('Sinusoidal Function after
loading')
xlabel('Time[sec]')
ylabel('Amplitude')
legend('Sin','Sin + Noise',0)
Curve fit
clc
clear
close all
t1 = [0 0.3 0.8 1.1 1.6 2.3];
y1 = [0.6 0.67 1.01 1.35 1.47
1.25];
p1 = polyfit(t1,y1,2);
y1hat = polyval(p1,t1);
E1 = y1 - y1hat;
figure
plot(t1,y1,'bo','linewidth',3),
grid on
title('Plot of y Versus t')
t2 = 0:0.1:2.8;
y2 = polyval(p1,t2);
figure
plot(t1,y1,'bo',t2,y2,'r','linew
idth',3), grid on
title('Plot of y Versus t')
legend('Data Points','Fitting
Line-Model',0)
figure
plot(t1,E1,'+','linewidth',3),
grid on
title('Plot of the Residuals')
%*** Order-5; Fifth-Degree
Polynomial Fit
t5 = 0:0.1:2.6;
p5 = polyfit(t1,y1,5);
y5hat = polyval(p5,t5);
figure
plot(t1,y1,'bo',t5,y5hat,'r','li
newidth',3), grid on
title('Plot of y Versus t')
legend('Data Points','Fitting
Order-5',0)
Energy and Temperature
clc
clear
close all
%*** Energy and Temperature data
t = [100, 150, 200, 250, 300,
400, 500]; % Temperature [deg-c]
e = [2507, 2583, 2658, 2734,
2810, 2968, 3132]; % Energy
[Kj/Kg]
subplot(211)
plot(e,t, '-o','linewidth',3),
grid on
% Find interpolated value for
e=2681
e2 = 2681; % New value of e
interp1(e, t, e2)
%Spline
e3 =
linspace(2500,3200,length(e));
t2 = interp1(e,t, e3, 'spline');
subplot(212)
plot(e,t, e3, t2, 'r--
o','linewidth',3), grid on
signal
clc
clear
close all
t=0:0.01:0.99;
t1=-1:0.01:1;
t2=-5:0.5:5;
f=5;
x=[zeros(1,45),3,zeros(1,54)];%i
mpulse
y=ones(1,100);%step
z=t;%ramp
a=t.^2;%Parabolic
b=square(2*pi*f*t);%square
c=sawtooth(2*pi*f*t);%sawtooth
d=sinc(2*pi*f*t1);%sinc
k=sinc(t2);%sinc
subplot(421)
plot(t,x,'r','Linewidth',3)
title('Impulse')
subplot(422)
plot(t,y,'r','Linewidth',3)
title('Step')
subplot(423)
plot(t,z,'k','Linewidth',3)
title('Ramp')
subplot(424)
plot(t,a,'k','Linewidth',3)
title('Parabolic')
subplot(425)
plot(t,b,'b','Linewidth',3)
title('Square')
subplot(426)
plot(t,c,'b','Linewidth',3)
title('Sawtooth')
subplot(427)
plot(t1,d,'g','Linewidth',3)
title('Sinc')
subplot(428)
plot(t2,k,'g','Linewidth',3)
title('Sinc')
figure
subplot(2,1,1)
plot(x,y ,'bo','linewidth',3),
grid on
xlabel('X')
ylabel('Y')
title('Plotting the given Data
Points')
subplot(2,1,2)
plot(x,y,'m','Linewidth',3),grid
on
xlabel('X')
ylabel('Y')
title('ploting')
Arithmetic Mean
clc
clear
close all
A=[10 9 11;4 3 1;7 8 9;10 15
13];
size(A)
suma=0;
for i=1:9;
suma=suma+A(i);
end
Am=suma/9
mean(A(:))
mean(A)
Max and Min
clc
clear
close all
a=[9 8 1;10 15 4;7 4 3];
min(a)%بياخد اصغر رقم في العمود
min(a(:))
max(a)%بياخد اكبر رقم في العمود
max(a(:))
Race Car Speed Prediction
clc
clear
close all
t=[0 5 10 15 20 25 30 35 40];
v=[24 33 62 77 105 123 151 170
188];
figure
plot(t,v,'go',t,v,'r-
','linewidth',3),grid on
title('car velocity Vs Time')
xlabel('Time[sec]')
ylabel('Velocity[m/sec]')
m=4.2;
b=18.3;
t2=0:5:40;
v2=b+m*t2;
figure
plot(t,v,'go',t,v,'r-',t2,v2,'b-
-','linewidth',3),grid on
title('car velocity Vs Time')
xlabel('Time[sec]')
ylabel('Velocity[m/sec]')
legend('Data Points','Measured
Velocity','Predicted
Velocity',0)
Student Performance Comparison
clc
clear
close all
format bank % 2 digits
A=[85 45 96 27 11];
B=[19 55 17 21 90];
At=0;%total marks of A
Bt=0;%total marks of B
for i=1:length(A);
At=At+A(i);
end
Aavag=At / length(A);
for i=1:length(B);
Bt=Bt+B(i);
end
Bavag=Bt / length(B);
disp([ ' Aavag= '
num2str(Aavag)])
disp([ ' Bavag= '
num2str(Bavag)])
if Aavag >Bavag;
disp(' student A is better
than student B')
Aavag;
else
disp(' student B is better
than student A')
Bavag;
end
Variance Standard Deviation
clc
clear
close all
a=[1 7 10;4 2 6;7 8 9];
n=9;
sum=0;
for i=1:n
sum=sum+a(i);
end
mean=sum/9;
var1=0;
for i=1:9
var1=var1+power((a(i)-
mean),2);
end
var1=var1/(n-1)
var(a(:))
Std1=sqrt(var1)
std(a(:))
Analytical Vs Numerical
clc
clear
close all
format short;
g=9.8;
cd=12.5;
m=68.1;
dt=input('enter time increment
dt [sec]: ');
tf=input('enter final time
[sec]: ');
%Analytical
T=0:dt:tf;
V=g*m/cd*(1-exp((-cd/m)*T));
%Numerical
ti=0:dt:tf;
ns=length(T);
vi(1)=0;
for n1=2:ns;
vi(n1)=vi(n1-1)+(g-
(cd/m)*vi(n1-1))*dt;
end
plot(T,V,'r',ti,vi,'b--
','linewidth',3), grid on
xlabel('Time[sec]')
ylabel('velocity[m/s]')
title(['Terminal Velocity Of
Moving Body',' with Sampling
period \ Deltat =' num2str(dt)])
legend('Analytical','Numerical')
Diff and Int
clc
clear
close all
x=sym('x');
y=diff(x^3);
z=int(x^3);
disp(['y= ',char(y)])
disp(['z= ',char(z)])
Generating Squences
clc
clear
close all
j=1;
for i=3:1.5:10
x(j)=i;
j=j+1;
end
disp(x')
Int use Quadl
clc
clear
close all
quadl(@sin,0,pi)
Int Use Trapezoids
clc
clear
close all
x=0:0.5:10;
y=0.5*sqrt(x)+x.*sin(x);
u=trapz(x,y)
clc
clear
close all
format long
x=linspace(0,pi,1000);
y=sin(x);
trapz(x,y)
Laplace
clc
clear
close all
syms t s
f=5*t;
F=laplace(f);
disp(['F= ',char(F)])
clc
clear
close all
syms t s
f=3*exp(-2*t)*sin(5*t)+4*exp(-
2*t)*cos(5*t);
F=laplace(f);
S=simplify(f);
disp(['F= ',char(F)])
disp(' ')
pretty(F)
disp(' ')
disp(['S= ',char(S)])
Math Tollbox
clc
clear
close all
x=sym('x');
f=1/(5+4*cos(x));
ezplot(f), grid on
clc
clear
close all
X=sym('X');
figure
ezplot(exp(-pi*X*X))
axis([-4 4 0 1.1])
figure
ezplot(sinc(X))
axis([-6 6 -0.22 1.1])
NumInt
clc
clear
close all
x=sym('x');
y=sin(x^2);
yint=int(y,x)
ezplot(y), grid on
Subplot
clc
clear
close all
x=-1:0.05:1;
for n=1:8
subplot(4,2,n);
y=sin(n*pi*x);
plot(x,y,'r','linewidth',3),
grid on
end
Sym Int Of Functions
clc
clear
close all
x=sym('x');
f=2*x^5+5*x^4-7*x^3+x^2+x+15;
disp(['f= ',char(f)])
fd=diff(f);
disp(['Fd= ',char(fd)])
fi=int(fd);
disp(['Fint = ',char(fi)])
figure
ezplot(f), grid on
figure
ezplot(fd), grid on
figure
ezplot(fi), grid on
clc
clear
close all
x=sym('x');
y=0.5*sqrt(x)+x.*sin(x)+x.^2;
disp(['y = ',char(y)])
yint1=int(y,x,-3,15);
ezplot(y), grid on
Arithmatic Sequuence
clc
clear
close all
x=input(' enter the value of
[start,step,number of
values]: ');
n1=x(1);
s=x(2);
n2=x(3);
sum=0;
an =[];
for i=1:n2
an(i)=n1+(i-1)*s;
sum=sum+an(i);
end
disp(' ')
disp(' Arithmatic Sequuence:
an=a1+(n-1)s');
disp(' ====================')
disp([' Sequence:'
num2str(an)])
disp([' Series sum:'
num2str(sum)])
Factorial
clc
clear
close all
n=1;
N=input(' enter a positive
integer(0 to exit): ');
if N~=0
for i=1:N;
n=n*i;
end
end
disp([' Factorial of (',
num2str(N) ,') = '
num2str(n)])
Fibonacci Sequence
clc
clear
close all
nr=input(' enter the range of
Fibonacci Sequence: ');
for i=1:nr
if(i<=2)
fib(i)=1;
else
fib(i)=fib(i-
1)+fib(i-2);
end
end
disp('=======================
========= ')
disp(['fibonicci series of ('
num2str(nr) ') elements ={'
num2str(fib),'}'])
Geometric sequence
clc
clear
close all
%using Summation Notation
disp(' Geometric squence:
an=s1*n+1');
disp(' ')
x1=input(' enter the value of
[step,number of values]: ');
s1=x1(1);
n1=x1(2);
sum1=0;
an =[];
for i=1:n1
an(i)=s1*i+1;
sum1=sum1+an(i);
end
disp(' ')
disp([' sequence:'
num2str(an)])
disp([' series sum:'
num2str(sum1)])
laplace
clc
clear
close all
syms t s
f=5*t;
F=laplace(f);
disp(['F= ',char(F)])
clc
clear
close all
syms t s
f=3*exp(-
2*t)*sin(5*t)+4*exp(-
2*t)*cos(5*t);
F=laplace(f);
m=simplify(F);
disp([' f = ',char(f)])
disp(' ')
disp([' F = ',char(F)])
pretty(F)
disp(' ')
disp([' F = ',char(m)])
pretty(m)
Gaussian And Uniform White Noise
clc
clear
close all
mu=0;
sigma=1;
noise=sigma*randn(1,10)+mu
More Simulation Techniques Available In The Following
Ebooks
clc
clear
close all;
L=100000;
mu=5;
sigma=2;
X=sigma*randn(L,1)+mu;
figure
subplot(211)
plot(X,'r');
title(['White noise :
\mu_x=',num2str(mu),'
\sigma^2=',num2str(sigma^2)])
xlabel('Samples')
ylabel('Sample Values')
grid on;
subplot(212)
n=10;
[f,x]=hist(X,n);
bar(x,f/trapz(x,f),'g')
hold on
g=(1/(sqrt(2*pi)*sigma))*exp(-
((x-mu).^2)/(2*sigma^2));
plot(x,g,'b','linewidth',3)
hold off
grid on
title('Theoretical PDF and
Simulated Histogram of White
Gaussian Noise');
legend('Histogram','Theoretical
PDF');
xlabel('Bins');
ylabel('PDF f_x(x)');
Random Number Generators
clc
clear
close all
a=5;
b=500;
y=a*randn(1000,1)+b;
stats=[mean(y) std(y) var(y)]
Random Numbers With in A Specific Range
clc
clear
close all
a=input('ent signal min range
:');
b=input('ent signal max range
:');
y=(b-a).*rand(1000,1)+a;
y_range=[min(y) max(y)]
plot(y),grid on
Random Numbers with in A Sphere
clc
clear
close all
r=2*rand(1000,1)-1;
th=asin(r);
fic=2*pi*rand(1000,1);
rad=3*(rand(1000,1).^(1/3));
[x1,y1,z1]=sph2cart(fic,th,rad);
figure
plot3(x1,y1,z1,'r'),grid on
axis equal
[x2,y2,z2]=sph2cart(fic,th,3);
figure
plot3(x2,y2,z2,'b'), grid on
axis equal
RLC Circuit
clc
clear
close all
R=10000;
C=0.1*10^-9;
L=0.01;
n=[L 0];
d=[R*L*C L R];
g=tf(n,d)
figure
step(g),grid on
figure
bode(n,d),grid on
RLC circuit analysis using MATLAB
y=conv(x,h)
subplot(311)
plot(x,'R','linewidth',3)
subplot(312)
plot(h,'b','linewidth',3)
subplot(313)
plot(y,'k','linewidth',3)
Convolution
clc
clear
close all
t=-1:0.01:2;
x=[zeros(1,100),ones(1,100),zeros(1,101)];
figure
plot(t,x,'R','linewidth',3)
t1=-2:0.01:3;
x1=[zeros(1,100),ones(1,300),zeros(1,101)];
figure
plot(t1,x1,'b','linewidth',3)
y=conv(x,x1);
figure
stem(y,'g','linewidth',1)
FourierTransform
clc
clear
close all
%303 صفحةbook pdf
%-----------------------------------------------
--------------------------------------
% % Purpose : To calculate DFT of a time signal
of two sinusoids
% ----------------------------------------------
--------------------------------------
% Get the input parameters
fr1=input('Frequency of the first sinusoid [Hz]
= ? ');
a1=input('Amplitude of the first sinusoid [ ] =
? ');
fr2=input('Frequency of the second sinusoid
[Hz] = ? ');
a2=input('Amplitude of the first sinusoid [ ] =
? ');
T=input ('Time record length [s] = ? ');
dt=input ('sampling time interval [s] = ? ');
fmax=input('maximum frequency for DFT [Hz] = ?
');
df=input('frequency sampling interval for DFT
[Hz] = ? ');
t1=0:dt:T;
N=T/dt; w1=2*pi*fr1; w2=2*pi*fr2; M=fmax/df;
% Build the input time series
for k=1:N
st(k)=a1*sin(w1*dt*k)+a2*sin(w2*dt*k);
end
% Apply the DFT with M points
for k=1:M
Sf(k)=complex(0,0);
for n=1:N
Sf(k)=Sf(k)+st(n)*exp(-i*2*pi*n*dt*k*df);
end
Sf(k)=Sf(k)*dt/2*pi;
end
% Prepare the frequency samples
for k=1:M
F(k)=(k-1)*df;
end
% Plot the output
figure
plot(F,abs(Sf),'b','linewidth',3);
title('The DFT of the Sum of two Sinusoids')
xlabel('Frequency [Hz]'); ylabel('Amplitude')
figure
plot(t1(1:N-1),st(1:N-1),'r','linewidth',3);
FourierTransform
clc
clear
close all
%312 صفحةbook pdf
%-----------------------------------------------
-----------------------------------------
% Program : FFT.m
% Purpose : To calculate FFT of a sine modulated
Gauss function
% ----------------------------------------------
------------------------------------------
% Get the input parameters
N=input ('N = ? ');
fr=input('Modulation frequency [Hz] = ? ');
dt=input ('time step, dt [s] = ? ');
w=2*pi*fr;
% Build the input time series
for k=1:N
X(k)=sin(w*dt*k)*exp(-(dt*N/(10*dt*k))^2);
end
% Apply the FFT
X2=fft(X,N);
X2=fftshift(X2); % swaps the left and right
halves
X2=abs(X2);
% Prepare the frequency samples
fmax=1/dt; df=1/(N*dt);
for k=1:N
F(k)=-fmax/2+(k-1)*df;
end
% Plot the output
plot(F,X2,'b','linewidth',3);
title('The FFT of a Time Signal')
xlabel('Frequency [Hz]'); ylabel('Amplitude')