![]() |
| Director: Prof. Andreas A. Linninger |
|
|
||||||||||||||||||||||||||||||||||||||||||
Introduction to MATLABDeveloped by Libin Zhang under the guidance of Prof. A. Linninger Laboratory
for Product and Process Design,
|
|||||||||||||||||||||||||||||||||||||||||||
|
|
These are the MATLAB initial value problem solvers. The table lists the kind of problem
you can solve with each solver, and the method each solver uses.
|
Solver |
Solves These Kinds of Problems |
Method |
|
ode45 |
Nonstiff differential equations |
Runge-Kutta |
|
ode23 |
Nonstiff differential equations |
Runge-Kutta |
|
ode113 |
Nonstiff differential equations |
Adams |
|
ode15s |
Stiff differential equations and DAEs |
NDFs (BDFs) |
dy = zeros(2,1); % a
column vector
dy(1) = y(2);
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1);
[T,Y] = ode15s(@vdp1000,[0 3000],[2 0]);
|
|
The document you are currently reading may look like an ordinary Microsoft Word® file, but there's one important difference. It's been enabled to transmit commands in an input cell directly to MATLAB and have the output appear immediately in the document in an output cell. Such a document is called an M-book. In an M-book, if you want to redo a command, you simply edit the cell in which it appears and re-evaluate the cell. To convert a line of text in an M-book into an input cell, you can either hit CONTROL-RETURN while the cursor is on that line (this not only creates a one-line cell but evaluates it immediately), or else highlight the text and type ALT-N-I, or else use the Notebook menu at the top of the Word window and click on Define Input Cell. (You need one of the last two methods if you want to create a multi-line input cell.) Input cells are identified by black surrounding brackets and the fact that they are set in green Courier type. Here is a typical input cell:
factor(x^2-4)
ans =
(x-2)*(x+2)
Try evaluating it by positioning the cursor in the cell and hitting CONTROL-RETURN. Note MATLAB creates a blue output cell in response. In this output cell you see exactly what MATLAB would respond had you entered your command in the Command Window.
The easiest way to plot a graph in MATLAB is with the command Plot. What happens to the output from a graphics command such as this depends on whether you are working from Command Window or in an M-Book, so try it both ways. If you execute the command from the Command Window (or change the Notebook Options… in the Notebook menu to turn off "Embed Figures in M-Book"), the output will pop up in a separate Figure Window. On the other hand, in an M-Book with "Embed Figures in M-Book" turned on (that's the default), the figure will appear just below the command that produced it. Go ahead and try evaluating:
x = -pi:pi/10:pi; %Prepare your
date
y = sin(x);
%Prepare your date
z = cos(x);
plot(x,y,'--rs',x,z,'-g*')
|
|
The command hold on sets a toggle switch so that output from the next plotting command is superimposed on the existing one. The command hold off turns this off again. Thus to find the solutions of the equation sin(x) = x/2, you can superimpose the plots of sin(x) and of x/2 as follows:
plot(x,x/2); hold off
|
|
Note that you can separate commands on the same line with semicolons (or commas), though a semicolon will also suppress the printed output (not the graphical output) of a command. Another important command for modifying plots is the axis command, which enables you to modify the scales on the axes. For example, axis equal makes the scales on the horizontal and vertical axes the same.
xlabel('x');
ylabel('sin(x)');
|
|
The 3-D analog of the plot function is plot3. If x, y,
and z are three vectors of the same length,
plot3(x,y,z) generates a line in 3-D through the points whose coordinates are the elements of x, y, and z and then produces a 2-D projection of that line on the screen. For example, these statements produce a helix.
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
axis square; grid on
|
|
![]() |
![]() |
There are eight flow control statements in MATLAB:
if,
together with else
and elseif,
executes a group of statements based on some logical condition.
switch,
together with case
and otherwise,
executes different groups of statements depending on the value of some
logical condition.
while
executes a group of statements an indefinite number of times, based on
some logical condition.
for
executes a group of statements a fixed number of times.
continue
passes control to the next iteration of a for
or while
loop, skipping any remaining statements in the body of the loop.
break
terminates execution of a for
or while
loop.
try...catch
changes flow control if an error is detected during execution.
return
causes execution to return to the invoking function.All flow constructs use end
to indicate the end of the flow control block.
To use MATLAB effectively, it's important to know how to
look up the syntax for hundreds of commands that you can't possibly memorize.
There are many ways to get online help.
If you remember the name of a command, but can't remember its syntax or
all its options, all you have to do is Click
Help Menu, Go to the Using the Desktop.