











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Page 1 of 11: Exp. 1 & 2
Objectives:
๏ผ Familiarize with the MATLAB environment. ๏ผ Learn basic syntax and script writing. ๏ผ Explore built-in functions and help features. ๏ผ Perform basic computations and plotting.
Introduction:
MATLAB works with three types of windows on your computer screen. These are the Command window, the Figure window and the Editor window. The Figure window only pops up whenever you plot something. The Editor window is used for writing and editing MATLAB programs (called M-files) and can be invoked in Windows from the pull-down menu after selecting File | New | M-file.
๏ Create a folder of your group name in C drive. ๏ Open MATLAB 6.5. In "current directory'' select your folder. ๏ From the "file" menu start a new m-file. Always write your program in m-file & save it. The file/function/variable name must start with a letter and cannot contain space. The name can be a mix of letters, digits, and underscores. ( e.g., vector_A, but not vector-A (since "-" is a reserved char). must not be longer than 31 characters. ๏ You can also write any command or program in "command window". ๏ Function โclear allโ removes all variables, globals, functions and MEX links. Write clear all at the beginning of your m-file. ๏ Write "clc" in command window to clear the command window, "clg" to clear graphics window. ๏ MATLAB is case sensitive. e.g., NAME, Name, name are 3 distinct variables. ๏ Write "help function_name" in command window after the ">>" sign to see the description of the function. For example, type "help sin" to know about sin functions in MATLAB. You can also use help in the menubar. ๏ Explore MATLABโs lookfor and help capability by trying the following:
lookfor keywords help help plot help ops help arith
Page 2 of 11: Exp. 1 & 2
Special Characters:
There are a number of special reserved characters used in MATLAB for various purposes. Some of these are used as arithmetic operators, namely, +, โ, *, / and . While others perform a multitude of purposes: ๏ท % -- anything after % (and until end of line) is treated as comments, e.g.,
x = 1:2:9; % x = [1 3 5 7 9]; ๏ท ; -- delimits statements; suppresses screen output, e.g., x = 1:2:9; y = 2:10; % two statements on the same line ๏ท ... -- statement continuation, e.g., x = [ 1 3 5 ... 7 9]; % x = [1 3 5 7 9] splitted into 2 lines ๏ท : -- range delimiter, e.g., x = [1:2:9]; % x=[1,3,5,7,9] ๏ท '-- matrix transposition, e.g., x = [1:2:9]'; % x changed from row vector to column vector if the vector/matrix is complex, โ ' โ results in complex conjugation and matrix transposition. ๏ท , -- command delimiter, e.g., x = [1:2:9], y = [1:9] % two statements on the same line ๏ท : -- precede an arithmetic operator to perform an elemental operation, instead of matrix operation, e.g., x = 3:3: x = 3 6 9
y = 3*ones(3,1)' y = 3 3 3
z = x./y z = 1 2 3
๏ท * -- "wild card", e.g.,
clear A* % clears all variables that start with A.
Note that many of these characters have multiple functionalities (function overloading) depending on the context, e.g., "*" is used for scalar multiply, matrix multiply and "wild card" as seen above.
Arithmetic Operations & Built in functions:
Example 1. Find ๐ฆ เต ๐๐ฅ๐แบ 5 เฌถ/3 ๐๐แป
Page 4 of 11: Exp. 1 & 2
z =x./y
๏ Some operations are performed on square matrices only.
๏ + โ * / ^ (algebraic/matrix definitions) .+ .โ .* ./ .^ (element by element operation) Additionally,
"'" performs matrix transposition; when applied to a complex matrix, it includes elemental conjugations followed by a matrix transposition \ and .\ perform matrix and elemental left division.
Exercise 3. A=[2 3; 4 5]; B=[3 4; 6 7]; Find A+B, AB, A.B, A/B, A\B, A.^2, A./B
Graphics:
MATLAB can produce 2 and 3 dimensional plots_. MATLAB_ is an interactive environment in which you can program as well as visualize your computations. It includes a set of high-level graphical functions for:
๏ท Line plots (plot, plot3, polar) ๏ท Bar graphs (bar, barh, bar3, bar3h, hist, rose, pie, pie3) ๏ท Surface plots (surf, surfc) ๏ท Mesh plots (mesh, meshc, meshgrid) ๏ท Contour plots (contour, contourc, contourf) ๏ท Animation (moviein, movie)
Example 2. x=0:0.1:pi; y=cos(x); plot(y); plot(x,cos(x),'r'); plot(x,y,x,y.^2);
Example 3. x=0:0.1:pi; y=cos(x); plot(y); hold on plot(x,cos(x),'r');
Page 5 of 11: Exp. 1 & 2
Example 4. x=linspace(0,7); y=exp(x); subplot(2,1,1), plot(x,y); subplot(2,1,2), semilogy(x,y);
Example 5. x = magic(3); bar(x); grid
Exercise 4. Plot the following functions in the same window y1=sin x, y2=sin 2x, y3=sin 3x, y4=sin 4x where x varies from 0 to pi.
Loops & Conditionals:
MATLAB has the following flow control constructs: ๏ท if statements ๏ท switch statements ๏ท for loops ๏ท while loops ๏ท break statements
-1 0 5 10 15 20 25 30 35
-0.
-0.
-0.
-0.
0
1
(^00 1 2 3 4 5 6 )
500
1000
(^10 00 1 2 3 4 5 6 )
10 2
(^01 2 )
1
2
3
4
5
6
7
8
9
Page 7 of 11: Exp. 1 & 2
If the matrices are all of the same size (as is the case here), then the above can be more succinctly written as
A = B + C;
For sufficiently large matrix operations, this latter method is vastly superior in performance.
Example 9. ( break statement )
The break statement lets you exit early from a for or a while loop:
x=โ10; while x< x=x+2; if x == โ 2 break; end end
What is the value of x after execution of the above loop?
Relational Operators
Symbol Meaning <= Less than equal < Less than
= Greater than equal Greater than == Equal ห = Not equal
Logical Operators
Symbol Meaning & AND | OR ห NOT
Defining functions:
๏ In MATLAB there is scope for user-defined functions.
Suggestion: Since MATLAB distinguishes one function from the next by their file names, name files the same as function names to avoid confusion. Use only lowercase letter to be consistent with MATLAB's convention.
๏ To define a function, start a new M-file. The first line of M-file should be
function variable_name=function_name(parameters);
Page 8 of 11: Exp. 1 & 2
variable_name is the name of variable whose value will be returned. function_name is user defined according to the rules stated previously.
Example 10. function y=cal_pow(x); y=1+x^2; end
๏ Save this function as cal_pow. ๏ Start another new M-file .This will be our main file. ๏ Write the following commands and run the file:
clear all; x=0:1:3; t=length(x); for i=1:t val(i)=cal_pow(x(i)); end plot(x,val);
Do the following:
Exercise 5. Write a program to compute the variance of an array x. The variance ๐ is defined to be:
๐ เต
เฏ
เฏเญเฌต
where ๐ฅฬ is the average of the array x. a) For x , use all the integers from 1 to 1000. b) Create x by built in function rand.
Exercise 6. Define the matrices
A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ] C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ] y=[ 4 3 2 1 ]'
Note the transpose ' on the y-vector which makes y a column vector. a) Compute AB and BA. Is matrix multiplication commutative? b) Compute AC. Why do you get an error message? c) Solve the following system of equations:
(^10) 0.5 1 1.5 2 2.5 3
2
3
4
5
6
7
8
9
10
Page 10 of 11: Exp. 1 & 2
Empirical formulas for finding ๐ต and ๐ท : If the transition width (normalized) and stop band attenuation are ฮ๐ and ๐ฟ respectively then ๐ด เต เต20logแบ๐ฟแป
Here ๐ should be of integer type. See what does built in MATLAB function ๐๐๐๐๐ do.
The impulse response, โ (^) เฎฝ แบ๐แป for low pass filter is given below:
sin แแ๐ เต
Design a linear phase low pass FIR filter using the Kaiser window to satisfy the following amplitude response specifications:
Sampling frequency, ๐นเฏฆ 1 kHz
Ideal cutoff frequency, ๐นเฏ 250 Hz
Transition width, ฮ๐น 50 Hz
Pass band ripple, ๐ฟ 0.
a) Write M-file to plot impulse response โแบ๐แป เต โเฎฝ แบ๐แป โ ๐คแบ๐แป vs. ๐ of the filter.
b) Plot magnitude (in dB) and phase response (in degrees) vs. analog frequency. To do this necessary codes are given at the end of this sessional sheet.
N.B.
๐เฏ เต
Follow the algorithm below to write the codes for Kaiser Window FIR low pass filter impulse response:
Page 11 of 11: Exp. 1 & 2
เฌถ เต
เฌต/เฌถ , ๐คแบ๐แป and โเฎฝ แบ๐แป. Now calculate โแบ๐แป
%%%finding filter magnitude and phase response in frequency domain Page 11 of 11:Expt 1 & 2 NFFT=1024; hfir=fft(hFIR,NFFT); %%%%% hFIR=calculated h(n) earlier k=0:NFFTโ1; %waxis=2pi1/NFFT.k; faxis=1/NFFT.k; magresdB=20log10(abs(hfir)); subplot(312),plot(faxis(1:NFFT/2+1)Fs,magresdB(1:NFFT/2+1)) grid on; xlabel('Frequency (Hz)') ylabel('Magnitude response (dB)') subplot(313),plot(faxis(1:NFFT/2+1)Fs,(unwrap(angle(hfir(1:NFFT/2+1)))) 0/pi) grid on; xlabel('Frequency (Hz)') ylabel('Phase response (degrees)') %%%finding magnitude and phase response by freqz built in function % figure % freqz(hFIR,1);
Reference Books:
Revised by,
Md. Samrat
(April 2025)
Page 2 of 7: Exp. 3
For example, suppose we have a table like this, which gives some values of an unknown function ๐. The data are given in the table:
Table 1
๐ ๐แบ๐แป 0 0 1 0. 2 0. 3 0. 4 -0. 5 -0. 6 -0.
The plot can be shown as: .
What value does the function have at, say, x = 2.5? Interpolation answers questions like this.
One of the simplest methods is linear interpolation. Consider the above example of determining ๐แบ2.5แป
. We join the data points by linear interpolation and get the following plot:
Page 3 of 7: Exp. 3
Noe we can get ๐แบ2.5แป. Since 2.5 is midway between 2 and 3 , it is reasonable to take ๐แบ2.5แป midway between ๐แบ 2 แป เต 0.9093 and ๐แบ 3 แป เต 0.1411 , which yields 0.5252.
Generally, linear interpolation takes two data points, say ( x a , ya ) and ( x b , y b ), and the interpolant is given by
This formula can be interpreted as a weighted mean.
Linear interpolation is quick and easy, but it is not very precise.
Exercise 2. Plot the curve corresponding to table1 using linear interpolation.
Exercise 3. y = sin(x) ; x = 0:10; x(i) = 0 : 0.25 :10; Construct the interpolant y and plot.
Polynomial interpolation is a generalization of linear interpolation. Note that the linear interpolant is a linear function. We now replace this interpolant with a polynomial of a higher degree.
Consider again the problem given above. The following sixth-degree polynomial goes through all seven points:
Substituting x = 2.5, we find that f (2.5) = 0.5965.
Page 5 of 7: Exp. 3
Above figure shows Lagrange interpolation of the bell curve function ๐แบ๐ฅแป เต ๐เฌฟ เฏซ^
เฐฎ for three different polynomial degrees: N = 5, 9, and 15. as N increases, the interpolation fits the central region better. However, for N = 15 , significant oscillations appear near the edges (Rungeโs phenomenon).
Algorithm for the Lagrange Polynomial: To construct the Lagrange polynomial
of degree n, based on the n+1 points for. The Lagrange coefficient polynomials for degree n are:
for.
So, for a given x and a set of (N+1) data pairs, ( x (^) i , f (^) i ), i = 0, 1, .................N:
1 Read N, x(0:N), f(0:N), x_val 2 sum โ 0 3 for i = 0 to N do 4 p โ 1 5 for j = 0 to N do 6 if j โ i then 7 p โ p * (x_val - x(j)) / (x(i) - x(j)) 8 endif 9 endfor
-5 -4 -3 -2 -1 0 1 2 3 4 5 x
-0.
0
1
2
2.5 Lagrange Interpolation for Bell Curve f(x) = e^
-x 2
Original Function e -x^2 Lagrange N= Points N= Lagrange N= Points N= Lagrange N= Points N=
Page 6 of 7: Exp. 3
10 sum โ sum + p * f(i) 11 endfor 12 result โ sum 13 Write result
Exercise 4. Write a MATLAB program implementing Lagrange Polynomial.
To construct and evaluate the Newton polynomial of degree ๏ฃ n that passes through the n ๏ซ 1 points ( x (^) k , y (^) k ) ๏ฝ ( x (^) k , f ( x (^) k )) for k ๏ฝ 0,1,โฆโฆ n :
This polynomial P n (x) is said to be a Newton Polynomial with n centers x 0 , x 1 to x n-1. It involves sums
of products of linear factors upto
a (^) n (x-x 0 )(x-x 1 )(x-x 2 )โฆ ................................ (x-xn-1 )
Construction for n = 1.
Use the two points.
Algorithm Newton polynomial:
To construct and evaluate the Newton polynomial of degree ๏ฃ n that passes through the n ๏ซ 1 points ( x (^) k , y (^) k ) ๏ฝ ( xk , f ( x (^) k )) , for k ๏ฝ 0,1,โฆโฆโฆ n :
where
So for a given set of (N+1) data pairs, ( x (^) k ,y (^) k ), k = 0 to N