KEMBAR78
Matlab 3 | PDF | Computers
0% found this document useful (0 votes)
33 views33 pages

Matlab 3

This document provides an overview of function files and graphs in MATLAB. It discusses how to create script and function files, set breakpoints, call user defined functions, overload functions, save data, prompt for user input, use control statements, and plot graphs with multiple curves and in multiple windows. Multidimensional functions and mesh plots are also introduced.

Uploaded by

joe.rahi01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views33 pages

Matlab 3

This document provides an overview of function files and graphs in MATLAB. It discusses how to create script and function files, set breakpoints, call user defined functions, overload functions, save data, prompt for user input, use control statements, and plot graphs with multiple curves and in multiple windows. Multidimensional functions and mesh plots are also introduced.

Uploaded by

joe.rahi01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

LEBANESE AMERICAN UNIVERSITY

School of Engineering
Department of Electrical and Computer Engineering

ELE443 Control System LAB


Fall 2013

Lecture 3: Function files & Graphs


Introduction

 Script files are used to write programs, to save and to run


them using MATLAB commands.
 Script files contain list of MATLAB commands.
 Script are saved in “filename.m”
 Select File menu in the MATLAB toolbar:
 File>New>M-file
 The Editor window will open.

2
Creating Script Files
 Commands in M-files are executed in the order they are listed.
 Script files can be edited and executed many times.
 The program is automatically saved when Run button is pressed.

Run
command

3
Breakpoints

 Breakpoints can be placed in script files for


debugging.
 A breakpoint can be set for every line.
 Place the cursor on the desired line.
 Press F12 or choose Debug>Set/Clear Breakpoint from Editor
toolbar.

 Program stops and Workspace variables are updated


once the program reaches a breakpoint
 Press Continue button to resume the program

4
Example of M-file

5
Function files
 MATLAB has its own set of built-in and toolbox functions
 Example: sin, exp, rand, plot…
 User can write his own function and executes it.
 Function files are written using the Editor Window.
 A function has a name, can have arguments and output
arguments.
 The first line in Function files has the following format:
 function [out1,…,outm] =function-name(arg1,…,argn)
 [out1,…,outm] is an array that returns m variables
 (arg1,…,argn) is a list of n arguments that the function
takes.

6
Function Files

 function[out1,…,outm]=function-name(arg1,…,argn)
 Function-name
 It’s the function name.
 The M-file should have the same name as function-name
 M-files that contain functions are saved as
 Function-name.m
 In order to call a user defined function, function-name.m
should exist in the Current Directory.

7
Example of function files

 Let’s define a function that transforms the polar coordinates


into their corresponding Cartesian coordinates.
 Let’s name the function pol_to_cart.
 This function has 2 input arguments:
 r and theta
 This function has 2 output arguments:
 x and y

8
Example of function files

This function is saved as pol_to_cart.m file

9
Calling User defined functions

>> [x,y]=pol_to_cart(1,pi/6)
x=
0.86603
y=
User defined functions can run in 0.5
Command Window, or in Script Files or
in other Function Files.

10
Function Overloading
 The behavior of a function can be modified depending on the
number of input and output arguments.
 Commands:
 nargin: Number of Input arguments
 nargout: Number of Output arguments
 Example:
function [P]=integral(t,x,flag)
if nargin==2
dt=t(2)-t(1);
P=sum(x)*dt;
elseif nargin==3
dt=t(2)-t(1);
P=cumsum(x).*dt;
end

11
Saving Data
 MATLAB uses its own platform independent file
format for saving data files.
 Files have a “.mat” extension
 save is used to save variables from the workspace to a named
file (by default: matlab.mat if no filename is given)
 save filename – saves entire workspace to filename.mat
 save var1 var2 … filename – saves named variables to filename.mat
 By default save overwrites an existing file of the same name,
use –append to append data to an existing file
 Variables of the same name are always overwritten!!!
 save var1 var2 … filename -append
 Data is recovered using load command
 load filename – loads entire .mat file
 load filename var1 var2 … - loads named vars

12
Prompting for User input

 Theinput function can be used to


prompt the user for numeric or string
input.
>>x = input(‘Enter a value for x’);
>>YourName = input(‘Enter your name’, ‘s’);

13
MatLab Programming

 Program Control Statements:

 Conditional Control (if, switch)


 Loop Control (for, while, continue, break)
 Error Control (try, catch)
 Program Termination (return)

14
Conditional Control (if statement)

clear;
x=-2;
y=10;
if(x<0)
angle=180+atand(y/x)
else
angle=atand(y/x)
end

15
Loop Control (For loop)

 Loop Expression Format:


for index = start:increment:end
statements
end
 Example:
for n = 2:size(x,2)
x(n) = 2 * x(n - 1);
end

 The command break exits a loop

16
Comparison of break and return

 break is used to escape the current loop.


 return is used to escape the current function.

 Remark: an infinite loop can be broken by typing “Ctrl+C”

17
Plotting
 MATLAB is used extensively to plot graphs.
 Different parameters can be modified in MATLAB figures:
 Number of subplots in a figure
 Scale (i.e. Linear or Logarithmic)
 Grid, colors, labels and legends.

 Consider the plotting example:


 t=0:0.0001:3;
 f=exp(-t).*sin(2*pi*10*t);
 plot(t,f), grid
 Note: t and f have the same size

18
Plot command

19
Plotting a function
 The Figure can be edited and labeled such that:

ylabel('F(t)')
xlabel('t')
title('F(t)=e^{-t}.sin(20\pit)')
axis([0,2,-1,1]) % axis([xmin, xmax, ymin, ymax])
legend('F(t)')

20
Plotting a function

21
Plotting 2 curves on the same graph

t=0:0.0001:3;
t=0:0.0001:3; f=exp(-
f=exp(- t).*sin(2*pi*10*t);
t).*sin(2*pi*10*t); g=cos(2*pi*t);
g=cos(2*pi*t); plot(t,f,'r‚)
plot(t,f,'r',t,g,'b--') 2 ways
hold
legend('f(t)','g(t)') plot(t,g,'b--')
legend('f(t)','g(t)')

 Use the help command to get more information on


the plot command.
22
Plotting 2 curves on the same graph

23
Plotting 2 curves in 1 graph window

t=0:1e-4:3;
f=exp(-t).*sin(2*pi*10*t);
g=cos(2*pi*t);
subplot(2,1,1),plot(t,f),grid
subplot(2,1,2),plot(t,g),grid

 subplot(M,N,n) creates an array of M-by-N graphs in a


figure window, where n is the number of a selected graph
in the array.

24
Plotting 2 curves in 1 graph window

25
Plotting 4 curves in 1 graph window

t=0:1e-4:3;
f= exp(-t).*sin(2*pi*10*t);
g= cos(2*pi*t);
y= tan(2*pi*t);
w= sinc(t);
subplot(2,2,1),plot(t, f),grid
subplot(2,2,2),plot(t, g),grid
subplot(2,2,3),plot(t, w),grid
subplot(2,2,4),plot(t, y),grid

26
Plotting 4 curves in 1 graph

27
Plotting curves in different graph windows
 To open a new window and to plot a curve, we use the
command: figure
 Example:

t=0:0.1:10;
y1=sin(2*pi*t);
y2=exp(-0.5*t)*sin(2*pi*t);
plot(t,y1)
figure,
plot(t,y2)

 In this way, the first graph stays in the first window, and a new
window opens and displays the second graph.

28
Plotting curves in different windows

29
Multidimensional Functions
 Consider the function f(x, y):

 f(x,y) is defined over the range:


 -2<x<2
 -3<y<3
 To define the domain of definition of this
multidimensional function, we use the command:
 ndgrid

30
Multidimensional Functions
 Plotting F(x,y) is done by:

x=-2:0.2:2;
y=-3:0.2:3;
[X,Y]=ndgrid(x,y);
f=X.*exp(-X.^2-Y.^2);
mesh(X,Y,f)
 Use the help command to further understand ndgrid
and mesh commands.
 It plots in a 3-Dimensional Space the points whose
coordinates are in matrices X, Y and f.
31
Multidimensional Functions

0.5

0.4

0.3

0.2

0.1

0
f

-0.1

-0.2

-0.3

-0.4

-0.5
3
2
2
1 1.5
1
0 0.5
-1 0
-0.5
-2 -1
-1.5
-3 -2
y
x

32
Looking Forward
 This is the last lecture concerning the basics of MATLAB.
 You are expected to know and understand these three
lectures very well.

 The upcoming three lectures will cover:


 Linear Systems.
 Control System Design Control Systems Design and
Analysis Under MATALB
 Simulink and Filters
Using Simulink to simulate
and observe the behavior of
controlled systems
+
Simulating Butterworth
33 filters

You might also like