Table of Contents:

- What is Matlab?

- How does one use Matlab?

- Matlab path

- Matlab variables

- Mathematical operations

- String variables and symbolic mathematics

- Plotting

 

What is Matlab?

Matlab is a powerful mathematical analysis program. One can use it much like a calculator to perform complex calculations or program it to perform repetitive analyses. Matlab also can perform symbolic manipulations using Maple symbolic libraries, comes with numerous advanced toolboxes (for example Simulink), and may accessed from within Microsoft Word.

Go back to top of the file

How does one use Matlab?

One can access the power of Matlab through one of three primary interfaces:

  1. Command Window
  2. M-files or script files
  3. Microsoft Word

By launching Matlab from either the desktop or NT Explorer, one can use either the command window or M-files. The current computer configuration does not permit access of Matlab from Word but this may change in the near future.

The command window is the active window immediately after launching Matlab. One enters Matlab commands at the ">>" prompt and presses enter to execute the command. To recall the last commands entered, simply press the up or down arrows; one can edit the commands before executing them. Multiple commands may be entered on one line separated by commas. Separating commands by a semi-colon suppresses output to the command window.

One may enter a sequence of commands into a M-file. Entering the M-file name in the command window executes the commands. Once a M-file is created, one can easily alter the commands for specific problems without having to reenter the entire sequence from the command window.

Go back to top of the file

Matlab path

To view the path, type path in the command window. The path is where Matlab looks for M-files when they are requested. If you create your own M-files and save them to their own directory, you must include this directory in the path.

To add a new directory from the command window enter path(path,'path to your M-files'). The path command without arguments returns the current path while the use of a second string argument specifies a new directory to add to the current path.

Go back to top of the file

Matlab variables

One can use Matlab like a calculator without specifying variables. For example compute the square of 2.5 or multiply 3.89 and 4.1 or subtract 25 from 99.3. (Note that the green text is what you type at the command window prompt and the blue text is what Matlab returns to the command window.)

2.5^2

ans =

6.2500

3.89*4.1

ans =

15.9490

99.3-25

ans =

74.3000

The result of these operations is assigned to a default variable ans and displayed. Adding a semicolon to the end of the operation suppresses the output; try it out!

25*3;

One can also specify other variable names. Matlab permits two types of variables: matrices and strings. A 1 by 1 matrix is a scalar while an m by n matrix contains m rows and n columns; the fact that Matlab treats a scalar as a 1 by 1 matrix is very important in future operations. String variables are specified by enclosing the string between single quotation marks.

Variable names may be up to 19 characters long. Names must begin with a letter but may be followed by any combination of letters, digits or underscores. Remember that variables are case sensitive and some predefined variables are useful (ans, pi, eps, j).

ans

ans =

75

pi

ans =

3.1416

eps

eps =

2.2204e-016

j

ans =

0 + 1.0000i

Verify that variable names are case sensitive by creating two variable var and Var. Assign two different values to the variables and print them out by entering their names separated by a comma.

var=1.2

var =

1.2000

Var=-5.1

Var =

-5.1000

var, Var

var =

1.2000

Var =

-5.1000

 

Go back to top of the file

Mathematical operations

Scalar (1 by 1 matrices) operations include addition, subtraction, multiplication, and division. Note the difference between left and right division.

Right division (a/b implies a=ans*b):

var/Var

ans =

-0.2353

Left division (a\b implies a*ans=b):

var\Var

ans =

-4.2500

This distinction, ans=a*b-1 or ans=a-1*b, is important in matrix division operations since matrix multiplication is not commutative.

Let's solve a quadratic equation

a=1; b=4; c=13;

x1=(-b-sqrt(b^2-4*a*c))/(2*a)

x1 =

-2.0000 - 3.0000i

The complex variable i appears here. We can readily verify that this is one root:

a*x1^2+b*x1+c

ans =

-5.3291e-015- 1.7764e-015i

We don't get exactly zero but compare the answer to the machine zero:

eps

eps =

2.2204e-016

Create a 3 by 3 matrix

m33=[1, 5, -1; 2, 0, 1; 1, -1, 0]

m33 =

1 5 -1

2 0 1

1 -1 0

Create a 1 by 3 matrix

m13=[1 3 5]

m13 =

1 3 5

Create another 1 by 3 matrix

n13=[0 5 9]

n13 =

0 5 9

One can also create a matrix by specifying a starting value, a step value and an ending value:

m=(0:0.1:1)*pi

m =

Columns 1 through 7

0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850

Columns 8 through 11

2.1991 2.5133 2.8274 3.1416

You can also use the colon operator to extract values from a matrix:

m(1:2)

ans =

0 0.3142

Valid matrix operations include addition, subtraction, multiplication, and division.

m13+n13

ans =

1 8 14

m33\m13

??? Error using ==> \

Matrix dimensions must agree.

This division fails because we are attempting to solve m33*x=m13. x must must have three rows and one or more columns thus m13 must have three rows and one or more columns. However, m13 is a 1 by 3 row matrix, a row vector. To convert m13 to a 3 by 1 column vector we can use the matrix transpose operator:

m33\m13'

ans =

3.6250

-1.3750

-4.2500

m33\n13'

ans =

6.2500

-2.7500

-7.5000

The dot product is given by:

m13*n13'

ans =

60

One can multiply element by element by preceding the asterisk with a period:

m13.*n13

ans =

0 15 45

Matlab's ability to robustly handle matrices is one of its strengths. For instance one can easily compute inverses:

inv(m33)

ans =

0.1250 0.1250 0.6250

0.1250 0.1250 -0.3750

-0.2500 0.7500 -1.2500

You can check what variables you have defined by entering who or whos.

who

Your variables are:

D V a1 c m22 var

L Var ans m m33 x1

U a b m13 n13

whos

Name Size Elements Bytes Density Complex

D 3 by 3 9 144 Full Yes

L 3 by 3 9 72 Full No

U 3 by 3 9 72 Full No

V 3 by 3 9 144 Full Yes

Var 1 by 1 1 8 Full No

a 1 by 1 1 8 Full No

a1 1 by 3 3 24 Full No

ans 2 by 5 10 80 Full No

b 1 by 1 1 8 Full No

c 1 by 1 1 8 Full No

m 2 by 11 22 176 Full No

m13 1 by 3 3 24 Full No

m22 3 by 3 9 72 Full No

m33 3 by 3 9 72 Full No

n13 1 by 3 3 24 Full No

var 1 by 1 1 8 Full No

x1 1 by 1 1 16 Full Yes

Grand total is 101 elements using 960 bytes

To eliminate a variable use clear:

clear a1

who

Your variables are:

D V ans m m33 x1

L Var b m13 n13

U a c m22 var

Entering clear by itself clears all variables. Be careful! To save your variables and workspace, you can use the save command. The variables can be restored using load.

Go back to top of the file

String variables and symbolic mathematics

A string variable is defined between single quotations:

f1='x^2+3*x+5'

f1 =

x^2+3*x+5

f2='x^3+a*x+b'

f2 =

x^3+a*x+b

String variables are used to symbolically manipulate expressions. For instance, we can add expressions:

symadd(f1,f2)

ans =

x^2+3*x+5+x^3+a*x+b

symmul(f1,f2)

ans =

(x^2+3*x+5)*(x^3+a*x+b)

One can differentiate symbolic expressions:

simplify(diff(ans,'x'))

ans =

5*x^4+3*a*x^2+2*b*x+12*x^3+6*a*x+3*b+15*x^2+5*a

One can also attempt to evaluate integrals symbolically as an indefinite integral:

int('x^2')

ans =

1/3*x^3

or as a definite integral

int('x^2',-1,1)

ans =

2/3

One can solve an algebraic equation symbolically using solve:

solve(f1)

ans =

[-3/2+1/2*i*11^(1/2)]

[-3/2-1/2*i*11^(1/2)]


To convert a symbolic result to an equivalent numerical result, try the numeric command:

numeric(ans)

ans =

-1.5000 + 1.6583i

-1.5000 - 1.6583i

solve('sin(x)+x=0.1','x')

ans =

5.001042187833512e-2

solve('a*x^2+b*x+c=0','x')

ans =

[1/2/a*(-b+(b^2-4*a*c)^(1/2))]

[1/2/a*(-b-(b^2-4*a*c)^(1/2))]

To solve multiple equations simply add the equations to the list to be solved:

f1='ln(x)+x*y=0'; f2='x*y+5*y=1';

[x,y]=solve(f1,f2,'x,y')

x =

.8631121967939437

y =

.1705578823046945

x=numeric(x)

x =

0.8631

y=numeric(y)

y =

0.1706

x*y+5*y-1

ans =

2.2204e-016

One can solve differential equations symbolically using dsolve:

dsolve('Dy+y=cos(t)')

ans =

1/2*cos(t)+1/2*sin(t)+exp(-t)*C1

or with specified boundary conditions:

dsolve('D2y+y=1','y(0)=0','y(1)=1')

ans =

1+1/sin(1)*cos(1)*sin(x)-cos(x)

Sometimes a solution cannot be found and then one must use ode23 or ode45.

 

Go back to top of the file

Plotting

To plot symbolic functions, one can use either ezplot:

f='exp(t)'

f =

exp(t)

ezplot(f,[-1,1]); title('Example Plot using Ezplot'); ylabel('f(t)'); xlabel('t');

Note that the plot is not shown here.

Using hold on allows you to add multiple lines:

hold on

ezplot('t^2',[-1,1])

hold off

One can also use plot if specifc values for the abscissa and ordinate are known:

x1=-1:0.01:1;

x2=-1:0.1:1;

y1=exp(x1);

y2=x2.^2;

plot(x1,y1,'-',x2,y2,'--'); grid; xlabel('t'); ylabel('f(t)'); title('Use of Plot'); axis([-1 1 0 3]);

If you do not have a white background and wish to have one, use the command.

whitebg

Note that repeated use of this command toggles the background between white and black.

Go back to top of the file