# Common Problems with Matlab

Sometimes Matlab does not give you the result which you would expect. There are some peculiarities you have to be aware of.

## General

How can I see more than 5 digits of my results?
Matlab does all computations with 15 digits accuracy, but shows only 5 to keep the output more compact. Also, in vectors and matrices Matlab uses a common factor 10m for all elements. If matrix elements have very different sizes, small elements will be displayed as `0.0000`. Use `format long g` to see all computed digits, and to get scientific notation for each matrix element.
For `fprintf` use `%.15g`, e.g., `fprintf('The answer is %.15g\n',x)`
When I use ```e^x`'' or ```ln(x)`'' I get strange results or errors
The exponential function must always be written as `exp(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)`.

## Graphs

How can I label a graph?
Use the `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 plot
ezplot tries to select the y-range in such a way that you only see the interesting part of your functions. Try using `axis tight` or `axis([xmin xmax ymin ymax])` after the `ezplot` command..
My graph does not fill the whole page.
By default the paper is vertically oriented (``portrait''), and the plot is in a rectangle which is wider than high (ratio 4:3), so that there is a lot of empty space on the page. To obtain a larger plot with the page in horizontal orientation (``landscape''), use
```orient landscape
print test1.ps    % save plot in file test1.ps```
How can I put several graphs on one page?
Here is an example which puts one graph in the top half and one graph in the bottom half of the page:
```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```
How can I change the plot style and color in `ezplot` ?
First save the file `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.)

## m-files

I saved my commands in the file `prob1.m`, but when I type `prob1` Matlab says it is undefined
You must tell Matlab where it should look for m-files:

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')` .

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.

How can I output numerical results together with describing text?
For scalar values use `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`.
For vectors and matrices use `disp`, e.g.,
```x = sqrt(1:6); disp('The square roots are'); disp(x)```
When I use ```echo on``` and my m-file contains ```for ... end``` the commands in the loop get echoed many times
Replace ```end`'' with `````echo off; end; echo on```'' to supress echoing after the first loop iteration.
IĀgenerate several plots in my m-file. When I run the m-file I only see the last plot.
Method 1: put a `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.

I use `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.
Use `drawnow` after each plotting command. This forces Matlab to update the graph immediately, rather than waiting until the end of the m-file.
How can I write loops without ``goto''?
If the termination condition is tested at the beginning of the loop use
```while condition
statements
end```

Otherwise use

```while 1
statements
if condition
break
end
statements
end```

See the Matlab help for `while`, `break`.

Tobias von Petersdorff