Chapter 4: Programming with MATLAB

Similar documents
SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

Branches, Conditional Statements

An Introduction to MATLAB

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

A Tutorial for ECE 175

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Desktop Command window

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

This is the basis for the programming concept called a loop statement

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

C/C++ Programming for Engineers: Matlab Branches and Loops

Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion of this lesson)

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

MATLAB Operators, control flow and scripting. Edited by Péter Vass

Programming for Experimental Research. Flow Control

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

7 Control Structures, Logical Statements

Introduction to LabVIEW

Part 1. Summary of For Loops and While Loops

Chapter 1: An Overview of MATLAB

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Introduction to Programming for Biology Research

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15

APPM 2460: Week Three For, While and If s

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

User Defined Functions

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Loops. While. Loops can be used:

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Lab 1 Intro to MATLAB and FreeMat

function [s p] = sumprod (f, g)

Contents I Introduction 1 Introduction to PL/SQL iii

Introduction. C provides two styles of flow control:

Computational Finance

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Iteration in Programming

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Practical 4: The Integrate & Fire neuron

ME 1020 Engineering Programming with MATLAB. Chapter 1 In-Class Assignment: 1.1, 1.3, 1.13, Topics Covered:

Dept. of Electrical, Computer and Biomedical Engineering. Data Acquisition Systems and the NI LabVIEW environment

Engineering Innovation Center LabVIEW Basics

CompSci 94 Making Decisions February 8, Prof. Susan Rodger

Files and File Management Scripts Logical Operations Conditional Statements

Introduction to programming in MATLAB

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

MATLAB 1. Jeff Freymueller September 24, 2009

Introduction to MATLAB

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Optimization Problems and Wrap-Up. CS 221 Lecture 14 Tue 6 December 2011

MATLAB SUMMARY FOR MATH2070/2970

Python Activity 7: Looping Structures WHILE Loops

Lecture Transcript While and Do While Statements in C++

Euler s Method with Python

Mini-Matlab Lesson 5: Functions and Loops

CS 106 Introduction to Computer Science I

What We Will Learn Today

Dr. Iyad Jafar. Adapted from the publisher slides

Repetition Structures Chapter 9

Summary of the Lecture

BIL101E: Introduction to Computers and Information systems Lecture 8

An Introduction to MATLAB

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Flow of Control: Loops

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

CS 221 Lecture. Tuesday, 11 October 2011

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Chapter 4 Branching Statements & Program Design

MEDIA COMPUTATION DRJAVA. Lecture 11.3 November 7, 2008

To see what directory your work is stored in, and the directory in which Matlab will look for files, type

MATLAB Second Seminar

APCS Semester #1 Final Exam Practice Problems

INTRODUCTION OF LOOPS

Matlab Advanced Programming. Matt Wyant University of Washington

Selection Statements. Chapter 4. Copyright 2013 Elsevier Inc. All rights reserved 1

CS 106 Introduction to Computer Science I

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Decision Structures CSC1310. Python Programming, 2/e 1

Lesson Seven: Holding Gestures

Chapter Goals. Contents LOOPS

R Demonstration Summary Statistics and the Law of Large Numbers

Finding MATLAB on CAEDM Computers

What is MATLAB? Try out the Command Window. How is MATLAB similar to Python? Sample comparison code INTRODUCTION TO MATLAB

4.0 Programming with MATLAB

A. Front Panel Design Lesson 4 Implementing a VI

LECTURE 5 Control Structures Part 2

Self-test Programming Fundamentals

Matlab Tutorial: Basics

MATLAB TUTORIAL WORKSHEET

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

PSpice Tutorial. Physics 160 Spring 2006

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Score score < score < score < 65 Score < 50

Lecture 2 Matlab Environment

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

ENGR 1181 MATLAB 09: For Loops 2

Transcription:

Chapter 4: Programming with MATLAB Topics Covered: Programming Overview Relational Operators and Logical Variables Logical Operators and Functions Conditional Statements For Loops While Loops Debugging MATLAB programs

Programming Overview: MATLAB programming can be used to solve very computationally intensive problems which may require thousands or hundreds of thousands of calculations. Operations can be: Sequential: Calculations are executed in order from the top down. Conditional: Calculations are made based on the answer (either true or false) to a question. Iterative: Calculations are made over and over until a condition is met. Sequential Conditional (If-Then) Iterative (For Loops) Iterative (While Loops) Statement Group 1 Statement Group 2 Statement Group 3

Relational Operators and Logical Variables: Relational Operators make comparisons between numbers or arrays. The result of a comparison is either: = 0 (if the comparison is false) or = 1 (if the comparison is true) The result can be used as a variable. When used to compare arrays, the relational operators compare the arrays on an element-by-element basis. The arrays must have the same dimension. When comparing an array to a scalar, all of the elements of the array are compared to the scalar.

Problem 4.4: Suppose that x = 6. Find the results of the following operations by hand and use MATLAB to check your results.

Problem 4.6: Suppose that x = 10, 2, 6, 5, 3 and y = 9, 3, 2, 5, 1. Find the results of the following operations by hand and use MATLAB to check your results.

Problem 4.8: Use the find and length commands What values are over 20? Use MATLAB to find them.

Logical Operators and Functions: MATLAB has five Logical Operators (or Boolean Operators).

Problem 4.12:

Problem 4.12: Step 1: Create plots of the height and speed versus time.

Problem 4.12: Step 2: Use the MATLAB relational and logical operators to find the times when the height is no less than 15 m.

Problem 4.12: Step 3: Use the MATLAB relational and logical operators to find the times when the height is no less than 15 m and the speed is simultaneously no greater than 36 m/s.

If Statements: The Conditional Operators (If Statements) use Relational Operators: < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to The result of these Relational Operators is either True or False (1 or 0). This can be used to control the flow of a program. This is called Logic Flow, which can be represented by a Flowchart.

If Statements: Create the following MATLAB program. Once you ve checked that it is working correctly, switch the values of x and y.

Nested If Statements: Create the following MATLAB program. In order to compute the value of z, both Logical Expressions must return a True value. Once you ve checked that it is working correctly, change the values of x and y to zero. x > 0 y > 0 Compute z

If-Else Statements: If-Else Statements provide two options based on the result of the Logical Expression. Create the following MATLAB program. Once you ve checked that it is working correctly, change the value of x to 50.

If-Elseif-Else Statements: This is the General Form of the if statement. Create the following MATLAB program. Once you ve checked that it is working correctly, change the value of x to 5 and then to 7.

Problem 4.16: Write a script file using Conditional If-Elseif-Else statements to evaluate the following function, assuming that x = 2, 0, and 6. The function is: y = for x < 1 2 + cos πx for 1 x 5 10 x 5 + 1 for x > 5 e x+1

For Loop: The For Loop is a structure that repeats a set of commands or calculations a specified number of times. Create the following MATLAB program. In this case, the variable x is a scalar.

For Loop: The For Loop can be used to Load a Vector with values. The Counter k is used to Index through the vector elements. What happens if you don t Initialize x? [Comment out x(1) = 3 and rerun] How many elements does x have? What change would you make if you wanted x to have only 3 elements?

Debugging Tool: Click on the Horizontal Lines to the right of the line numbers. This creates Breakpoints on the lines of code. When you hit Run, the Editor Bar changes to the following:

Debugging Tool: The Editor Window and the Command Window change as well. The Green Arrow indicates the line that is going to be executed next:

Debugging Tool: Click on the Continue button to step through the program. The values of the variables appear in the Command Window. Notice how the vector x is loaded as you step through the for loop. You can stop execution and Debugging by clicking on the red Quit Debugging button.

Debugging Tool: Click on the Breakpoints/Clear All tab to delete all of the breakpoints.

Problem 4.22:

Problem 4.27:

Problem 4.27: Step 1: Create a vector for Time t, use it to calculate the Supply Voltage v_s, and plot v_s versus t.

Problem 4.27: Step 2: Create an if statement that can calculate the Load Voltage for a given Supply Voltage. Test the if statement at the following time values:

Problem 4.27: Step 3: Use a for loop with an if statement to load the Load Voltage vector v_l. Plot both the supply voltage v_s and the load voltage v_l versus t.

While Loops: The While Loop is a structure that repeats a set of commands or calculations until the Logical Expression condition is met. The number of iterations through the loop is unknown prior to starting the program. Create the following MATLAB program. Use the Debugging Tool to step through the program. In this case, the variable x is a scalar.

While Loops: Change the While Loop as shown below. Use the Debugging Tool to step through the program. In this case, the variable x is a Vector. Notice how the Counter k is used to Load the x Vector.

Problem 4.32:

Example Problem: Write a While Loop to plot the following function over the range 2 x 6. y = for x < 1 2 + cos πx for 1 x 5 10 x 5 + 1 for x > 5 e x+1