Professional Documents
Culture Documents
2 K 12 LAB4
2 K 12 LAB4
LAB MANUAL
Objective:
Derivation in Matlab
Integration in Matlab
o Numerical computation
o Symbolic/ general computation
Square wave synthesis
o From built in function
o By coding
Fourier series
Derivation in Matlab:
You must first give MATLAB a list of the variable and function names that
will appear in the symbolic expressions you will be working with. Do this using
the command syms
>>syms x y z a;
This command basically tells the software that you will be using the symbols x,
y, z, and f in forthcoming symbolic expressions.
Code:
clc
clear all
close all
syms x;
f = x^2 + sin(x);
diff(f,x)
diff(f,x,2) %will give 2nd order derivative
diff(f,x,3) % 3rd order derivative
limit(f,x,0); % to take the limit of f as x goes to zero.
Integration in Matlab:
Integration is an important operation In signals and systems employed in several analysis techniques.
These techniques include calculating the energy, power, trigonometric and exponential Fourier series
and Fourier transform.
subs(f,{x,y},{1,pi/2}) % If you would like to substitute numerical values into a symbolic function, use
To do double or triple integrals you need to manually use the int function multiple times.
Numerical computation
Different methods through formulas
quad command which uses an adaptive Simpson’s method
trapz which uses Trapezoidal integration
quad(’exp(-x.ˆ2)’,1,2)
ans =
0.1353
Code:
t = -1:0.01:3; %time
u_t=t>=0;
u_t_1=t>=2;
f=u_t-u_t_1;
I1 = trapz(t,f)
t = 0:0.01:pi/2; %time
g = sin(2*t); %g(t)=sin(2t)
I2 = trapz(t,g)
Graph a 1 KHz pulse wave of 5 volts peak amplitude. Choose the number of points as
power of two, i.e. 128, 256, 512. This selection for the number of points will help speed up
MATLAB Program:
plot(t,v)
axis([0 T -10 10]) %Change the scale on the figure x-axis: 0 to T, y-axis: -10
plot(t,v)
Synthesize the square wave from harmonics: (Use the Fourier Table for calculating harmonic
amplitudes)
(a) Fundamental.
(b) Fundamental and 3rd harmonic.
(c) Fundamental, 3rd and 5th harmonics.
LAB TASK:
Q1:Take 1st , 2nd , 3rd , order derivative of function
f(x)= x2cos(x)+xsin(x)2
Q2:Find anti derivative for the function
f(x)=xsin(x)2
Q3:Evaluate integral
2
1 ∫sin(x)2dx
Q4:
(a) Write the code to add the 7th harmonic to obtain the waveform.
(b) Write the instructions to add the 9th harmonic to obtain the waveform.
(c) ) Write a program to add Fundamental to 99th harmonic and display the resulting
waveform.
HINT: Add following lines to your MATLAB program before subplot instructions:
v99=0; %synthesized square wave % add only odd harmonics.
for n=1:2:99
v99 = v99+(4*V/(n*pi))*sin(n*w0*t);
end
subplot(3, 2, 6)
plot(t,v99)
axis([0 .001 -15 15])
The plots are shown below