3. Certain information must
remember
MATLAB is an abbreviation for “Matrix laboratory”. Most of the programming
languages works with one number at a time, Whereas MATLAB is designed to
operate primarily on whole matrices and arrays.
Current Folder: Access your files that saved earlier.
Command window: Enter commands at the command line indicated by the
prompt(>>).
Workspace: Explore data that you create or import from files.
34/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
4. MATLAB uses pi for π, Inf for ∞, i for and Nan for not a
number.
% symbol used in MATLAB for comment line.
4
1
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
5. Function of colon, semicolon
and comma
As you work in MATLAB, you issue commands that creates variable and
call functions e.g., if you type
a=1 this will generate a variable ‘a’ in the workspace.
If you want to terminate or suppress the result display of output in the
command window the function in command window press semicolon
as the variable or function it will terminate the program
54/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
6. Understanding variables and
output
a= 1
MATLAB adds variable ‘a’ to the
workspace and displays the result
in the command window.
a=
1
b=2
b=
2
c= a + b
c =
3
d= cos(a)
d=
0.5403
When you do not specify an
output variable, MATLAB uses the
variable ans, to store the results of
yours calculation.
64/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
7. Using semicolon
If you end a statement with a semicolon, MATLAB performs the
computation, but suppresses the display of the output in the command
window.
e= a*b;
74/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
8. Matrices and Arrays
formation
Matrix: A matrix is a two-dimensional array often used for linear
algebra.
All MATLAB variables are multidimensional arrays, no matter what
type of data.
84/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
9. What is M-files?
MATLAB allows you to write series of commands into a file and execute
the file as complete unit, like writing a function and calling it M-files.
MATLAB allows writing two kinds of program files:- (1) scripts-script
files are program files with .m extension. In these files, you can write
series of commands which you want to execute together. Scripts do not
accept inputs and do not return any outputs. They operate on data in
the workspace. (2) Functions- functions files are also program files with
.m extension. Functions can be accept inputs and return outputs.
Internal variables are local to the function.
94/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
10. How to use .mfiles
You can use the MATLAB editor or any other text editor to create yours
.mfiles. After creating and saving the file, you can run it in two ways-
Clicking the Run button on the editor window or
Just typing the filename (without extension) in the command prompt:>>
the command window prompt displays the results.
104/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
11. Function m-files
function [output variables]=function_name(input variable)
Function y=dude(x)
Y=x^2+cos(x)
Vectorize y=x.^2+cos(x)
114/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
13. 13
Start vector grades
Find average of all
entries in grades
Start grades from low
to high
Replace grades (1) with
average
Sum entires in grades
Start vector grades
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
14. Example of function mfiles.
Function total=quiz_grades(grades)
Average=mean(grades);
Grades=sort(grades);
Grades(1)=average;
Total=sum(grades);
144/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
16. % define global variables
global g
g=32.2; %acceleration due to gravity in ft/s^2
% define inputs
v0=200; % initial velocity in ft/s
theta=20; % launch angle in degree
t=[0:0.01:4.5]; % time in seconds
164/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
17. 17
function
[h,x]=projectiletrajectory(v0,theta,t)
global g
% compute height
h=v0*t*sind(theta)-(1/2)*g*(t.^2);
%compute horizontal distance
x=v0*t*cosd(theta);
Inputs Outputs
v0=initial
velocity
theta=angle
t=Time
h=height
x=horizontal
distance
Global Variables
% define global variables
global g
g=32.2; %acceleration due to
gravity in ft/s^2
% define inputs
v0=200; % initial velocity in ft/s
theta=20; % launch angle in degree
t=[0:0.01:4.5]; % time in seconds
g= Acceleration due to
gravity
% calculate the trajectory- distance along the x and y
[y, x]=projectiletrajectory(v0,theta,t);
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
18. Inline function object
>> f= inline(‘3*x.^4-17*x+8’, ‘x’) …
% this is f(x)=3*x^4-17*x+8
f=
inline function:
f(x)=3*x^4-17*x+8
>>f(3)
ans= 200
184/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
19. Good programming habits
Typical sequence for program operation
19
Input Processing Input
When designing any MATLAB program, think about these steps in
The following order:
Output: what do I want my program to produce/create/do?
Input:what information do I have for my program to work with?
Processing: how do I take the input information that have and get
the output information I want?
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
21. Flow chart
A flowchart is a graphical map or visual plan of the logic needed to
solve the given problem or implement an algorithm.
A flow chart does not need any programe code and therefore allows the
programmer to focus only on the logic or plan of action without
worrying about the syntax.
A good flow chart will save the programmer time when writing code
and can be very helpful if debugging become necessary.
214/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
23. Example
23
Start
Car’s speed (90
km/h), time
between
departure (2h),
time to catch up
(4h).
distance= speed(car)x time
(catch u)
Speed (KTM
Display KTM
train average
speed
Stop
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
24. Pick variable name carefully
For example:
If q>=10
S=0.9*p;
disp(‘s=‘); disp(s);
else
Disp(‘s=‘); disp(p);
end
If quantity>=10
SalePrice=0.9*itemPrice
;
disp(‘itemPrice=‘);
disp(salePrice);
else
Disp(‘itemPrice=‘);
disp(price);
end
24
Picking generic variable names makes code very difficult
To read or debug or revised.
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
25. Comments
A comment is a line of code that conveys
information about your program to someone
reading it, but it is not executed when the
program is run.
Comments allow other to view yours code and
understand what u did.
Comments allow u to remember what u did.
To creat comment in MATLAB use the % symbol(this
will appear in green colored text)
254/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
26. Examples of comments in
MATLAB
% Usage : describe how to run your code (i.e.
what format any inputs need to be given in, are
there input arguments, etc.
% the variables in this program, ‘q’ stands for
quantity's’ stand for item price and ‘s’ stands
for sale price.
264/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
27. Slides ends here!
Dr. Mohammed Danish
Sr. Lecturer, Malaysian Institute of Chemical and
bioengineering Technology (MICET),
UniKL, Alor Gajah, Melaka, Malaysia
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET 27