Sometimes Matlab does not give you the result which you would expect. There are some peculiarities you have to be aware of.
0.0000
. Use format long g
to
see all computed digits, and to get scientific notation for each matrix
element.e^x
'' or ``ln(x)
'' I get
strange results or errorsexp(x)
, not e^x
. The
logarithm function must be written as
log(x)
, not ln(x)
. (For some
reason dsolve
does accept
ln(x)
.)fzero('exp(-t)-t',1)
gives an
error (same for fsolve
)x
: use
fzero('exp(-x)-x',1)
. Alternatively, you can use an inline
function: g=inline('exp(-t)-t','t'); fzero(g,1)
solve
gives warning like ``1
equations in 3 variables'' and does not give correct answersolve(F,G,x,y)
and if
F
or G
happens to be x
or
y
, Matlab does not recognize it as an equation. You can
work around this design flaw by using a command like
solve(2*F,G,x,y)
.axis([xmin xmax ymin ymax])
after your plot
commands. Use
axis tight
if you want to see everything you plotted.ezplot
does not show all of my
plotaxis tight
or
axis([xmin xmax ymin ymax])
after the ezplot
command.title
, xlabel
, ylabel
,
legend
commands, e.g.
x=0:.1:10; y1=sin(x); y2=cos(x);
plot(x,y1,'-',x,y2,'--')
title('Trigonometric functions')
xlabel('x')
ylabel('y')
legend('sin(x)','cos(x)')
orient landscape print test1.ps % save plot in file test1.ps
subplot(2,1,1) % divide page in 2 rows, 1 col, use 1st part ezplot('sin(x)',[-3 3]) % plot command for first graph subplot(2,1,2) % use 2nd part of divided page ezplot('x^2', [-3 3]) % plot command for second graph orient tall % make plot tall enough to fit whole page print test1.ps % save plot in file test1.ps
ezplot
?setcurve.m
in your home directory.
To change the curve color to red, use the following command after the
ezplot
command:
setcurve('color','red')
To change the curve color to green, use a dashed instead of a solid
line, and make the lines thicker, use the following command after the
ezplot
command:
setcurve('color','green','linestyle','--','linewidth',2)
(Type help plot
to see available line styles and
colors.)
prob1.m
, but when I type
prob1
Matlab says it is
undefinedIn the Matlab desktop window you can use the current folder field in the desktop toolbar (click on "..." to the right of the field), see Matlab desktop window with current folder field
If you are on a Windows machine you might save your m-file in the
directory C:\My Documents\homework
. At the Matlab command
prompt you can type addpath('C:\My Documents\homework')
.
On the WAM or Glue system you should use addpath('H:\mydocuments\homework')
.
In the Matlab desktop window you can also select in the "File" menu the item "Set Path...". Then use "Add Folder...", select a folder and hit "OK". Then click "Save", so that you do not have to do this again the next time you start Matlab.
If you are on a Unix machine you might save your m-file in the
directory /homes/myname/homework1
. At the Matlab command
prompt ">>
" you should type
addpath('/homes/myname/homework1')
.
fprintf
with %g
for
each value you want to insert in the text, e.g.,
fprintf('The volume is %g, the area is
%g\n',volume,area)
If you want to see e.g. 15 digits use %.15g
instead of
%g
in fprintf
. disp
, e.g.,
x = sqrt(1:6);
disp('The square roots are'); disp(x)
echo
on
and my m-file contains for
... end
the commands in the loop get echoed many
timesend
'' with ``echo off; end; echo
on
'' to supress echoing after the first loop iteration.pause
statement after each plot
command. Then Matlab will wait for you to press a key before moving on.
Method 2: put the statement figure
before
each plot command. This will generate a separate window for each plot,
and show all plots with publish.
hold on
with several plot commands in my m-file. I want to see what the graph looks
like after each plotting command, but Matlab waits until the last plotting
command before showing the graph.drawnow
after each plotting command. This forces
Matlab to update the graph immediately, rather than waiting until the
end of the m-file.dirfield
,
vectfield
or vectfieldn
, Matlab
says the command is undefineddirfield
, vectfield
,
vectfieldn
are not built in. You must save the files
dirfield.m
, vectfield.m
, vectfieldn.m
into your home directory by
clicking on these links and then selecting the ``Save As...'' command in
your browser. If you started Matlab from your home directory the command
dirfield
will be available. If you started Matlab from some
other directory Matlab might not find dirfield
. In this
case you can use the cd
or addpath
command in
Matlab to fix the problem.dirfield
, ezplot
, fplot
or other
commands.*
'',
``./
'', ``.^
'' instead of ``*
'',
``/
'', ``^
''. Double-check this. The function
for dirfield
must be defined as a function of two
arguments, even if it does not depend on t or y.dsolve
returns an expression with
RootOf
. Whatever I do with this answer I get an
error.y
'' substituted for ``_Z
''
(example). But if you want to do
anything with the answer, like substituting values or using
contour
, you must type in the function with
``y
'' substituted for ``_Z
'' yourself.dsolve('Dy=y^2','y(0)=0','t')
.dsolve
gives error message ``Index
exceeds matrix dimension''.Dy
on the right hand side, e.g., dsolve('y/x=Dy','x')
. Try
putting Dy
on the left hand side of the equation.s=simplify(sol)
and
s=simple(sol)
to rewrite the solution. E.g., for
sol=dsolve('Dy=y+sin(10*t)','t')
using
s=simple(sol)
helps.dsolve
in a for
loop, Matlab does
not use the value of the loop variabledsolve
are strings which are not
evaluated using the current values of the variables. Often it is
possible to use dsolve
outside of the loop with a symbolic
parameter, and then to substitute values in the resulting symbolic
expression inside the loop (Example).
If you really need to put dsolve
inside the loop, you
must convert the value of the loop variable to a string using
num2str
and concatenate strings using
[string1,string2]
. E.g., to solve y' - a
y = 1 for a=-2,-1,...,2 you must use
for a=-2:2 dsolve(['Dy-(',num2str(a),')*y=1'],'t') end
Note that you cannot put dsolve
outside of the loop in
this case (why?).
Heaviside
or
Dirac
I get an
errorHeaviside
or Dirac
you must use it in a
string (inside quotes), e.g., f='2*Heaviside(t-2)+3';
laplace(f,t,s)
ezplot
with a function containing
Heaviside
I get an
error.Heaviside.m
in your home
directory.