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.fprintf
use %.15g
, e.g.,
fprintf('The answer is %.15g\n',x)
e^x
'' or ``ln(x)
'' I get
strange results or errorsexp(x)
, not e^x
. The natural
logarithm function must be written as
log(x)
, not ln(x)
. The base
10 logarithm is log10(x)
, not
log(x)
.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)')
ezplot
does not show all of my
plotaxis tight
or
axis([xmin xmax ymin ymax])
after the ezplot
command..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
undefinedIf 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')
.
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 should type addpath('C:\My Documents\homework')
.
On WAM/Glue you should use addpath('H:\mydocuments\homework')
.
In the integrated environment 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.
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(1)
before
the first plot command, figure(2)
before the second plot
command etc. . This will generate a separate window for each plot.
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.while condition statements end
Otherwise use
while 1 statements if condition break end statements end