Professional Documents
Culture Documents
MATLAB FOR
ENGINEERING
STUDENTS
1. INTRODUCTION
The name MATLAB stands for MATrix LABoratory.
As part of the Engineering program, you will be using
Matlab to solve problems in
Circuits
Communication systems
Digital signal processing
Control systems
Probability and statistics
1.1 STARTING MATLAB
Double-clicking on the MATLAB shortcut icon
(MATLAB 2008b) on your Windows desktop.
>> 1+2*3
ans =
7
You may assign a value to a variable or output
argument name. For example,
>> x = 1+2*3
x=
7
This variable name can always be used to refer
to the results of the previous computations.
Therefore, computing 4x will result in
>> 4*x
ans =
28.0000
1.4 OVERWRITING VARIABLE
Once a variable has been created, it can be
reassigned. In addition, if you do not wish to
see the intermediate results, you can suppress
the numerical output by putting a semicolon (;)
at the end of the line. Then the sequence of
commands looks like this:
>> x = 7;
>> x = x + 1
x=
8
1.5 CONTROLLING THE HIERARCHY OF
OPERATIONS OR PRECEDENCE
In MATLAB, it becomes
>> 1/(2+3^2)+4/5*6/7
ans =
0.7766
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
type,
>> A = [1 2 3; 4 5 6; 7 8 9]
MATLAB then displays the 3 x 3 matrix as follows,
>> A = [1 2 3; 4 5 6; 7 8 9];
>> inv(A)
ans =
-1.7778 0.8889 -0.1111
1.5556 -0.7778 0.2222
-0.1111 0.2222 -0.1111
4.CREATING M-FILES
Write a series of MATLAB statements into a file
and then execute them with a single command.
You can save program and edit for future use
2)Function M-file
functions files have input and output
parameters.
suitable for tasks that will be applied to different
data
4.2 ADDING COMMENT IN M-FILE
Comments are strings or statements in an M-file that
do not execute.
Add comments in an M-file to describe the code or
how to use it.
A comment symbol, %, is added at the start of each
selected line, and the color of the text becomes green
or the color specified for comments
E.g:
N = 5 % A scalar
v = [1 0 0] % A row vector
v = [1; 2; 3] % A column vector
v = v' % Transpose a vector (row to column or
% column to row)
4.3EXAMPLE OF SCRIPT FILE
Exercise 1
Consider the system of equations:
x + 2y + 3z = 1
3x + 3y + 4z = 1
2x + 3y + 3z = 2
Find the solution x to the system of equations.
Solution:
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
Save the m-file, for example, exercise1.m.
Run the m-file, in the command line, by typing:
>>exercise1
x=
-0.5000
1.5000
-0.5000
4.4FUNCTION M-FILE
The function declaration must be the first
executable line of any MATLAB function m-file.
function y = f(x)
y = x^2-1;