Math101,  Matlab Help

 

Starting Matlab:

Matlab is one of the most commonly used scientific computational programs. To start it, click (or double click) the Matlab icon.


Ending Matlab:

To finish a Matlab session, type "quit" at the prompt in the Matlab window.


Simple arithmetic:

Matlab interprets expressions you type in. For example

A=5+3

B=3*4+2

A/B

B^2 (this is squaring)

A/C would produce an error: C has not been defined. If you don't assign the result of an expression to some variable name (like A or B above), then it is assigned to the variable ans. If you don't want to see the result of a command on the screen, end the line with a semicolon (;) For example,

C=sqrt(A+B);

assigns the result to a variable C, but doesn't tell you what it is. You can see it with the command

C

Other functions available include exp, log, sin, cos, tan, atan, acos, the last two being inverse functions (inverse tangent, inverse cosine).


Graphing functions:

The basic variable in Matlab is the array, meaning a list of numbers, not a single number. You can create an array with a single command, for example

x=1:10

makes the variable x the array [1 2 3 4 5 6 7 8 9 10]. The command

y=x.^2

makes the variable y the array [1 4 9 16 25 36 49 64 81 100]. This is just each one of the x's squared. (Note the '.' before the '^' in the command to square each element of the x array.) Now

plot(x,y)

plots the squaring function. You need not take x to be just integers. The command

x=linspace(0,10,101)

takes values from 0 to 10 equally spaced (linearly spaced), and 101 of them in all. This means x is the array [0 0.1 0.2 0.3 ... 9.8 9.9 10.0]. (This might have been a good time to end the line with a semicolon -- no need to see all those x's.) Now

y=x.^2;

plot(x,y,'g')

plots the squaring function, but with more values. The graph will be in green because of the 'g'. Try also things like

y=2*x+3

plot(x,y)

plot(x,sin(x))

plot(x,exp(x))


A Matlab Exercise

We have talked about a number of functions. Let us see them graphically, and try to understand the graphs. Sometimes the insight we get from general arguments makes it easier to interpret the graphs, other times the graphs themselves may give us insight we had not thought of.

Exponential functions

Verify that you can get exponential functions with different bases by trying out the commands

x=0:5

exp(log(2)*x)

exp(log(3)*x)

Try the following commands to see exponential growth and decay:

x=linspace(0,5,101);

plot(x,exp(x))

plot(x,exp(-x))

Power laws

x=linspace(-1.5,1.5,101);

plot(x,x.^3,'g')

hold on

plot(x,x.^2)

plot(x,x.^4,'b')

plot(x,x.^(-1),'w')

hold off

It is best to enter these in EXACTLY the order I have given, so that everything will be visible. Why do you get a warning message when you plot x^(-1)? What can you observe about the various powers and their relative importance in this graph?

A Polynomial

In class we looked briefly at the polynomial P(x)=3x^3+2x^2-x+1. Graph it with

plot(x,3*x.^3+2*x.^2-x+1)

As we mentioned, near x=0 the important terms are -x+1, a linear function. Since P(x) is essentially just this linear function near x=0, that must be the equation of the tangent line. See this by first of all "holding" the plot, so you can overlap two graphs, then graphing the tangent line:

hold on

plot(x,-x+1,'g')

hold off

You can see that P(x) is very nearly this linear function, at least for a small region near x=0.

Exponentials and powers

Exponentials decay faster than powers grow, and grow faster than (reciprocal) powers decay -- all remarks that apply as x gets large. Observe this, on the positive x-axis, with

x=linspace(0,5,101);

plot(x,(x).*exp(-x))

plot(x,(x.^2).*exp(-x))

plot(x,(x.^3).*exp(-x))

etc.

Also

x=linspace(1,5,101);

plot(x,exp(x)./x)

plot(x,exp(x)./(x.^2))

plot(x,exp(x)./(x.^3))

etc.

What is being illustrated here?

Here is a rather tricky challenge: graph the rational function (x^2+3)/(x-1) in such a way that its main features can be seen. We sketched a graph in class, but you may find it hard to get a machine -- either Matlab or a graphing calculator -- to do it well. Some possible tricks: plot(x,y,'+') just plots points (as +'s), but does not connect them. You can still see the shape of the function, and it may actually look better. You can remove values of x from the domain -- say x(21), x(22), x(23) -- with the command

x(21:23)=[ ];

The effect is to throw away those values, and renumber what is left, so that there are fewer x values. You may want to do that for values of x which are too close to the singularity at x=1, where the denominator is zero.