Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Lab5 (Signal & System) Instructor: Anan Osothsilp Date: 20 Feb 07 Due Date 09 March 07.
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Programming with MATLAB
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow.
Basic operations in MATLAB Operation Symbol Example Addition+ 5+8 Subtraction- 5-8 Multiplication* 5*8 Division/ 5/8 Power^ 5^8 Lowest Highest 5+3/3*9.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
What is MATLAB ? MATrix LABoratory Developed by The Mathworks, Inc ( Mathworks, Inc Interactive, integrated, environment –for.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
1 Advanced MATLAB Vectors and matrices fprintf Cell arrays Structures Flow of control Vectorization Functions.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment  Command window, Workspace, Path window, Editor window,and Figure window) Basic.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Digital Image Processing Introduction to M-function Programming.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Programming Numerical Computing with. MATLAB for Scientists and Engineers.
Lecture (5) Programming (2) Eng. Osama Talaat 1. Announcement  M-Files are available:  Download the file from the course page
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 Structured Programming EEN170 Programming in MATLAB.
CSE123 - Lecture 4 Structured Programming- Loops.
Beginning Programming for Engineers Matlab Conditional Computation.
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Control Statements in Matlab
ECE 1304 Introduction to Electrical and Computer Engineering
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Introduction to Matlab LAB 4
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Conditional Logic Presentation Name Course Name
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Repetition (While Loop) LAB 9
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
REPETITION Why Repetition?
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql

For loops To run a command more than once as an index varies, you may use a for loop. % display of numbers for i = 1:9 disp(i) End % 6! fact = 1; for i = 2:6 fact = fact * i; end disp(fact) ENG. Ahmad Alaql2

Ex 1: Write m-file to compute the factorial of any integers ! ENG. Ahmad Alaql3

The Solution : N= input('Enter the number to calculate the factorial: '); fact=1; for i=2:N fact=fact*i; end disp(fact) ENG. Ahmad Alaql4

5 % Calculate the Square of numbers 1 to 5 for i = 1:5 disp([int2str(i) ' squared equals ' int2str(i^2)]) end Note : INT2STR used to convert integer to string.

Summing Series total = 0; for n = 1:6 total = total + 2ˆn; end ENG. Ahmad Alaql6

7 % function function [out] = f(in1) out = in1 * sin(in1 *pi/4); % loop maxN = input('Enter the value of N : '); I(1) = f(1); for N=2:maxN I(N) = I(N-1) + f(N); end disp('Values of I_N') disp(I)

>> maxN = input('Enter the value of N : '); for N=2:maxN X= N * sin (N *pi \ 4) end disp('Values of I_N') disp(X) ENG. Ahmad Alaql8

9 EXERCISE : Calculate the summations

ENG. Ahmad Alagil10 While loop: while (condition) commands... end

Examples x=1; while x<=100 disp(x); x=x+1; end ENG. Ahmad Alaql11

ENG. Ahmad Alagil12 EX : Write out the values of x2 for all positive integer values x such that x3 < x = 1; while x^3 < 2000 disp(x^2) x = x+1; end

n = 10; while n > 0 disp('Hello World') n = n - 1; end ENG. Ahmad Alaql13

ENG. Ahmad Alagil14 IF Statement if (expression) commands... elseif (expression) commands... else commands... end

a=50; if a<10 b=a/2; else b=a/5; end ENG. Ahmad Alaql15

ENG. Ahmad Alagil16 if x >= 0 & x <= 1 f = x; elseif x > 1 & x <= 2 f = 2-x; else f = 0; end

Conditional Statements if logical_expression1 statements1 elseif logical_expression2 statements2 else statements3 end ENG. Ahmad Alaql17

if 2>4 a=5 elseif 2<1 a=6 else a=10 end ENG. Ahmad Alaql18

ENG. Ahmad Alagil19 The MATLAB Command switch clc; n=input('Please input the figure:','s') switch n case ('triangle') n=3; sum=(n-3).*(180) case ('square') n=4; sum=(n-3).*(180) case('pentagon') n=5; sum=(n-3).*(180) case ('hexagon') n=6; sum=(n-3).*(180) end

color = 'rose'; switch lower(color) case {'red', 'light red', 'rose'} disp('color is red') case 'blue' disp('color is blue') case 'white' disp('color is white') otherwise disp('Unknown color.') end ENG. Ahmad Alaql20

x=input('Enter the value of N : ') u=input('Enter the value of s : ') switch u case {'in','inch'} % multiple matches y = 2.54*x; % converts to centimeters disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)]) % disp is used to print pretty in the command window % in the above a string vector is being printed case {'m','meter'} y = x*100; % converts to centimeters disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)]) case { 'millimeter','mm'} y = x/10; disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)]) case {'cm','centimeter'} y = x; disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)]) otherwise disp (['unknown units:' units]) y = nan; % not a number end ENG. Ahmad Alaql21

ENG. Ahmad Alagil22 HW.3