Вы находитесь на странице: 1из 9

Outline

6.094
Introduction to Programming in MATLAB (1) Symbolic Math
(2) Simulink
(3) File I/O
Lecture 5: Symbolics, Simulink®, (4) Graphical User Interfaces
File I/O, Building GUIs

Instructor:
Danilo Šüepanoviü

IAP 2010

Symbolic Math Toolbox Symbolic Variables

• Don’t do nasty calculations by hand! • Symbolic variables are a type, like double or char
• Symbolics vs. Numerics
• To make symbolic variables, use sym
Advantages Disadvantages » a=sym('1/3');
» b=sym('4/5');
Symbolic •Analytical solutions •Sometimes can't be » mat=sym([1 2;3 4]);
•Lets you intuit solved ¾ fractions remain as fractions
things about •Can be overly » c=sym('c','positive');
solution form complicated
¾ can add tags to narrow down scope
Numeric •Always get a •Hard to extract a
¾ see help sym for a list of tags
solution deeper understanding
•Can make solutions •Num. methods
accurate sometimes fail • Or use syms
•Easy to code •Can take a while to » syms x y real
compute ¾ shorthand for x=sym('x','real'); y=sym('y','real');
Symbolic Expressions Cleaning up Symbolic Statements

• Multiply, add, divide expressions » pretty(ans)


» d=a*b ¾ makes it look nicer
¾ does 1/3*4/5=4/15;
» collect(3*x+4*y-1/3*x^2-x+3/2*y)
» expand((a-c)^2); ¾ collects terms
¾ multiplies out
» simplify(cos(x)^2+sin(x)^2)
» factor(ans) ¾ simplifies expressions
¾ factors the expression
» subs(‘c^2’,c,5) ans=
» matInv=inv(mat) ¾ Replaces variables with numbers
25

¾ Computes inverse symbolically or expressions. To do multiple substitutions


pass a cell of variable names followed by a cell of values
» subs(‘c^2’,c,x/7) ans=
1/49*x^2

More Symbolic Operations Exercise: Symbolics

• We can do symbolics with matrices too • The equation of a circle of radius r centered at (a,b) is
given by: ( x − a ) + ( y − b ) = r
2 2 2
» mat=sym('[a b;c d]');
• Use solve to solve this equation for x and then for y
» mat2=mat*[1 3;4 -2];
¾ compute the product
» d=det(mat)
¾ compute the determinant
» i=inv(mat)
¾ find the inverse • It’s always annoying to integrate by parts. Use int to do
the following integral symbolically and then compute the
• You can access symbolic matrix elements as before value by substituting 0 for a and 2 for b: b
³ xe dx
x
» i(1,2)
a
Exercise: Symbolics Outline

• The equation of a circle of radius r centered at (a,b) is (1) Symbolic Math


given by: ( x − a ) + ( y − b ) = r
2 2 2

• Use solve to solve this equation for x and then for y (2) Simulink
(3) File I/O
» syms a b r x y
» solve('(x-a)^2+(y-b)^2=r^2','x') (4) Graphical User Interfaces
» solve('(x-a)^2+(y-b)^2=r^2','y')

• It’s always annoying to integrate by parts. Use int to do


the following integral symbolically and then compute the
value by substituting 0 for a and 2 for b: b
³ xe dx
x

a
» Q=int(x*exp(x),a,b)
» subs(Q,{a,b},{0,2})

SIMULINK Getting Started


• Interactive graphical environment
• In MATLAB,
• Block diagram based MATLAB add-on environment
Start Simulink
• Design, simulate, implement, and test control, signal
processing, communications, and other time-varying
systems
Courtesy of The MathWorks, Inc. Used with permission.

• Create a new
Simulink file,
similar to how
you make a new
script

Courtesy of The MathWorks, Inc. Used with permission.


Courtesy of The MathWorks, Inc. Used with permission.
Simulink Library Browser Connections
• The Library Browser contains various blocks that you can • Click on the carat/arrow on the right of the band
put into your model
limited white noise box
• Examine some blocks:
¾ Click on a library: “Sources”
– Drag a block into Simulink: “Band limited white noise” • Drag the line to the scope
¾ Visualize the block by going into “Sinks” ¾ You’ll get a hint saying you can quickly connect
– Drag a “Scope” into Simulink blocks by hitting Ctrl
¾ Connections between lines represent signals

• Click the play button


Courtesy of The MathWorks, Inc. Used with permission.

• Double click on the scope.


¾ This will open up a chart of the variable over the
simulation time
Courtesy of The MathWorks, Inc. Used with permission.

Connections, Block Specification Behind the curtain


• To split connections, hold down ‘Ctrl’ when clicking on a • Go to “Simulation”->”Configuration Parameters”
connection, and drag it to the target block; or drag
at the top menu
backwards from the target block
See ode45? Change the solver type here
• To modify properties of a block, double-click it and fill in
the property values.

Courtesy of The MathWorks, Inc. Used with permission. Courtesy of The MathWorks, Inc. Used with permission.
Exercise: Simulink Exercise: Simulink
• The diagram should look like this. To change the transfer
• Take your white noise signal, and split it into high function parameters, double click the blocks and specify
frequency and low frequency components. Use the the numerator and denominator as polynomials in s
Transfer Function block from Continuous and use these (remember how we defined polynomial vectors before)
transfer functions:
1 0.1s
LP = HP =
0.1s + 1 0.1s + 1
• Hook up scopes to the input and the two outputs
• Send the two outputs to the workspace by using the to
Workspace block from Sink

Courtesy of The MathWorks, Inc. Used with permission.

Exercise: Simulink Toolboxes

• After running the simulation, double-clicking the scopes will • Math


show: ¾ Takes the signal and performs a math operation
Input » Add, subtract, round, multiply, gain, angle
Low pass
• Continuous
¾ Adds differential equations to the system
High Pass » Integrals, Derivatives, Transfer Functions,
State Space
• Discontinuities
¾ Adds nonlinearities to your system
• Discrete
¾ Simulates discrete difference equations
¾ Useful for digital systems

Courtesy of The MathWorks, Inc. Used with permission.


Building systems Outline

• Sources (1) Symbolic Math


» Step input, white noise, custom input, sine
wave, ramp input, (2) Simulink
¾ Provides input to your system (3) File I/O
• Sinks (4) Graphical User Interfaces
» Scope: Outputs to plot
» simout: Outputs to a MATLAB vector on workspace
» MATLAB mat file

Importing Data Importing Data

• MATLAB is a great environment for processing data. If you • With importdata, you can also specify delimiters. For
have a text file with some data: example, for comma separated values, use:
» a=importdata('filename', ', ');
¾ The second argument tells matlab that the tokens of
interest are separated by commas or spaces

• To import data from files on your hard drive, use • importdata is very robust, but sometimes it can have
importdata trouble. To read files with more control, use fscanf (similar
» a=importdata('textFile.txt'); to C/Java), textread, textscan. See help or doc for
information on how to use these functions
¾ a is a struct with data, textdata, and colheaders fields

» x=a.data;
» names=a.colheaders;
Writing Excel Files Reading Excel Files

• MATLAB contains specific functions for reading and writing • Reading excel files is equally easy
Microsoft Excel files • To read from an Excel file, use xlsread
• To write a matrix to an Excel file, use xlswrite » [num,txt,raw]=xlsread('randomNumbers.xls');
» [s,m]=xlswrite('randomNumbers',rand(10,4),... ¾ Reads the first sheet
'Sheet1'); % we specify the sheet name ¾ num contains numbers, txt contains strings, raw is the
• You can also write a cell array if you have mixed data: entire cell array containing everything
» C={'hello','goodbye';10,-2;-3,4}; » [num,txt,raw]=xlsread('randomNumbers.xls',...
» [s,m]=xlswrite('randomNumbers',C,'mixedData'); 'mixedData');
¾ Reads the mixedData sheet
» [num,txt,raw]=xlsread('randomNumbers.xls',-1);
• s and m contain the 'success' and 'message' output of the
write command ¾ Opens the file in an Excel window and lets you click on the
data you want!
• See doc xlswrite for more usage options
• See doc xlsread for even more fancy options

Outline Making GUIs


• It's really easy to make a graphical user interface in
MATLAB
(1) Symbolic Math
• To open the graphical user interface development
(2) Simulink environment, type guide
» guide
(3) File I/O ¾ Select Blank GUI
(4) Graphical User Interfaces

Courtesy of The MathWorks, Inc. Used with permission.


Draw the GUI Change Object Settings
• Select objects from the left, and draw them where you • Double-click on objects to open the Inspector. Here you can
want them change all the object's properties.

Courtesy of The MathWorks, Inc. Used with permission. Courtesy of The MathWorks, Inc. Used with permission.

Save the GUI Add Functionality to MATLAB file


• When you have modified all the properties, you can save • To add functionality to your buttons, add commands to the
the GUI 'Callback' functions in the MATLAB file. For example, when
• MATLAB saves the GUI as a .fig file, and generates an the user clicks the Draw Image button, the
MATLAB file! drawimage_Callback function will be called and executed
• All the data for the GUI is stored in the handles, so use set
and get to get data and change it if necessary
• Any time you change the handles, save it using guidata
» guidata(handles.Figure1,handles);

Courtesy of The MathWorks, Inc. Used with permission. Courtesy of The MathWorks, Inc. Used with permission.
Running the GUI Outline
• To run the GUI, just type its name in the command window
and the GUI will pop up. The debugger is really helpful for
writing GUIs because it lets you see inside the GUI (1) Symbolic Math
(2) Simulink
(3) File I/O
(4) Graphical User Interfaces

Now you know EVERYTHING!

Courtesy of The MathWorks, Inc. Used with permission.

Вам также может понравиться