- Generals
- M-files
- Command
- Matrix
- Figure
- Symbolic Math Toolbox
- Solving Equations
- Simulink
Generals
- MATLAB is case-sensitive in the names of commands, functions, and variables.
- statement continued to the next line : three or more periods followed by a carriage return
- several statements can be placed on a single line if separated by commas or semicolons
- help cedit, type cedit : CEDIT Set command line editor keys.
- Text strings are entered into MATLAB surrounded by single quotes. For example,
s = 'Is this a test?';disp(s);
- the system command notepad accesses your editor, the MATLAB command
will let you edit the file named rotate.m using notepad.
>> !notepad rotate.m
- dir will list the contents of the current directory while the command
what will list only the M-files in the directory. pwd, cd commands work, too.
- The MATLAB function clock gives the current time accurate to a hundreth of a second (see help clock).
Given two such times t1 and t2, etime(t2,t1) gives the elapsed time from t1 to t2.
One can, for example, measure the time required to solve a given linear system A x = b using Gaussian elimination
as follows: t = clock; x = A\b; time = etime(clock,t)
M-file
- script file consists of a sequence of normal MATLAB statements. If the file has the filename,
say, rotate.m, then the MATLAB command rotate will cause the statements in the file to be executed.
- function file : The first line declares the function name, input arguments, and output arguments.
The first comments will come out by typing "help function_name".
- run rotate.m file
>>rotate
edit rotate.m file
>>!rotate.m
Command
- save, load : invoking the command save before exiting causes all variables to be written
to a non-human-readable diskfile named matlab.mat.
When one later reenters MATLAB, the command load will restore the workspace to its former state.
function format
file_name=['userdata_a' num2str(alpha) '_b' num2str(alpha) '.mat'];
save(file_name, 'user');
load (file_name);
- for loop : "x = []; for i = 3:-1:1, x=[x,i^2], end" makes x = [9 4 1]
- any : If A is a vector, any(A) returns logical 1 (true) if any of the elements of A is a nonzero number
or is logical 1 (true), and returns logical 0 (false) if all the elements are zero.
If A is a matrix, any(A) treats the columns of A as vectors, returning a row vector of logical 1's and 0's.
Matrix
- . : *, ^, \, and /, can be made to operate entry-wise by preceding them by a period.
For example, either [1,2,3,4].*[1,2,3,4] or [1,2,3,4].\^2 will yield [1,4,9,16].
- Matrices can be built from blocks. For example, if A is a 3-by-3 matrix, then
B = [A, zeros(3,2); zeros(2,3), eye(2)] will build a certain 5-by-5 matrix.
- A(1:4,3) is the column vector consisting of the first four entries of the third column of A.
A colon by itself denotes an entire row or column:
A(:,3) is the third column of A
- empty matrix : x=[] has nothing for its element.
- eig : eigenvalues and eigenvectors
- inv : inverse
- [A;B] : attach B to A as additional rows
Figure
- figure : creates a new figure object using default property values.
- figure properties
- set axis range: axis([0 2000 0 2200]);
- set axis tick: set(gca,'XTick',0:500:2000);
- set transparent background color: set(gca,'color','none'), Edit->Copy figure->paste to ppt or word
- save figures in fig format: saveas(gcf, 'test.fig');
- save figures in other formats, jpeg: print(gcf, '-djpeg', '-r90', 'test.jpg');
- gcf : returns the handle to the current figure and is useful as an argument to the set and get commands.
- subplot : h = subplot(m,n,p) or subplot(mnp) breaks the figure window into an m-by-n matrix of small axes,
selects the pth axes object for the current plot, and returns the axes handle.
Symbolic Math Toolbox
- syms x %±âÈ£ ¼öÇÐÀÇ º¯¼ö ¼±¾ð
y = 3*x^2+5*sin(x) -10 %±âÈ£½ÄÀÇ Á¤ÀÇ
y1 = diff(y,x) %y¸¦ x·Î ¹ÌºÐ
y2 = diff(y,x,2) %y¸¦ x·Î 2¹ø ¹ÌºÐ
y3 = int(y,x) %y¸¦ x·Î ÀûºÐ
y4 = int(y,x,0,1) %y¸¦ x·Î 0,1 ±¸°£¿¡¼ ÀûºÐ
y5 = int(y,x,0,pi/2) %y¸¦ x·Î 0,pi/2 ±¸°£¿¡¼ ÀûºÐ
Solving Equations
- ODE (Ordinary Differential Equation)
- reference (Korean)
- writing Euler, Runge-Kutta function
- using MATLAB function such as ode23, ode45 etc
- using symbolic math toolbox (dsolve)
- using simulink state space
- using simulink integrator (1/s)
Example : y' + y = 0; y(0) = 0.1
odetosolve.m
function [y2]=odetosolve(t,y)
y2=-y;
command line
options = odeset('RelTol',1e-4,'AbsTol',1e-5,'MaxStep',.05);
[t,Y] = ode45(@odetosolve,[0 20],[.1],options);
plot(t,Y)
- Polynomials
Example : p(x) = x^3 - 2x^2 - x + 2
p=[1 -2 -1 2];
x=[-3:0.1:3];
y=polyval(p,x);
plot(x,y)
grid on
roots(p)
- Laplace transform
Example :
f=sym('sin(t)');
F=laplace(f);
F;
G=sym('0.1/(0.1*s+1)');
g=ilaplace(G);
g;
Simulink
From MIT Tutorial : F-8 longitudinal control system
- Start Simulink. Type simulink at the Matlab prompt.
- Start a new model. In the File menu of the Simulink library window, click "New"
and then "Model".
- Add a step function to your model. In the Simulinklibrary window, double click
the icon labeled "Sources". Drag the step function to the model window.
- Add three transfer functions to your model. In the library window, double click
the icon labeled "Continuous". Drag transfer function to the model window 3 times.
- Add a scope to the model. A scope allows us to examine the output of the system.
Double clickthe icon labeled "sinks".
- Add a sum operator to the model. The sum is in the "Math
Operations" library.
- Change the sum operator so that it performs subtraction rather than addition.
Double clickon the sum operator. A new window will appear with the title
"BlockP arameters:Sum" Change the list of signs to "|+-" and click"OK".
- Change the name of the transfer function to "Controller" , "Aircraft" and the third to
"Sensor". Click on the name, "Transfer Fcn", delete the existing name, and type Controller.
- Connect the step function to the summer. The small triangle on the right side
of the step function represents the output. Clickon the small triangle and hold
the mouse down. Hold the cursor over the input triangle to the
sum icon¡¯s positive input and release the mouse button.
- Connect the output of the sum to the input of the controller, the output of the
controller to the input of the aircraft, and the output of the aircraft to the input
of the scope.
- Flip the orientation of the sensor. Select the sensor blockb y clicking on it. In
the "Format" menu, select "Flip Block."
- Create a feedbacklo op. Hold down the control key and click somewhere on
the arrow between the aircraft blockand the scope. A dashed line will appear;
connect it to the input of the sensor. Connect the output of the sensor to the
negative input of the sum operator.
- Change the aircraft transfer function.Double clickthe aircraft icon.
The numerator and denominator have to be set
using Matlab matrix notation. We will be using only single-input
single-output (SISO) transfer functions, so both the numerator and denominator
will be row vectors. The vectors represent the coefficients of a polynomial in
order of decreasing exponent of s. So, for our denominator 976s2 + 11.25s + 1,
the vector is [976 11.25 1]. Enter [976 11.25 1] in the "denominator" field.
Enter 964 in the numerator field.
- We will model the sensor as having a 0.01 second lag in reporting the speed.
A simple lag of &tau seconds is represented by the transfer function H(s) = 1/(&tau s+1).
Change the transfer function of the sensor to 1 / ( 0.01s+1 ).
- Set the simulation to run for 30 seconds. On the "Simulation" menu,
click "Configuration Parameters". Fill in the "stop time" field with 30.
- Open the scope window. Double clickthe scope icon.
- Run the simulation. In the "Simulation" menu of the model window, click
"Start".
- Examine the output of the simulation. Return to the scope window. To get the
scope to show the most appropriate scale, clickthe binocular icon in the scope
toolbar. Notice how the speed varies in response to a step input.
- Try a different controller. Change the transfer function of the controller to 2.5s
0.15s+1. Note that to get 2.5s as the numerator, the vector is [2.5 0].
- Re-run the simulation and observe the output of the scope. Has the new controller
improved the output?
- Now we would like to plot the output in the standard Matlab plot interface.
From the "Sinks" library, add a new "To Workspace" icon. Connect it to the
output signal. Double clickthe "To Workspace" icon and change the "save format" to "array"
- Run the simulation.
- Return to the Matlab command window.
Type who to see a list of variables available to you. Note that there are two new
variables: tout and simout. These are the time and output signals from the
simulation.
- Plot the output signal agains the time.
- Save the Simulink model. In the "File" menu of the model window, click on Save.
Browse to an appropriate directory and save the model as "F8.mdl".