Professional Documents
Culture Documents
EXPERIMENT NO: 02
Example 1
Voltage Divider: In an electrical circuit if several resistors are connected in series, the voltage across each of them
is given by the voltage divider rule:
Rn
Vn = V
Req s
Where, V n and Rn are the voltage across nth resistor, Rn .
1
After executing the script file in the command window ,the output is as follows:
while
x = -10;
while x < 0
x = x + 1;
end
Question: What is the value of x after execution of the above code?
for
x = 0;
for i = 1:10
x = x + 1;
2
end
Question: What is the value of x after executing the above code?
switch
x = 10;
switch x
case 5
disp(‘x is 5’)
case 6
disp(‘x is 6’)
case 8
disp(‘x is 8’)
case 10
disp(‘x is 10’)
otherwise
disp(‘invalid choice’)
end
Question: What is the output shown after executing the above code?
break
The break statement lets you exit early from a for or while loop:
x = -10;
while x < 0
x = x + 2;
if x == -2
break;
end
end
Question: What is the value of x after executing the above code?
3
Example 2
Write a script file to sum the series 1 + 2 + 3 + ⋯⋯⋯ + n , n taken from user.
Solution:
4
Writing User-defined Functions
A function file starts with a line declaring the function with its name, input arguments and outputs. Then, follows the
statements required to produce the outputs from the inputs.
Example: Suppose, we want to calculate average of some numbers. For this purpose, let us create a function named
average.m. The steps are:
(i) Open a new m-file from editor.
(ii) Write the following code as shown in the figure:
Body of a function
The function body contains the computer program (code) that actually performs the computation. The code can use
all MATLAB programming features. This includes calculations, assignments, any built-in or another user-defined
functions, flow control etc. You can now understand the function as shown in Fig. 2.6.
5
How to use a user-defined function
We can call a function from the command window as show in the following Fig. 2.7.
Example 3
Write a function that takes radius as input and find the area and perimeter of a circle.
he function is defined as follows:
Solution: T
6
plot command
The plot command is used to create two-dimensional plots. The simplest form of the command is:
plot(x,y)
lot command to draw simple curve.
Example: p
Line Specifiers
Line specifiers are optional and can be used to define the style and color of the line and type of the markers (if the
markers are used).
Line-color Specifiers
Marker-type Specifiers
Marker type specifiers are used to define the type of the markers (when markers are used).
Marker-type Specifier
Plus Sign +
Circle o
Asterisk *
Point .
Square s
7
Diamond d
Five-point Star p
Six-point Star H
Some Examples
Command Meaning
plot(x, y) A blue solid line connecting the points with no markers (default)
plot(x, y, ’r’) A red solid line connecting the points.
plot(x, y, ’--y’) A yellow dashed line connecting the lines
plot(x, y, ’*’) The points are marked with * (no line between points)
A green dotted line connecting the points that are marked with
plot(x, y, ’g:d’)
diamond markers.
Example 4
Suppose the following given data is used to create vectors
that are then used in the plot command.
Year 2002 2003 2004 2005 2006 2007 2008
Sales(millions) 8 12 20 22 18 24 27
To plot this data, the following codes are written in the
command window:
8
The output figure is the same as above.
Example 5
Plot the function y = 3x3 − 26x + 6 and its first and second derivatives for − 2≤x≤4 , all in the same plot.
Solution:
x = -2:0.01:4;
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;
9
title(‘string’) To give a title to your figure
text(x, y, ’string’)
To place a text anywhere on the figure.
gtext(‘string’)
legend(‘string1’, ’string2’) To place a legend on the plot
Use of stem
The function stem is used to plot discrete-time
functions (in contrast to the plot function, which is
used to plot continuous-time functions).
Example 6
Observe the following code:
>> x = 0:1:10;
>> y = sin(x);
>> stem(x, y)
10
11
Example 7
The figure shows a simple high-pass filter consisting of a resistor and a capacitor. The ratio of the output voltage
V o to the input V i is given by:
Vo j2πf RC
Vi
= 1+j2πf RC
12
REPORT
[Submit in the next session]
Q1. Write MATLAB script to plot sin sin 2x and its derivative on the same plot in two different colors and in three
different ways.
Q2. A resistor of value R = 4 Ω and an inductor of value L = 1.3 H are connected in series to a voltage source, as
shown in the figure (a).
When the voltage source applies a rectangular voltage pulse with an amplitude of V = 12 V and a duration of
0.5 s , as shown in figure (b). The current i (t) is given by:
Q3. The figure shows a simple low-pass filter consisting of a resistor and a capacitor.
n
Assume R = n kΩ and C = 20 μF , where n is the last two digits of your student ID. Calculate the amplitude
and phase response of this filter as a function of frequency and plot them in a single figure using subplot.
V 1
[Hint:Gain of a low-pass filter is given by: V o = 1+jωRC ].
i
Reference Books:
(1) “Duane Hanselman, Bruce Littlefield “, Mastering MATLAB 7
(2) “Amos Gilat”, MATLAB An introduction with Applications
Web resource: www.mathworks.com
13
14