SlideShare a Scribd company logo
Chapter 2
MATLAB PROGRAMMING
14/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
MATLAB Programming
window
24/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
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
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
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
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
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
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
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
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
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
Script m-files
12
Argumentssqrt(x)
log(x)
linspace(a,b,n)
4/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
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
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
Global variables
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);
154/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
% 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
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
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
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
Example
204/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
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
Standard flowcharting
symbols
Start/stop………………
Process instruction…….
Conditional test………..
Input/output………….
Connector……………
Flowline………………
224/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
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
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
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
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
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

More Related Content

PPT
How to work on Matlab.......
PPTX
Embedded programming u3 part 1
PDF
Introduction to MATLAB
PPTX
All About MATLAB
PDF
Matlab Presentation
PPTX
Matlab
PDF
Compiler Design- Machine Independent Optimizations
PPTX
Matlab 1 level_1
How to work on Matlab.......
Embedded programming u3 part 1
Introduction to MATLAB
All About MATLAB
Matlab Presentation
Matlab
Compiler Design- Machine Independent Optimizations
Matlab 1 level_1

What's hot (20)

PPTX
Matlab project
PPT
Chap3 flow charts
PPS
PRAM algorithms from deepika
PPTX
Parallel algorithms
PPTX
Flow chart programming
PPTX
Matrix multiplication
PDF
MatLab Basic Tutorial On Plotting
PDF
Matlab for beginners, Introduction, signal processing
PDF
Introduction to Matlab
PPT
Flowchart - Introduction and Designing Tools
PDF
Matlab introduction lecture 1
PPTX
Matlab Introduction
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PDF
Algorithm and c language
PPT
Matlab introduction
PPTX
Structural testing
PPT
Parallel algorithms
PDF
Forelasning4
PPT
Parallel algorithms
Matlab project
Chap3 flow charts
PRAM algorithms from deepika
Parallel algorithms
Flow chart programming
Matrix multiplication
MatLab Basic Tutorial On Plotting
Matlab for beginners, Introduction, signal processing
Introduction to Matlab
Flowchart - Introduction and Designing Tools
Matlab introduction lecture 1
Matlab Introduction
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Algorithm and c language
Matlab introduction
Structural testing
Parallel algorithms
Forelasning4
Parallel algorithms
Ad

Similar to 02 MATLAB programming (20)

PPTX
Simulation lab
PPT
Introduction to matlab
PDF
Introduction to Matlab for Engineering & Science Students.pdf
PPT
Chapter 01.ppt
PPT
Chapter 01.ppt
PDF
Introduction to matlab
PDF
Introduction to matlab
PPT
matlabchapter1.ppt
PPTX
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
PPTX
PPT
Palm m3 chapter1b
PPTX
Chap#0_Introducation_to_nsnsnnMATLAB.pptx
PPT
Chapter 1.ppt
PPTX
3_MATLAB Basics Introduction for Engineers .pptx
PPT
MatlabIntro (1).ppt
PDF
EE6711 Power System Simulation Lab manual
PPT
WIDI ediot autis dongok part 2.ediot lu lembot lu
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
PPTX
1. Ch_1 SL_1_Intro to Matlab.pptx
PPTX
matlab presentation fro engninering students
Simulation lab
Introduction to matlab
Introduction to Matlab for Engineering & Science Students.pdf
Chapter 01.ppt
Chapter 01.ppt
Introduction to matlab
Introduction to matlab
matlabchapter1.ppt
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Palm m3 chapter1b
Chap#0_Introducation_to_nsnsnnMATLAB.pptx
Chapter 1.ppt
3_MATLAB Basics Introduction for Engineers .pptx
MatlabIntro (1).ppt
EE6711 Power System Simulation Lab manual
WIDI ediot autis dongok part 2.ediot lu lembot lu
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
1. Ch_1 SL_1_Intro to Matlab.pptx
matlab presentation fro engninering students
Ad

More from Dr. Mohammed Danish (17)

PPTX
Instrumental method of analysis Oil and Fat(Unit 7 b)
PPTX
Analysis of oil and Fat(Unit 7)
PPTX
Non edible application of oil and Fat (Unit 6 b)
PPTX
Oil and Fat edible applications (Unit 6 a)
PPTX
Fatty acid reaction and derivatives (Unit 5 b)
PPTX
Fatty acid clusters (Unit 5 a)
PPTX
Production of Glycerin (Unit 4 b)
PPTX
Fatty Acid Isolation (Unit 4 a)
PPTX
Processing of edible oil (Unit 3)
PPTX
Chemical and Physical properties of Oil and Fat (Unit 2)
PPTX
Introduction of Edible oils (Unit 1)
PPTX
01 Chapter MATLAB introduction
PPTX
08-09 Chapter numerical integration
PPTX
06-07 Chapter interpolation in MATLAB
PPTX
05 Chapter MATLAB Differntial equations
PPTX
04 Chapter MATLAB linear algebra review
PPTX
03 Chapter MATLAB finite precision arithmatic
Instrumental method of analysis Oil and Fat(Unit 7 b)
Analysis of oil and Fat(Unit 7)
Non edible application of oil and Fat (Unit 6 b)
Oil and Fat edible applications (Unit 6 a)
Fatty acid reaction and derivatives (Unit 5 b)
Fatty acid clusters (Unit 5 a)
Production of Glycerin (Unit 4 b)
Fatty Acid Isolation (Unit 4 a)
Processing of edible oil (Unit 3)
Chemical and Physical properties of Oil and Fat (Unit 2)
Introduction of Edible oils (Unit 1)
01 Chapter MATLAB introduction
08-09 Chapter numerical integration
06-07 Chapter interpolation in MATLAB
05 Chapter MATLAB Differntial equations
04 Chapter MATLAB linear algebra review
03 Chapter MATLAB finite precision arithmatic

Recently uploaded (20)

PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Lecture1 pattern recognition............
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Computer network topology notes for revision
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Mega Projects Data Mega Projects Data
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPT
Quality review (1)_presentation of this 21
PPTX
climate analysis of Dhaka ,Banglades.pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Supervised vs unsupervised machine learning algorithms
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
IBA_Chapter_11_Slides_Final_Accessible.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
ISS -ESG Data flows What is ESG and HowHow
Fluorescence-microscope_Botany_detailed content
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Lecture1 pattern recognition............
Qualitative Qantitative and Mixed Methods.pptx
Computer network topology notes for revision
oil_refinery_comprehensive_20250804084928 (1).pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Mega Projects Data Mega Projects Data
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
STUDY DESIGN details- Lt Col Maksud (21).pptx
Clinical guidelines as a resource for EBP(1).pdf
Quality review (1)_presentation of this 21
climate analysis of Dhaka ,Banglades.pptx

02 MATLAB programming

  • 1. Chapter 2 MATLAB PROGRAMMING 14/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
  • 2. MATLAB Programming window 24/5/2016 DR. MOHAMMED DANISH/ UNIKL-MICET
  • 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
  • 15. Global variables 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); 154/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
  • 20. Example 204/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
  • 22. Standard flowcharting symbols Start/stop……………… Process instruction……. Conditional test……….. Input/output…………. Connector…………… Flowline……………… 224/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