Introduction to Matlab
Introduction:
Matlab is a computer application that simplifies many complex mathematical tasks.
The name Matlab stands for "matrix laboratory".
Matlab has many functions and "Toolboxes" to help in various applications.
Starting Matlab
(It is suggested that the student follow along by opening a Matlab window while going through this tutorial.)
For NIU CEET Matlab is found on most computers in the Engineering Computer Laboratory. Clicking on the Matlab icon will open Matlab and give an initial prompt.
Initial Matlab prompt:
>>
Entering Data
Values for variables can be entered directly with the keyboard. Variables could include scalars, vectors, or matrices
Scalars - type variable name, equals, and value
Example type:
a = 2.0
Typing the variable name will give its value
Example type:
a
A result of 2.0 should be displayed.
Vectors - elements of array are in square brackets separated by comas
Example type:
A=[3,4,5]
Note: Matab distinguishes between upper and lower case letters!
Matrix - rows of an matrix are separated by semicolons or line breaks
Example type:
C = [10,11,12; 20,21,22
30,31,32]
Equations - use standard symbols and notation
Example type:
D=A*C
Transpose - use prime ' (apostrophe) to transpose a vector or matrix
Example type:
B=A'
E=A*B
Typing a semicolon (;) at the end of the line keeps echo from appearing
Example type:
b=2;
b
Laplace Transfer Functions
Polynomials represented as arrays
4s3+3s2+2s+1 (polynomial expression)
Y=[4,3,2,1] (Matlab representation)
Transfer function - ratio of polynomials of same order
X=[0,30,20,10] (Matlab representation of numerator)
Can enter a transfer function using tf command
Form: Sys=tf(num,den);
Example type :
S=tf(X,Y)
Note: Transfer functions can also be entered using different forms.
Analysis Functions
Matlab has many analysis functions already available.
Useful functions for beginners are step and bode
Step response (response to unit step input)
Form: step(Sys)
Example: step(S)
Bode - generates a bode plot of the transfer function
Form: bode(Sys)
Example: bode(S)
Note: Each of these functions can be entered using different forms
"M" Files
Often want to repeat same operation
Can enter commands in text file with ".m" suffix
Typing file name will execute the commands
(file must be in Matlab path)
Try creating an M file. In File menu choose New; M-file
In text editor window type:
N=[0,1];
D=[1,0.5];
S1=tf(N,D);
step(S1);
Save file as mystep.m
In Matlab window type:
mystep
A step response should appear.
Saving Information
You can save the data you generated in a binary ".mat" file.
This can be done using the save command
Form: save filename
This will save all variables in the workspace in file "filename.mat"
Specific variables only can be saved by typing the variable names after the filename.
Saved data can be retrieved using the load command
Form: load filename
Try saving your data, quitting Matlab, restarting Matlab, and reloading your data.
More Help
Matlab has built in help command: Gives help on specific functions
Form: help name
Example 1: help bode (help on using bode plots)
Example 2: help help (help on using help command)
Other sources for help
From MathWorks (Creators of Matlab)
From Mathtools,a general technical computing portal with many Matlab links