際際滷

際際滷Share a Scribd company logo
PROGRAMMING IN MATLAB
Introduction
o A computer program is a sequence of computer commands. In
a simple program the commands are executed one after the other
in the order they are typed
o MATLAB is also a programming language. Like other computer
programming languages, MATLAB has some decision making
structures for control of command execution
o These decision making or control flow structures include for
loops, while loops, and if-else-end constructions
Introduction
o Control flow structures are often used in script M-files and
function M-files
o By creating a file with the extension .m, we can easily write and
run programs
o We do not need to compile the program since MATLAB is an
interpretative (not compiled) language
Relational and logical operators
o A relational operator compares two numbers by determining
whether a comparison is true or false
Relational operators
< : Less than
> : Greater than
<= : Less than or equal to
>= : Greater than or equal to
= = : Equal to
~= : Not Equal to
Cont.. .
 Relational operators are used as arithmetic operators within a
mathematical expression. The result can be used in other
mathematical operations, in addressing arrays, and together
with other MATLAB commands (e.g., if) to control the flow of a
program
 When two numbers are compared, the result is 1 (logical true) if
the comparison, according to the relational operator, is true, and
0 (logical false) if the comparison is false
Cont.. .
 If two scalars are compared, the result is a scalar 1 or 0
 If two arrays are compared (only arrays of the same size can be
compared), the comparison is done element-by-element, and
the result is a logical array of the same size with 1s and 0s
according to the outcome of the comparison at each address
 If a scalar is compared with an array, the scalar is compared
with every element of the array, and the result is a logical
array with 1s and 0s according to the outcome of the
comparison of each element
Logical operators
AND (&)
Example: A&B
and(A,B)
Operates on two operands (A and B). If both are
true, the result is true (1); otherwise the result Is
false (0)
OR (|)
Example: A|B
or(A,B)
Operates on two operands (A and B). If either
one, or both, are true, the result is true (1);
otherwise (both are false) the result is false (0)
NOT (~)
Example: ~A
not(A)
Operates on one operand (A). Gives the opposite
of the operand; true (1) if the operand is false, and
false (0) if the operand is true
Cont.. .
 Logical operators have numbers as operands. A nonzero number
is true, and a zero number is false
 Logical operators (like relational operators) can be used with
scalars and arrays
Logical built-in functions
xor(a,b) Exclusive or. Returns true (1) if one operand is true and
the other is false
all(A) Returns 1 (true) if all elements in a vector A are true
(non- zero). Returns 0 (false) if one or more elements are
false (zero).If A is a matrix, treats columns of A as
vectors, and returns a vector with 1s and 0s
any(A) Returns 1 (true) if any element in a vector A is true
(nonzero). Returns 0 (false) if all elements are false
(zero). If A is a matrix, treats columns of A as vectors,
and returns a vector with 1s and 0s
find(A) If A is a vector, returns the indices of the nonzero
elements
find(A>d) If A is a vector, returns the address of the elements that
are larger than d (any relational operator can be used)
Summary
INPUT OUTPUT
A B AND
A&B
OR
A|B
XOR
(A,B)
NOT
~A
NOT
~B
false false false false false true true
false true false true true true false
true false false true true false true
true true true true false false false
Conditional Statement and Loops
o In MATLAB, loops and conditional statements are control
statements that allow programmers to execute code
repeatedly or conditionally, and control the flow of a
program
o Control flow is determined by control structures
o Control structure determines what gets executed
MATLAB CONTROL STRUCTURES
o MATLAB has four control structures
o two for deciding between alternatives
- if statements
- switch statements
o two for repeating (looping or iteration)
- while loops
- for loops
o MATLAB also has implicit loops over arrays
The if-end structure
o An if statement runs the body when the condition is
true
o if statement are of form
if <condition>
<body>
end
 if and end are keywords (cant be used as variables)
Cont.. ..
 <condition> is a logical expression which can be
evaluated to true or false
 <body> is the body of the if statement
 One or more statements
 Only executed when the condition evaluates to true
The if-else-end Structure
 an else clause can be run if the condition is false
if <condition>
<true-body>
else
<false-body>
end
 else is also a keyword
 <false-body> is run when the condition is false
The if-elseif-else-end Structure
 the general scheme looks like
if <condition1>
<true-body1>
elseif<condition2>
<true-body2>
else
<false-body>
end
 where the elseif and else clauses are optional
 and the elseif clause can be repeated for more conditions
Example
A worker is paid according to his hourly wage up to 40
hours, and 50% more for overtime. Write a program in a
script file that calculates the pay to a worker. The program
asks the user to enter the number of hours and the hourly
wage. The program then displays the pay
Solution
for-end Loops
 for loops simplify looping over arrays
 for loops iterate over each element of an array or
range
for <variable> = <array>
<body>
end
 <body> is run once for each element of the array
Ex.2
Ex.3
 variable m is assigned the value 75, then 80, then 71
 after each time m is assigned, the for loop body is run
7_Programming in MATLAB For Enginee.pptx
Nested for loop
o when we need the index, loop over 1:length(A)
o for example, to find the index of the maximum in values:
while loop
o a while loop repeats the body while the condition stays true
o the general scheme looks very similar to an if statement
while <condition>
<body>
end
 while and end are keywords
 when <condition> is false the loop will not execute again
Cont.. .
 a while loop repeats the following steps
- first, evaluating the conditional expression
- if the condition is true, run the body
- if the condition is false, jump to the statement after the
body
 an iteration is a single execution of the body
 the condition is evaluated before each iteration begins
 sometimes the body may never get executed
Ex.
Note !!!
 not updating the loop variable is a common mistake
 what happens when we run this program?
 you can stop the infinite loop by pressing control c
 x is never updated in the body, so it stays at 1
 the condition is never false, the loop never stops!
Ex..
Cont.. .
 the length function returns the length of a vector
 the loop variable i ranges from 1 up to 6
 single letter loop variable, especially i, j, and k are common

More Related Content

Similar to 7_Programming in MATLAB For Enginee.pptx (20)

hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppthlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
PraveenaFppt
Py-際際滷s-2 (1).ppt
Py-際際滷s-2 (1).pptPy-際際滷s-2 (1).ppt
Py-際際滷s-2 (1).ppt
KalaiVani395886
Py-際際滷s-2.ppt
Py-際際滷s-2.pptPy-際際滷s-2.ppt
Py-際際滷s-2.ppt
TejaValmiki
Py-際際滷s-2.ppt
Py-際際滷s-2.pptPy-際際滷s-2.ppt
Py-際際滷s-2.ppt
AllanGuevarra1
Lewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_CodingLewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
R.K.College of engg & Tech
Ch04
Ch04Ch04
Ch04
Arriz San Juan
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
malekaanjum1
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.pptppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
Control structures
Control structuresControl structures
Control structures
Gehad Enayat
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.pptPPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
RahulKumar812056
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
VeerannaKotagi1
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
Viraj Shah
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
Jaya Kumari
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
Aami Kakakhel
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
ErnieAcuna
Control Structures Python like conditions and loops
Control Structures Python like conditions and loopsControl Structures Python like conditions and loops
Control Structures Python like conditions and loops
ramireddyobulakondar
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
classVIII_Coding_Teacher_Presentation.pptx
classVIII_Coding_Teacher_Presentation.pptxclassVIII_Coding_Teacher_Presentation.pptx
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppthlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
hlukj6;lukm,t.mnjhgjukryopkiu;lyk y2.ppt
PraveenaFppt
Py-際際滷s-2 (1).ppt
Py-際際滷s-2 (1).pptPy-際際滷s-2 (1).ppt
Py-際際滷s-2 (1).ppt
KalaiVani395886
Py-際際滷s-2.ppt
Py-際際滷s-2.pptPy-際際滷s-2.ppt
Py-際際滷s-2.ppt
TejaValmiki
Py-際際滷s-2.ppt
Py-際際滷s-2.pptPy-際際滷s-2.ppt
Py-際際滷s-2.ppt
AllanGuevarra1
Lewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_CodingLewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
malekaanjum1
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.pptppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
Control structures
Control structuresControl structures
Control structures
Gehad Enayat
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.pptPPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
RahulKumar812056
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
VeerannaKotagi1
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
Viraj Shah
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
Jaya Kumari
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
Aami Kakakhel
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
ErnieAcuna
Control Structures Python like conditions and loops
Control Structures Python like conditions and loopsControl Structures Python like conditions and loops
Control Structures Python like conditions and loops
ramireddyobulakondar
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
classVIII_Coding_Teacher_Presentation.pptx
classVIII_Coding_Teacher_Presentation.pptxclassVIII_Coding_Teacher_Presentation.pptx
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets

More from SungaleliYuen (18)

9_Symbolic Math in MATLAB for Engineers.pptx
9_Symbolic Math in MATLAB for Engineers.pptx9_Symbolic Math in MATLAB for Engineers.pptx
9_Symbolic Math in MATLAB for Engineers.pptx
SungaleliYuen
8_Polynomials, Curve Fitting & Interpolation.pptx
8_Polynomials, Curve Fitting & Interpolation.pptx8_Polynomials, Curve Fitting & Interpolation.pptx
8_Polynomials, Curve Fitting & Interpolation.pptx
SungaleliYuen
5_Vectors & Matrices for Engineers .pptx
5_Vectors & Matrices for Engineers .pptx5_Vectors & Matrices for Engineers .pptx
5_Vectors & Matrices for Engineers .pptx
SungaleliYuen
3_MATLAB Basics Introduction for Engineers .pptx
3_MATLAB Basics Introduction for Engineers .pptx3_MATLAB Basics Introduction for Engineers .pptx
3_MATLAB Basics Introduction for Engineers .pptx
SungaleliYuen
Lecture_3.pptx
Lecture_3.pptxLecture_3.pptx
Lecture_3.pptx
SungaleliYuen
5_Basics of Electrical Machines.pptx
5_Basics of Electrical Machines.pptx5_Basics of Electrical Machines.pptx
5_Basics of Electrical Machines.pptx
SungaleliYuen
4_Inductance, Energy.pptx
4_Inductance, Energy.pptx4_Inductance, Energy.pptx
4_Inductance, Energy.pptx
SungaleliYuen
2_Electric Current.pptx
2_Electric Current.pptx2_Electric Current.pptx
2_Electric Current.pptx
SungaleliYuen
VENANCE AND MPYA.pptx
VENANCE AND MPYA.pptxVENANCE AND MPYA.pptx
VENANCE AND MPYA.pptx
SungaleliYuen
Angela and claudius.pptx
Angela and claudius.pptxAngela and claudius.pptx
Angela and claudius.pptx
SungaleliYuen
MARIA NDAGO PROJECT.pptx
MARIA NDAGO  PROJECT.pptxMARIA NDAGO  PROJECT.pptx
MARIA NDAGO PROJECT.pptx
SungaleliYuen
emmanuel and catherine project.pptx
emmanuel and catherine project.pptxemmanuel and catherine project.pptx
emmanuel and catherine project.pptx
SungaleliYuen
Lecture 3.pptx
Lecture 3.pptxLecture 3.pptx
Lecture 3.pptx
SungaleliYuen
1. Introduction.pptx
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptx
SungaleliYuen
3_Magnetic Circuits.pptx
3_Magnetic Circuits.pptx3_Magnetic Circuits.pptx
3_Magnetic Circuits.pptx
SungaleliYuen
4_Inductance, Energy.pptx
4_Inductance, Energy.pptx4_Inductance, Energy.pptx
4_Inductance, Energy.pptx
SungaleliYuen
3_Types of DC GEN.pptx
3_Types of DC GEN.pptx3_Types of DC GEN.pptx
3_Types of DC GEN.pptx
SungaleliYuen
4_Generator Characteristics.pptx
4_Generator Characteristics.pptx4_Generator Characteristics.pptx
4_Generator Characteristics.pptx
SungaleliYuen
9_Symbolic Math in MATLAB for Engineers.pptx
9_Symbolic Math in MATLAB for Engineers.pptx9_Symbolic Math in MATLAB for Engineers.pptx
9_Symbolic Math in MATLAB for Engineers.pptx
SungaleliYuen
8_Polynomials, Curve Fitting & Interpolation.pptx
8_Polynomials, Curve Fitting & Interpolation.pptx8_Polynomials, Curve Fitting & Interpolation.pptx
8_Polynomials, Curve Fitting & Interpolation.pptx
SungaleliYuen
5_Vectors & Matrices for Engineers .pptx
5_Vectors & Matrices for Engineers .pptx5_Vectors & Matrices for Engineers .pptx
5_Vectors & Matrices for Engineers .pptx
SungaleliYuen
3_MATLAB Basics Introduction for Engineers .pptx
3_MATLAB Basics Introduction for Engineers .pptx3_MATLAB Basics Introduction for Engineers .pptx
3_MATLAB Basics Introduction for Engineers .pptx
SungaleliYuen
5_Basics of Electrical Machines.pptx
5_Basics of Electrical Machines.pptx5_Basics of Electrical Machines.pptx
5_Basics of Electrical Machines.pptx
SungaleliYuen
4_Inductance, Energy.pptx
4_Inductance, Energy.pptx4_Inductance, Energy.pptx
4_Inductance, Energy.pptx
SungaleliYuen
2_Electric Current.pptx
2_Electric Current.pptx2_Electric Current.pptx
2_Electric Current.pptx
SungaleliYuen
VENANCE AND MPYA.pptx
VENANCE AND MPYA.pptxVENANCE AND MPYA.pptx
VENANCE AND MPYA.pptx
SungaleliYuen
Angela and claudius.pptx
Angela and claudius.pptxAngela and claudius.pptx
Angela and claudius.pptx
SungaleliYuen
MARIA NDAGO PROJECT.pptx
MARIA NDAGO  PROJECT.pptxMARIA NDAGO  PROJECT.pptx
MARIA NDAGO PROJECT.pptx
SungaleliYuen
emmanuel and catherine project.pptx
emmanuel and catherine project.pptxemmanuel and catherine project.pptx
emmanuel and catherine project.pptx
SungaleliYuen
1. Introduction.pptx
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptx
SungaleliYuen
3_Magnetic Circuits.pptx
3_Magnetic Circuits.pptx3_Magnetic Circuits.pptx
3_Magnetic Circuits.pptx
SungaleliYuen
4_Inductance, Energy.pptx
4_Inductance, Energy.pptx4_Inductance, Energy.pptx
4_Inductance, Energy.pptx
SungaleliYuen
3_Types of DC GEN.pptx
3_Types of DC GEN.pptx3_Types of DC GEN.pptx
3_Types of DC GEN.pptx
SungaleliYuen
4_Generator Characteristics.pptx
4_Generator Characteristics.pptx4_Generator Characteristics.pptx
4_Generator Characteristics.pptx
SungaleliYuen

Recently uploaded (20)

CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
How to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using ArduinoHow to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using Arduino
CircuitDigest
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
Embedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.pptEmbedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.ppt
23ucc580
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
J. Agricultural Machinery
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Piping-and-pipeline-calculations-manual.pdf
Piping-and-pipeline-calculations-manual.pdfPiping-and-pipeline-calculations-manual.pdf
Piping-and-pipeline-calculations-manual.pdf
OMI0721
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
Soil Properties and Methods of Determination
Soil Properties and  Methods of DeterminationSoil Properties and  Methods of Determination
Soil Properties and Methods of Determination
Rajani Vyawahare
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
Turbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdfTurbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdf
Totok Sulistiyanto
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).pptCONTRACTOR ALL RISK INSURANCESAR (1).ppt
CONTRACTOR ALL RISK INSURANCESAR (1).ppt
suaktonny
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
How to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using ArduinoHow to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using Arduino
CircuitDigest
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Cloud Computing concepts and technologies
Cloud Computing concepts and technologiesCloud Computing concepts and technologies
Cloud Computing concepts and technologies
ssuser4c9444
Embedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.pptEmbedded System intro Embedded System intro.ppt
Embedded System intro Embedded System intro.ppt
23ucc580
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
J. Agricultural Machinery
CFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptxCFOT Fiber Optics FOA CERTIFICATION.pptx
CFOT Fiber Optics FOA CERTIFICATION.pptx
MohamedShabana37
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Piping-and-pipeline-calculations-manual.pdf
Piping-and-pipeline-calculations-manual.pdfPiping-and-pipeline-calculations-manual.pdf
Piping-and-pipeline-calculations-manual.pdf
OMI0721
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
Soil Properties and Methods of Determination
Soil Properties and  Methods of DeterminationSoil Properties and  Methods of Determination
Soil Properties and Methods of Determination
Rajani Vyawahare
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdfCS3451-OPERATING-SYSTEM NOTES ALL123.pdf
CS3451-OPERATING-SYSTEM NOTES ALL123.pdf
PonniS7
Turbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdfTurbocor Product and Technology Review.pdf
Turbocor Product and Technology Review.pdf
Totok Sulistiyanto

7_Programming in MATLAB For Enginee.pptx

  • 2. Introduction o A computer program is a sequence of computer commands. In a simple program the commands are executed one after the other in the order they are typed o MATLAB is also a programming language. Like other computer programming languages, MATLAB has some decision making structures for control of command execution o These decision making or control flow structures include for loops, while loops, and if-else-end constructions
  • 3. Introduction o Control flow structures are often used in script M-files and function M-files o By creating a file with the extension .m, we can easily write and run programs o We do not need to compile the program since MATLAB is an interpretative (not compiled) language
  • 4. Relational and logical operators o A relational operator compares two numbers by determining whether a comparison is true or false Relational operators < : Less than > : Greater than <= : Less than or equal to >= : Greater than or equal to = = : Equal to ~= : Not Equal to
  • 5. Cont.. . Relational operators are used as arithmetic operators within a mathematical expression. The result can be used in other mathematical operations, in addressing arrays, and together with other MATLAB commands (e.g., if) to control the flow of a program When two numbers are compared, the result is 1 (logical true) if the comparison, according to the relational operator, is true, and 0 (logical false) if the comparison is false
  • 6. Cont.. . If two scalars are compared, the result is a scalar 1 or 0 If two arrays are compared (only arrays of the same size can be compared), the comparison is done element-by-element, and the result is a logical array of the same size with 1s and 0s according to the outcome of the comparison at each address If a scalar is compared with an array, the scalar is compared with every element of the array, and the result is a logical array with 1s and 0s according to the outcome of the comparison of each element
  • 7. Logical operators AND (&) Example: A&B and(A,B) Operates on two operands (A and B). If both are true, the result is true (1); otherwise the result Is false (0) OR (|) Example: A|B or(A,B) Operates on two operands (A and B). If either one, or both, are true, the result is true (1); otherwise (both are false) the result is false (0) NOT (~) Example: ~A not(A) Operates on one operand (A). Gives the opposite of the operand; true (1) if the operand is false, and false (0) if the operand is true
  • 8. Cont.. . Logical operators have numbers as operands. A nonzero number is true, and a zero number is false Logical operators (like relational operators) can be used with scalars and arrays
  • 9. Logical built-in functions xor(a,b) Exclusive or. Returns true (1) if one operand is true and the other is false all(A) Returns 1 (true) if all elements in a vector A are true (non- zero). Returns 0 (false) if one or more elements are false (zero).If A is a matrix, treats columns of A as vectors, and returns a vector with 1s and 0s any(A) Returns 1 (true) if any element in a vector A is true (nonzero). Returns 0 (false) if all elements are false (zero). If A is a matrix, treats columns of A as vectors, and returns a vector with 1s and 0s find(A) If A is a vector, returns the indices of the nonzero elements find(A>d) If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used)
  • 10. Summary INPUT OUTPUT A B AND A&B OR A|B XOR (A,B) NOT ~A NOT ~B false false false false false true true false true false true true true false true false false true true false true true true true true false false false
  • 11. Conditional Statement and Loops o In MATLAB, loops and conditional statements are control statements that allow programmers to execute code repeatedly or conditionally, and control the flow of a program o Control flow is determined by control structures o Control structure determines what gets executed
  • 12. MATLAB CONTROL STRUCTURES o MATLAB has four control structures o two for deciding between alternatives - if statements - switch statements o two for repeating (looping or iteration) - while loops - for loops o MATLAB also has implicit loops over arrays
  • 13. The if-end structure o An if statement runs the body when the condition is true o if statement are of form if <condition> <body> end if and end are keywords (cant be used as variables)
  • 14. Cont.. .. <condition> is a logical expression which can be evaluated to true or false <body> is the body of the if statement One or more statements Only executed when the condition evaluates to true
  • 15. The if-else-end Structure an else clause can be run if the condition is false if <condition> <true-body> else <false-body> end else is also a keyword <false-body> is run when the condition is false
  • 16. The if-elseif-else-end Structure the general scheme looks like if <condition1> <true-body1> elseif<condition2> <true-body2> else <false-body> end where the elseif and else clauses are optional and the elseif clause can be repeated for more conditions
  • 17. Example A worker is paid according to his hourly wage up to 40 hours, and 50% more for overtime. Write a program in a script file that calculates the pay to a worker. The program asks the user to enter the number of hours and the hourly wage. The program then displays the pay
  • 19. for-end Loops for loops simplify looping over arrays for loops iterate over each element of an array or range for <variable> = <array> <body> end <body> is run once for each element of the array
  • 20. Ex.2
  • 21. Ex.3 variable m is assigned the value 75, then 80, then 71 after each time m is assigned, the for loop body is run
  • 23. Nested for loop o when we need the index, loop over 1:length(A) o for example, to find the index of the maximum in values:
  • 24. while loop o a while loop repeats the body while the condition stays true o the general scheme looks very similar to an if statement while <condition> <body> end while and end are keywords when <condition> is false the loop will not execute again
  • 25. Cont.. . a while loop repeats the following steps - first, evaluating the conditional expression - if the condition is true, run the body - if the condition is false, jump to the statement after the body an iteration is a single execution of the body the condition is evaluated before each iteration begins sometimes the body may never get executed
  • 26. Ex.
  • 27. Note !!! not updating the loop variable is a common mistake what happens when we run this program? you can stop the infinite loop by pressing control c x is never updated in the body, so it stays at 1 the condition is never false, the loop never stops!
  • 28. Ex..
  • 29. Cont.. . the length function returns the length of a vector the loop variable i ranges from 1 up to 6 single letter loop variable, especially i, j, and k are common