Professional Documents
Culture Documents
(EL-223)
LABORATORY MANUAL
Spring 2020
________________________________________________________________________________________________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD
Description:
M-file:
Figure 2.1
You will be presented with the MATLAB Editor/Debugger screen as shown in “Fig 2.2”
“Fig 2.2”
• Here you will type your code, can make changes, etc.
• Once you are done with typing, click on File, in the MATLAB Editor/Debugger screen and
select Save As…
Script Files:
• Script files do not take the input arguments or return the output arguments
• Example (plotxy.m)
x = [1 2 3; 4 5 6]
y = [2 3 4; 6 7 8]
plot(x,y)
Function Files:
• The function files may take input arguments or return output arguments.
• Function Definition
• The first line of a function m-file must be of the following form
• Following the ‘function’ keyword , the (optional) output parameters are enclosed in square
brackets [ ]
• If the function has no output_parameter_list the square brackets are omitted and a single
keyword is used. In that case the first line of a function m-file must be of the following form
• The function_name is a character string that will be used to call the function
• The function_name must also be the same as the file name (without the ``.m'') in which the
function is stored Example:
• Function Calling: Function can either be called from “ M- File” or a “Command window”.
For above example function will be called in a following way:
function y =mean(x)
Example:
Function to compute the length of the hypotenuse of right triangle given the lengths of the
other two sides
a - The length of one side
b - The length of the other side
c - The length of the hypotenuse
function c =
hyp(a,b) c =
sqrt(a^2 + b^2) ;
end
• Function calling
C = hyp(1,2)
Input/Output Functions:
Input Function:
• Syntax : Prompt for user input
• Format : input(‘String to display’) Description:
Input function is used to get data from user and prompts until data has been entered.
• Example;
>> marks = input (‘Enter total marks = ’)
Enter total marks = 5
marks =
5
Disp Function:
• Syntax : Display array
• Format : disp(variable_name), disp(‘String to display’) Description:
Displays the array, without printing the array name. In all other ways it's the same as
leaving the semicolon off an expression except that empty arrays don't display.
Example;
>> disp(‘ hello ’)
>> a=[1 3 5];
>> disp(a)
Exercise: Clearly observe the syntax of each code and explain what is being done by given
code?
If block if (n <= 0)
if (<condition>) disp('n is negative or zero');
<body> elseif (rem(n,2)==0)
else if disp('n is
<body> even'); else
else disp('n is
<body> odd'); end
end
Code Explanation:
Code Explanation:
plot (x,y)
• where x is a vector (one dimensional array), and y is a vector. Both vectors must have the
same number of elements.
• The plot command creates a single curve with the x values on the abscissa (horizontal axis)
and the y values on the ordinate (vertical axis).
• The curve is made from segments of lines that connect the points that are defined by the x
and y coordinates of the elements in the two vectors.
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
• The code can be done in the Command Window, or by writing and then running a script
file.
• Once the plot command is executed, the Figure Window must open with the following plot
as shown in “Fig 2.3”
“Fig 2.3”
plot(x,y,’line specifiers’)
Color Specifiers:
• The specifiers are optional. This means that none, one, two, or all the three can be included
in a command.
Example:
• plot(x,y) A solid blue line connects the points with no markers.
• plot(x,y,’*’) The points are marked with * (no line between the points.)
• plot(x,y,’g:d’) A green dotted line connects the points which are marked with
diamond markers.
“Fig 2.4”
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves (when several curves are
in one plot). The location of the legend is specified by the mouse.
text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to the plot axes.
“Fig 2.5”
Three typical ways to display multiple curves in MATLAB (other combinations are possible…)
One figure contains one plot that contains multiple curves o Requires
the use of the command “hold” (see MATLAB help)
One figure contains multiple plots, each plot containing one curve o
Requires the use of the command “subplot”
Example:
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z)
grid on
Or
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,’b’)
hold on
Plot(x,z,’g’)
hold off
grid on
Subplots:
• Subplot divides the current figure into rectangular panes that are numbered row wise.
• Syntax:
subplot(rows,cols,index)
“Fig 2.6”
Example of Subplot:
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
subplot(1,2,1);
plot(x,y)
subplot(1,2,2)
plot(x,z)
grid on
“Fig 2.7”
Run File
After setting breakpoints, run the file from the Command Window or the Editor. Running the file
produces these results:
• MATLAB displays the current workspace in the Function Call Stack, on the Editor tab in
the Debug section.
Pause a Running File
To pause the execution of a program while it is running, go to the Editor tab and click
the Pause button. MATLAB pauses execution at the next executable line, and the Pause button
changes to a Continue button. To continue execution, press the Continue button.
Pausing is useful if you want to check on the progress of a long running program to ensure that it is
running as expected.
Find and Fix a Problem
While your code is paused, you can view or change the values of variables, or you can modify the
code.
View or Change Variable While Debugging
View the value of a variable while debugging to see whether a line of code has produced the expected
result or not. To do this, position your mouse pointer to the left of the variable. The current value of
the variable appears in a data tip.
The data tip stays in view until you move the pointer. If you have trouble getting the data tip to
appear, click the line containing the variable, and then move the pointer next to the variable. For
more information, see Examine Values While Debugging.
You can change the value of a variable while debugging to see if the new value produces expected
results. With the program paused, assign a new value to the variable in the Command Window,
Workspace browser, or Variables Editor. Then, continue running or stepping through the program.
For example, here MATLAB is paused inside a for loop where n = 2:
EXERCISES:
Q1. Check the functionality of the following and write in your words what each command does in
MATLAB:
ceil()
floor()
round()
fix()
iseven()
isvector().
Isscalar().
Ischar().
audiowrite()
audioread()
b)
A= [1 2; 3 4];
C= [2 4]; B=C'*A
c)
C= [2 4]; inv(C)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
d)
x=-1:0.01:1;
y=0:0.1:1000;
plot(x,y)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
a)
2x=4;
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________