SlideShare a Scribd company logo
1 of 27
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

What's hot

Chap3 flow charts
Chap3 flow chartsChap3 flow charts
Chap3 flow chartsamit139
 
PRAM algorithms from deepika
PRAM algorithms from deepikaPRAM algorithms from deepika
PRAM algorithms from deepikaguest1f4fb3
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithmsDanish Javed
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMOHDRAFIQ22
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingDr. Manjunatha. P
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 
Flowchart - Introduction and Designing Tools
Flowchart - Introduction and Designing ToolsFlowchart - Introduction and Designing Tools
Flowchart - Introduction and Designing ToolsJawad Khan
 
Matlab introduction lecture 1
Matlab introduction lecture 1Matlab introduction lecture 1
Matlab introduction lecture 1Mohamed Awni
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introductionideas2ignite
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkreddyprasad reddyvari
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c languagekamalbeydoun
 
Structural testing
Structural testingStructural testing
Structural testingrahi20
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithmsguest084d20
 
Forelasning4
Forelasning4Forelasning4
Forelasning4Memo Love
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithmsguest084d20
 

What's hot (20)

Matlab project
Matlab projectMatlab project
Matlab project
 
Chap3 flow charts
Chap3 flow chartsChap3 flow charts
Chap3 flow charts
 
PRAM algorithms from deepika
PRAM algorithms from deepikaPRAM algorithms from deepika
PRAM algorithms from deepika
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 
Flow chart programming
Flow chart programmingFlow chart programming
Flow chart programming
 
Matrix multiplication
Matrix multiplicationMatrix multiplication
Matrix multiplication
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On Plotting
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Flowchart - Introduction and Designing Tools
Flowchart - Introduction and Designing ToolsFlowchart - Introduction and Designing Tools
Flowchart - Introduction and Designing Tools
 
Matlab introduction lecture 1
Matlab introduction lecture 1Matlab introduction lecture 1
Matlab introduction lecture 1
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
Matopt
MatoptMatopt
Matopt
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Structural testing
Structural testingStructural testing
Structural testing
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 
Forelasning4
Forelasning4Forelasning4
Forelasning4
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 

Similar to 02 MATLAB programming

Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Parallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterParallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterSudhang Shankar
 
Introduction to Matlab for Engineering Students.pdf
Introduction to Matlab for Engineering Students.pdfIntroduction to Matlab for Engineering Students.pdf
Introduction to Matlab for Engineering Students.pdfDrAzizulHasan1
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab IntroductionDaniel Moore
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingRuymán Reyes
 
Basic concept of MATLAB.ppt
Basic concept of MATLAB.pptBasic concept of MATLAB.ppt
Basic concept of MATLAB.pptaliraza2732
 
Handout2.pdf
Handout2.pdfHandout2.pdf
Handout2.pdfShoukat13
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systemsRaghav Shetty
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabvikrammutneja1
 

Similar to 02 MATLAB programming (20)

Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Parallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterParallel Programming on the ANDC cluster
Parallel Programming on the ANDC cluster
 
Introduction to Matlab for Engineering Students.pdf
Introduction to Matlab for Engineering Students.pdfIntroduction to Matlab for Engineering Students.pdf
Introduction to Matlab for Engineering Students.pdf
 
Lecture 06 assembler
Lecture 06 assemblerLecture 06 assembler
Lecture 06 assembler
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
Mmc manual
Mmc manualMmc manual
Mmc manual
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous Computing
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Basic concept of MATLAB.ppt
Basic concept of MATLAB.pptBasic concept of MATLAB.ppt
Basic concept of MATLAB.ppt
 
Handout2.pdf
Handout2.pdfHandout2.pdf
Handout2.pdf
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systems
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Input-output
Input-outputInput-output
Input-output
 
Ch1
Ch1Ch1
Ch1
 

More from Dr. Mohammed Danish

Instrumental method of analysis Oil and Fat(Unit 7 b)
Instrumental method of analysis Oil and Fat(Unit 7 b)Instrumental method of analysis Oil and Fat(Unit 7 b)
Instrumental method of analysis Oil and Fat(Unit 7 b)Dr. Mohammed Danish
 
Non edible application of oil and Fat (Unit 6 b)
Non edible application of oil and Fat (Unit 6 b)Non edible application of oil and Fat (Unit 6 b)
Non edible application of oil and Fat (Unit 6 b)Dr. Mohammed Danish
 
Oil and Fat edible applications (Unit 6 a)
Oil and Fat edible applications (Unit 6 a)Oil and Fat edible applications (Unit 6 a)
Oil and Fat edible applications (Unit 6 a)Dr. Mohammed Danish
 
Fatty acid reaction and derivatives (Unit 5 b)
 Fatty acid reaction and derivatives (Unit 5 b) Fatty acid reaction and derivatives (Unit 5 b)
Fatty acid reaction and derivatives (Unit 5 b)Dr. Mohammed Danish
 
Production of Glycerin (Unit 4 b)
Production of Glycerin (Unit 4 b) Production of Glycerin (Unit 4 b)
Production of Glycerin (Unit 4 b) Dr. Mohammed Danish
 
Processing of edible oil (Unit 3)
Processing of edible oil (Unit 3)Processing of edible oil (Unit 3)
Processing of edible oil (Unit 3)Dr. Mohammed Danish
 
Chemical and Physical properties of Oil and Fat (Unit 2)
Chemical and Physical properties of Oil and Fat (Unit 2)Chemical and Physical properties of Oil and Fat (Unit 2)
Chemical and Physical properties of Oil and Fat (Unit 2)Dr. Mohammed Danish
 
Introduction of Edible oils (Unit 1)
Introduction of Edible oils (Unit 1) Introduction of Edible oils (Unit 1)
Introduction of Edible oils (Unit 1) Dr. Mohammed Danish
 
08-09 Chapter numerical integration
08-09  Chapter numerical integration 08-09  Chapter numerical integration
08-09 Chapter numerical integration Dr. Mohammed Danish
 
06-07 Chapter interpolation in MATLAB
06-07 Chapter interpolation in MATLAB06-07 Chapter interpolation in MATLAB
06-07 Chapter interpolation in MATLABDr. Mohammed Danish
 
05 Chapter MATLAB Differntial equations
05 Chapter MATLAB Differntial equations05 Chapter MATLAB Differntial equations
05 Chapter MATLAB Differntial equationsDr. Mohammed Danish
 
04 Chapter MATLAB linear algebra review
04 Chapter MATLAB linear algebra review04 Chapter MATLAB linear algebra review
04 Chapter MATLAB linear algebra reviewDr. Mohammed Danish
 
03 Chapter MATLAB finite precision arithmatic
03 Chapter MATLAB finite precision arithmatic03 Chapter MATLAB finite precision arithmatic
03 Chapter MATLAB finite precision arithmaticDr. Mohammed Danish
 

More from Dr. Mohammed Danish (17)

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

Recently uploaded

Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 

Recently uploaded (20)

Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 

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