Matlab
Matlab
What is Matlab?
• Matlab is basically a high level language which
has many specialized toolboxes for making
things easier for us
• How high?
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
Starting MATLAB
• Matlab (matrix laboratory) is a software package that lets you
do mathematics and computation, analyze data, develop
algorithms, do simulation and modeling, and produce
graphical displays and graphical user interfaces.
• To run matlab on a PC double- click on the matlab icon.
• You get matlab to do things for you by typing in commands.
• You can type help at the matlab prompt, or pull down the Help
menu on a PC.
• The various forms of help available are
• Help win opens a matlab help GUI (graphical user interface)
• Help desk opens a hypertext (text displayed on a computer
display) help browser
• Demo starts the matlab demonstration
Contd…
• You can learn how to use any matlab command by typing help
followed by the name of the command, for example, help sin.
• You can also use the look for command, which searches the help
entries for all matlab commands for a particular word.
First Steps
• To get matlab to work out 1 + 1, type the following at the prompt
(command window):
• you have started MATLAB on your system successfully and you
are ready to type the commands at the MATLAB prompt (which
is denoted by double arrows “>>”). Entering scalars and simple
operations is easy as is shown in the examples below:
x= 1+1
ans =
2
>> 2/sqrt(3+x)
• To suppress the output in MATLAB use a semicolon to end the
command line as in the following examples. If the semicolon is
not used then the output will be shown by MATLAB:
» y=32;
» z=5;
» x=2*y-z;
» w=3*y+4*z
w=
116
• MATLAB is case-sensitive, i.e. variables with lowercase letters
are different than variables with uppercase letters. Consider
the following examples using the variables x and X.
Matlab Screen
• Command Window
– type commands
• Current Directory
– View folders and m-files
• Workspace
– View program variables
– Double click on a variable
to see it in the Array Editor
• Command History
– view past commands
– save a whole session
using diary
Contd…
Typing Matrices
To type a matrix in to matlab you must
• Begin with a square bracket[
• Separate elements in a row with commas or spaces
• Use a semi colon ; to separate rows
• End the matrix with another square bracket].
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
‘ complex conjugate transpose
For example type:
a = [1 2 3; 4 5 6;7 8 9]
matlab responds with
a=
1 2 3
4 5 6
7 8 9
• Matrices can be made up of sub matrices: Try this:
>> b = [a 10*a;-a [1 0 0; 0 1 0;0 0 1]]
b=
Contd…
Subscripting
• Individual elements in a matrix are denoted by a row
index and a column index. To pick out the third element
of the vector u type:
>> u (3)
ans =
0.1763
Contd…
• The last two examples use the colon symbol as an index, which
matlab interprets as the entire row or column. If a matrix is
addressed using a single index, matlab counts the index down
successive columns:
>> a(4)
ans =
2
>> a(8)
ans =
6
End as a subscript
To access the last element of a matrix along a given dimension, use
end as a subscript. This allows you to go to the final element
without
knowing in advance how big the matrix is.
Example:
Or
Basic Graphics
• The bread-and-butter of matlab graphics is the plot command.
Earlier we produced a plot of the sine function:
• There are many options for changing the appearance of a plot. For
example: plot(x,y,’r-.’) will join the points using a red dash-dotted
line.
• Other colours you can use are: ’c’, ’m’, ’y’, ’r’, ’g’, ’b’, ’w’, ’k’,
which correspond to cyan, magenta, yellow, red, green, blue, white,
and black. Possible line styles are: solid ’-’, dashed ’--’, dotted ’:’,
and dash-dotted ’-.’. To plot the points themselves with symbols you
can use: dots’.’, circles ’o’, plus signs ’+’, crosses ’x’, or stars ’*’,
and many others (type help plot for a list).
Plotting Many Lines
• To plot more than one line you can specify more than one set
of x and y vectors in the plot command:
Plotting Matrices
Subplots
• To plot more than one set of axes in the same window, use the
subplot command. You can type subplot (m,n,p) to break up the
plotting window into m plots in the vertical direction and n plots in
the horizontal direction, choosing the pth plot for drawing in to.
Three-Dimensional Plots
• The plot3 command is the 3-d equivalent of plot:
Basic Task: Plot the function sin(x) between
0≤x≤4π
• Create an x-array of 100 samples between 0 and
4π.
>>x=linspace(0,4*pi,100);
0.8
0.6
>>y=sin(x); 0.4
0.2
-0.4
-0.6
>>plot(y) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
Plot the function e-x/3sin(x) between 0≤x≤4π
>>plot(y2) 0.6
0.5
0.4
0.3
0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
0.7
0.6
• plot(.)
0.5
0.4
0.3
Example:
0.2
0.1
>>x=linspace(0,4*pi,100); 0
>>y=sin(x); -0.1
>>plot(y) -0.2
>>plot(x,y) -0.3
0 10 20 30 40 50 60 70 80 90 100
0.7
• stem(.) 0.6
0.5
0.4
0.3
Example:
0.2
0.1
>>stem(y) 0
>>stem(x,y) -0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
• title(.)
This is the sinus function
>>title(‘This is the sinus function’) 1
0.8
• xlabel(.) 0.6
0.4
sin(x)
0
• ylabel(.)
-0.2
-0.4
-0.6
-0.8
>>ylabel(‘sin(x)’) -1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
PROGRAMMING IN MATLAB
• Function files, on the other hand, play the role of user defined
commands that often have input and output. You can create
your own commands for specific problems this way, which
will have the same status as other MATLAB commands.
function [a] = log3(x)
% [a] = log3(x) - Calculates the base 3 logarithm of x.
a = log(abs(x))./log(3);
% End of function
Loops
• The two types of loops that we will discuss are “for” and
“while” loops. Both loop structures in MATLAB start with a
keyword such as for, or while and they end with the word end.
Contd…
• The “for” loop allows us to repeat certain commands. If you want to
repeat some action in a predetermined way, you can use the “for” loop.
The “for” loop will loop around some statement, and you must tell
MATLAB where to start and where to end.
For example,
>> for j=1:4
j+2
end
A=[1,5,-3;2,4,0;-1,6,9];
» for i=1:3
for j=1:3
A2(i,j) = A(i,j)^2;
end
end
If statement
Step6–Post-processing:
• In this step, we obtain the reaction at node 1 and the force in
each spring using MATLAB.
• First we set up the global nodal displacement vector U, then
we calculate the global nodal force vector F
Problem 1
• Consider the spring system composed of two springs as
shown. Given k1= 200kN/m, k2= 250kN/m, and P=10kN,
determine:
1. The global stiffness matrix for the system.
2. The displacements at node 2.
3. The reactions at nodes 1 and 3.
4. The force in each spring
Example2