5 views

Uploaded by Dusan Petrovic

Some book about Matlab

save

- CSC Quiz4 SelfQuiz
- Simpy
- c++
- Verilog HDL Operators
- Lesson07 DecisionMaking
- write up.docx
- Sample Paper Amazon
- Control Statements
- Objective C Programming the Big Nerd Ranch Guide
- REXX
- Software Development Manager
- Logic Practice
- CSClassX
- MCQ Specimen
- Second Edition 090323
- Low Level C Programming
- Lab 2
- Awesome c
- softwaretesting_2010.pdf
- Sm Oopd2 [Flow Charts 2]
- sw
- Mu Sigma Placement Papers
- Introduction to visual basic 2008
- 2573595 - Adjust Error Message M7-021
- PFMEA
- Attributes
- Course Syllabus in Software Engineering
- CSClassX
- 0001990834 POD Error Goods Movement Status
- Php-Web Designing
- cronistas
- Guía Actividades y Rúbrica de Evaluación - Actividad 5 - Proponer Solución Del Problema de Contaminación Del Suelo
- Goldfinger - Lead Sheet
- Almagro García, Antonio. El Dibujo Infantil
- Sahara Info
- Realiza Gran Cantidad de Cálculos en Coma Flotante Para Procesamiento Grafico
- COMEDK 1st Round Schedule for Engineering
- o Peixinho Faz
- NewDest Amer Elemen Test 7
- Nama
- Geokimia Larutan, Sedimentasi, Dan Batuan Sedimen ( Gias Triatamaputra_A_270110160021 )
- Asis.dosquebradas
- Intervencion Mediacion 2º CONTENIDOS
- Leyes Gases
- MBA LISA COY.docx
- 16651616
- Páginas de Croquis de inspeção e ABS 02.10.2018.pdf
- 063.pdf
- 1-2
- PAGINAS WEB HECHAS EN DRUPAL.docx
- La Farsa Judia.pdf
- LecNote317-05 Cardinal Arithmetics.pdf
- 19795026-0-PTC-Mathcad-Prime-DS.pdf
- Cara Melakukan Metode Kangguru.docx
- SH1690 Physical Science Syllabus and Subject Outline
- Vosylius S_UKN_DPV_5 k._handout 2017 is Fb
- Le genre des noms communs inanimés.doc
- english-for-work-everyday-business-english.pdf
- René Guénon_ I. Objeto de La Psicología
- Prezentacija Za Okrugli Sto_ Bauman
- Domacin Marketing
- Aerodinamika
- matlab
- Kalendar Nastave 2014 2015 Mas
- The NACA Airfoil Series
- Pregled Brojevi Racuna Banaka
- 1843 a Novel Vertical-Axis Wind Turbine for Distributed & Utility Deployment
- McLean Automatic Flight Control Systems
- Aircraft Conceptual Design - MASON
- ENG_4_1_05
- Pr 2009011 Erv
- pestan-hdpe-cevi-za-vodu
- Bf Feeder
- matlab
- Spisak
- Albert Pike - Morals and Dogma
- Mechanical vibrations
- Fibreglas Info 1 2012 En

You are on page 1of 22

**Programming in MATLAB / Chapter 7
**

Topics Covered:

1. Relational and Logical Operators

2. Conditional statements.

if–end

if-else–end

if-elseif-else–end

(c) 2003 The Ohio State University

INTRODUCTION TO PROGRAMMING

163164

**! In a simple program the commands are executed in the order
**

they are typed.

! Many situations may require that:

*

*

*

**Commands will not be executed in order.
**

Different commands are executed in different runs.

The execution of a group of commands is repeated many

times.

(c) 2003 The Ohio State University

! Read Chapter 7 in the MATLAB book. ! In the class we will only cover if-end conditional statements (this lecture) and for-end loops (next lecture).INTRODUCTION TO PROGRAMMING 163164 ! MATLAB provide tools (commands) that can be used to control the flow of a program. (c) 2003 The Ohio State University . ! Students can learn other tools from the book by themselves.

RELATIONAL OPERATORS Relational operator Meaning < Less than. ! Relational operators compare two numbers in a comparison statement. <= Less than or equal to. == Equal to. ! If the statement is false. > Greater than. it is assigned a value of 1. >= Greater then or equal to. ~= Not equal to. it is assigned a value of 0. (c) 2003 The Ohio State University 164167 . ! If the statement is true.

and assigns the answer to a. >> a=5<10 a= Since 5 is smaller than 10 the number 1 is assigned to a. 1 >> y=(6<10) + (7>8) + (5*3= =60/4) y= =1 =0 =1 2 (c) 2003 The Ohio State University . ans = 0 Checks if 5 is smaller than 10. EXAMPLES >> 5>8 Since 5 is not larger than 8 the answer is 0.165 RELATIONAL OPERATORS.

Logical Operator Name Meaning & Example: A & B AND True if both operands (A and B) are true. ~ Example: ~ A NOT True if the operand (A) is false. ! A zero number is false. ! A nonzero number is true. (c) 2003 The Ohio State University . | Example: A | B OR True if either or both operands (A and B) are true.167 LOGICAL OPERATORS ! Logical operators have numbers as operands. False if the operand (A) is true.

3 and 7 are both true (nonzero). 1 >> x=-2. a= 1 is assigned to a since at least one number is true (nonzero). so the outcome is 1. Mathematically correct. y=5. -5<x is true (=1) and then 1<-1 is false (0).LOGICAL OPERATORS. EXAMPLES >> 3&7 ans = 168169 3 AND 7. The mathematically correct statement is obtained by using the logical operator &. 1 >> a=5|0 5 OR 0 (assign to variable a). The answer is false since MATLAB executes from left to right. >> -5<x<-1 ans = 0 >> -5<x & x<-1 ans = 1 Define variables x and y. (c) 2003 The Ohio State University . Since both are true (1). The inequalities are executed first. the answer is 1.

either nothing is done. I will study harder so that I can get a better job. If the condition is met. buy a new car. ! A condition stated. one set of actions is taken. If the condition is not met. I will quit college. or a second set of actions is taken. Example: If I win the Lottery.CONDITIONAL STATEMENTS 172177 ! Conditional statements enable MATLAB to make decisions. (c) 2003 The Ohio State University . ! The process is similar to the way we (humans) make decisions. If I do not win the Lottery. and go fishing.

172 THE FORM OF A CONDITIONAL STATEMENT If Conditional expression consisting of relational and/or logical operators Examples: if a < b if c >= 5 if a == b if a ~= 0 if (d<h)&(x>7) if (x~=13)|(y<0) (c) 2003 The Ohio State University All variables must have assigned values. .

THREE FORMS OF THE if STATEMENT If conditional statement if conditional statement 1 commands end 172177 command group 1 elseif conditional statement 2 command group 2 if conditional statement command group 1 else command group 3 else command group 2 end end (c) 2003 The Ohio State University .

......THE if–end STATEMENT False . .... . end ............ end (c) 2003 The Ohio State University ..... if conditional expression True ....... if 172174 MATLAB program.. ..... . ..... MATLAB program.......... .. A group of MATLAB commands...

% The program calculates the average of the grades.EXAMPLE OF USING THE if–end STATEMENT % A script file that demonstrates the use of the if-end statement. is printed. score = input('Enter (as a vector) the scores of the three tests '). disp('The average grade is:') disp(ave_grade) if ave_grade < 60 disp('The student did not pass the course. a massage: % The student did not pass the course. % If the average is less than 60. ave_grade = (score(1) + score(2) + score(3))/3. % The user is asked to enter three grades.') end (c) 2003 The Ohio State University .

EXAMPLE OF USING THE if–end STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example1 Enter (as a vector) the scores of the three tests [78 61 85] The average grade is: 74. (c) 2003 The Ohio State University .6667 >> Lecture8Example1 Enter (as a vector) the scores of the three tests [60 38 55] The average grade is: 51 The student did not pass the course.

............ ....... .. False 174175 MATLAB program.... ....... ........ ..THE if–else-end STATEMENT ...... .... MATLAB program... Group 1 of MATLAB commands.. (c) 2003 The Ohio State University ...... Group 2 of MATLAB commands....... . else end .... if conditional expression if True end ..

If the average is less than 60. is printed. is printed. ave_grade = (score(1) + score(2) + score(3))/3. The program calculates % the average of the grades. disp('The average grade is:') disp(ave_grade) if ave_grade < 60 disp('The student did not pass the course.') else disp('The student passed the course. % The user is asked to enter three grades. % Otherwise.EXAMPLE OF USING THE if–else-end STATEMENT % A script file that demonstrates the use of the if-else-end statement. a massage: The student passed the course. a % massage: The student did not pass the course.') end (c) 2003 The Ohio State University . score = input('Enter (as a vector) the scores of the three tests ').

EXAMPLE OF USING THE if–else-end STATEMENT Executing the script file of the previous slide in the Command Window: >> Lecture8Example2 Enter (as a vector) the scores of the three tests [65 80 83] The average grade is: 76 The student passed the course. >> Lecture8Example2 Enter (as a vector) the scores of the three tests [60 40 55] The average grade is: 51.6667 The student did not pass the course. (c) 2003 The Ohio State University .

... if conditional expression if True True ..... .......... commands. (c) 2003 The Ohio State University MATLAB program.......... end ....... Group 1 of MATLAB ... commands.. elseif conditional expression .. ..... ... .... Group 2 of MATLAB ........... end Group 2 of MATLAB commands...... else .. ...... ............176THE if–elseif-else-end STATEMENT 177 .. False False elseif MATLAB program........

% The program calculates the tip in a restaurant according to the % amount of the bill.EXAMPLE OF USING THE if–elseif-else-end STATEMENT % A script file that demonstrates the use of the if-elseif-else-end % statement. % If the bill is less than 10$ the tip is $1. format bank clear tip (The file continues on the next slide) (c) 2003 The Ohio State University . % Above $60 the tip is 20% of the bill. % Between $10 and $60 the tip is 18% of the bill.80.

else tip = bill*0. if bill <= 10) tip = 1. end disp('The tip is (in dollars):') disp(tip) (c) 2003 The Ohio State University .8. elseif (bill > 10) & (bill <= 60) tip = bill*0.2.18.(Continuation from the previous slide) bill = input('Enter the amount of the bill (in dollars): ').

00 (c) 2003 The Ohio State University .80 >> Lecture8Example3 Enter the amount of the bill (in dollars): 100 The tip is (in dollars): 20.70 >> Lecture8Example3 Enter the amount of the bill (in dollars): 6 The tip is (in dollars): 1.EXECUTING THE SCRIPT FILE OF THE RESTAURAT TIP CALCULATION >> Lecture8Example3 Enter the amount of the bill (in dollars): 15 The tip is (in dollars): 2.

if – else – end.COMMENTS ABOUT if–end STATEMENTS ! For every if command a computer program must have an end command.end. end statements following each other. ! A program can have many if …. ! A computer program can perform the same task using different combinations of if . and if – elseif – else – end statements.. (c) 2003 The Ohio State University 172177 .

Problem 8. Chapter 7. MATLAB book. Problem 16. Chapter 7. The first line in the script file. Problem 1. MATLAB book. MATLAB book. and in the Command Window should be a comment with your name. (c) 2003 The Ohio State University . 3. and a printout of the Command Window showing how the script file was used. Chapter 7. 2. In problems 2 and 3 submit a printout of the script file. In problem 1 submit a printout of the Command Window.MATLAB ASSIGNMENT 8: 199202 1.

- CSC Quiz4 SelfQuizUploaded bytopherski
- SimpyUploaded byRoberto Solar
- c++Uploaded byjacobpauls
- Verilog HDL OperatorsUploaded byWellison Rodrigues
- Lesson07 DecisionMakingUploaded byLJ Parfan
- write up.docxUploaded byNiranjana Karandikar
- Sample Paper AmazonUploaded byAnkit Mittal
- Control StatementsUploaded bygen_estrada_1
- Objective C Programming the Big Nerd Ranch GuideUploaded bytempofm9
- REXXUploaded byRamesh Krishnan Rajavelu
- Software Development ManagerUploaded bypatricia chipana
- Logic PracticeUploaded byanuparyan2008
- CSClassXUploaded byFarooqHaiderButt
- MCQ SpecimenUploaded byTanveer Akram
- Second Edition 090323Uploaded byWounded_god
- Low Level C ProgrammingUploaded byBogdan Luca
- Lab 2Uploaded byradians042
- Awesome cUploaded byRiham Abdallah
- softwaretesting_2010.pdfUploaded byRamesh Dua
- Sm Oopd2 [Flow Charts 2]Uploaded byAshish Chopra
- swUploaded byEngr Syed Yahya
- Mu Sigma Placement PapersUploaded byDwarak Chaluvadi
- Introduction to visual basic 2008Uploaded byAruna Kumarasiri
- 2573595 - Adjust Error Message M7-021Uploaded byJorge
- PFMEAUploaded byMegha Bafna
- AttributesUploaded bytt3340
- Course Syllabus in Software EngineeringUploaded byricardocabugao
- CSClassXUploaded byFarooqHaiderButt
- 0001990834 POD Error Goods Movement StatusUploaded bybacankasiruta2014
- Php-Web DesigningUploaded byrasihavini

- Domacin MarketingUploaded byDusan Petrovic
- AerodinamikaUploaded byDusan Petrovic
- matlabUploaded byDusan Petrovic
- Kalendar Nastave 2014 2015 MasUploaded byDusan Petrovic
- The NACA Airfoil SeriesUploaded byDusan Petrovic
- Pregled Brojevi Racuna BanakaUploaded byDusan Petrovic
- 1843 a Novel Vertical-Axis Wind Turbine for Distributed & Utility DeploymentUploaded byDusan Petrovic
- McLean Automatic Flight Control SystemsUploaded byDusan Petrovic
- Aircraft Conceptual Design - MASONUploaded byDusan Petrovic
- ENG_4_1_05Uploaded byDusan Petrovic
- Pr 2009011 ErvUploaded byDusan Petrovic
- pestan-hdpe-cevi-za-voduUploaded byDusan Petrovic
- Bf FeederUploaded byDusan Petrovic
- matlabUploaded byDusan Petrovic
- SpisakUploaded byDusan Petrovic
- Albert Pike - Morals and DogmaUploaded byDusan Petrovic
- Mechanical vibrationsUploaded byDusan Petrovic
- Fibreglas Info 1 2012 EnUploaded byDusan Petrovic