Dr. Jie Zou PHY 33201 Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Flow Charts, Loop Structures
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Dr. Jie Zou PHY Chapter 9 Ordinary Differential Equations: Initial-Value Problems Lecture (I) 1 1 Besides the main textbook, also see Ref.: “Applied.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
MatLab – Palm Chapter 4, Part 3 For and While Loops
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
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.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Digital Image Processing Lecture 6: Introduction to M- function Programming.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
Digital Image Processing Introduction to M-function Programming.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
1 Structured Programming EEN170 Programming in MATLAB.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana
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.
EEE 161 Applied Electromagnetics
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
Chapter 4 MATLAB Programming
Programming with MATLAB
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
Ch 7: JavaScript Control Statements I.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Introduction to Matlab LAB 4
244-2: MATLAB/Simulink FUNDAMENTALS
Using Script Files and Managing Data
Matlab Basics.
REPETITION Why Repetition?
Presentation transcript:

Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 3.

Dr. Jie Zou PHY Outline M-files Script files* Function files Interactive Input and output Enter information Display result Structured programming Decisions (or Selection) Loops (or Repetition)

Dr. Jie Zou PHY M-files M-files: An M-file is a text file stored with a.m extension. To create a new M-file: Go to File, New, M-file. Type in the commands line by line in the edit window. Save the file as filename.m. To execute an M-file: Type the filename without.m in the command window. Two types of M-files: Script files: A script file is a set of MATLAB commands that are saved on a file – we consider script files mainly in this course. Function files: Read and 3.5 of the hand-out.

Dr. Jie Zou PHY Example: bungee- jumper-1.m t=[0:2:20]’; %Time(s) g=9.81; %Acceleration due to gravity(m/s^2) m=68.1; %Mass (kg) cd=0.25; %Drag coefficient (kg/m) v=sqrt(g*m/cd)*tanh(sqrt(g*cd/ m)*t); figure; %To open a figure window plot(t, v) title(‘Plot of v versus t’) xlabel(‘Values of t’) ylabel(‘Values of v’) Develop a script file to compute and plot the velocity v of the bungee jumper as a function of time t. Set t = 0 to 20 s in steps of 2. Save it as bungee- jumper-1.m Execute the file. What are the values of v at t = 0, 10, and 20 s?

Dr. Jie Zou PHY Interactive Input and output The input command: n = input('promptstring’): Prompt the user to enter a value from the keyboard n = input('promptstring','s'): Prompt the user to enter a string from the keyboard Examples: >> m = input(‘Mass (kg): ‘);  >> myname = input(‘My name is: ‘,’s’);  The display command: display(value) : value = a numeric value, a variable, or a string enclosed in ‘ ‘. Examples: >> display(m)>> display(myname)

Dr. Jie Zou PHY Example: bungee- jumper-2.m g=9.81; %Acceleration due to gravity(m/s^2) m=input(‘Mass (kg): ‘); cd=input(‘Drag coefficient (kg/m): ‘); t=input (‘Time (s): ‘); v=sqrt(g*m/cd)*tanh(sqrt(g*cd/ m)*t); disp(‘ ‘) disp(‘Velocity (m/s):’) disp(v) In the bungee-jumper example, create a script file that prompts the user to enter the values for the mass, m, in kg, the drag coefficient, c d, in kg/m, and the time, t, in s. Also, compute and display the value for the velocity, v, at the specified time instant. Save the file as bungee- jumper-2.m. Run the file.

Dr. Jie Zou PHY Structured programming: Decisions (1) The if Structure: Syntax: if condition statements end Example: Write an M- file (Grade.m) to evaluate whether a grade is passing. See the flowchart. grade >= 60 T Output: displayed message (‘passing grade’) Input: grade = numerical value (0 -100) F

Dr. Jie Zou PHY Structured programming: Decisions (cont.) (2) The if…else Structure: Syntax: if condition statements 1 else statements 2 end Example: Modify Grade.m to evaluate whether a grade is passing or not passing. See the flowchart. grade >= 60 T Output: displayed message (‘passing grade’) Input: grade = numerical value (0 -100) F Output: displayed message (‘Not passing grade’)

Dr. Jie Zou PHY Structured programming: Decisions (cont.) (3) The if…elseif Structure: Syntax if condition 1 statements 1 elseif condition 2 statements 2. else statements else end Example: Write an M-file (mysign.m) to do the following: Input x; If x > 0, display ‘sign = 1’; if x < 0, display ‘sign = -1’; otherwise, display ‘sign = 0’.

Dr. Jie Zou PHY Relational operators in MATLAB ExampleOperatorRelationship x == 0== Equal unit ~= ‘m’~= Not equal a < 0< Less than s > t> Greater than 3.9 <= a/3<= Less than or equal to r >= 0>= Greater than or equal to

Dr. Jie Zou PHY Logical operators in MATLAB xy~x (Not)x & y (And)x | y (Or) TTFTT TFFFT FTTFT FFTFF Priority of the operators: Highest Lowest

Dr. Jie Zou PHY Structured programming: Loops (1) The for…end Structure: Syntax: for index = start:step:finish statements end Note: If an increment of 1 is desired, the step can be dropped. step can be any numeric value, integer or non-integer, positive or negative. Example: Write your own M-file (mysum.m) to compute the following sum using a for loop: … 100 Also, check the MATLAB built-in function sum.

Dr. Jie Zou PHY Structured programming: Loops (cont.) (2) The while structure: A while loop repeats as long as a logical condition is true. Syntax: while condition statements end Example: What will be displayed? x = 8 while x > 0 x = x – 3; disp(x) end

Dr. Jie Zou PHY Structured programming: Loops (cont.) (3) The while…break loop: It allows loop termination on a true condition anywhere in the loop. Syntax: while (1) statements if condition, break, end statements end Example: What will be the final x value? x = 8; while (1) x = x – 5; if x < 0, break, end end