Matlab Programming for Engineers

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
C++ for Engineers and Scientists Third Edition
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
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.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Problem Solving We now have enough tools to start solving some problems. For any problem, BEFORE you start writing a program, determine: –What are the.
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)
Branches and Program Design
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
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.
Matlab Programming for Engineers
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
ITEC 2600 Introduction to Analytical Programming
ECE 1304 Introduction to Electrical and Computer Engineering
EEE 161 Applied Electromagnetics
Computer Application in Engineering Design
Signals in Matlab Matlab as a name stands for Matrix Laboratory.
Chapter 4: Making Decisions.
Selection Statements by Ahmet Sacan
Chapter 4: Making Decisions.
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 4: Making Decisions.
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
Introduction to MATLAB
Lecture 3 MATLAB programming (1)
Conditional Statements
Lecture 10 2D plotting & curve fitting
IF if (condition) { Process… }
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Selection Statements Chapter 4 Attaway MATLAB 4E.
Visual Basic – Decision Statements
Introduction to Matlab LAB 4
Selection Statements.
Loop Statements & Vectorizing Code
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.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Linear Algebra Lecture 6.
EECE.2160 ECE Application Programming
Chapter 3: Selection Structures: Making Decisions
Matlab Basics.
Loop Statements & Vectorizing Code
Selection Statements Chapter 4 Attaway MATLAB 5E.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Matlab Programming for Engineers Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Nidal Farhat

Relational Operators (==, ~=, >, < , >=, <=) OBJECTIVES The Logical Data Type Relational Operators (==, ~=, >, < , >=, <=) Logic Operators (&, &&, |, ||, xor, ~) Logical Functions (ischar, isempty, ….) Branches (if-else) Statement switch Statement try-catch Statement Additional Plotting Features

THE LOGICAL DATA TYPE (true, false)

RELATIONAL OPERATORS (==, ~=, >, < , >=, <=) Some relational operations and their results

RELATIONAL OPERATORS (==, ~=, >, < , >=, <=) Be careful because of roundoff errors Compare array by a value Compare array by another one (of the same size)

LOGIC OPERATORS Operator Operation & Logical AND (scalar/array, both sides evaluation) && Logical AND (scalar, left then right side evaluation) | Logical inclusive OR (scalar/array both sides evaluation) || Logical inclusive OR (scalar, left then right side evaluation) xor Logical exclusive OR ~ Logical NOT

LOGIC OPERATORS EXAMPLE

LOGICAL FUNCTIONS

QUIZ!

Branches: a) if Statement

BRANCHES (if-STATEMENT) Optional

BRANCHES (if-STATEMENT) Example: Write a program that determine whether a quadratic equation: has real (identical/different) or complex roots. (Use input command for entering the coefficients (a,b,c) and disp commands to display the output). Write a program that calculates the roots in each case. (Use input command for entering the coefficients of the equation and disp commands to display the output). Test the program on the following equations:

BRANCHES (if-STATEMENT) Example: evaluating a Function of Two Variables Test it using the following (x,y) values:

BRANCHES (if-STATEMENT) Nested if-Statements It is preferable to use if-else-end statement whenever is possible

Branches: b) switch Statement

BRANCHES (switch-STATEMENT) The switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives. The switch_expr and case_expr may be a scalar or a string Optional

BRANCHES (switch-STATEMENT) Remarks: The case expression could be more than one enclosed by {} At most one code block can be executed. If the switch_exp matches more than case_exp the first one is executed.

BRANCHES (switch-STATEMENT) Example:

BRANCHES (switch-STATEMENT) Example: Example

BRANCHES (switch-STATEMENT) Example

b) try/catch Statement Branches: b) try/catch Statement

BRANCHES (try/catch) The try/catch statement is designed to trap errors. In MATLAB when an error is encountered while running the program the program execution is aborted. Use try/catch statement to run a part of the program susceptible to produce error when executed. MATLAB will try to execute this part. If an error is produced, then instead of aborting, it executes the part of the program found in the catch statement. If no error is produced in the try block, the catch block will not be executed.  The programmer can handle errors in the program without causing the program to stop.

BRANCHES (try/catch)

BRANCHES (try/catch) Example

fprintf command fprintf(format,data)

Additional plotting features Example f(x) = sin(x), from -2π to 2π Then restrict the axes to the region 0 ≤ x ≤ π, and 0 ≤ y ≤ 1

Additional plotting features Plotting Multiple Plots on the Same Axes OR

Additional plotting features Creating Multiple Figures

Additional plotting features Subplots Subplot(m,n,p) ≡ creates mxn subplots in the current figure (array of subplots), m rows and n columns. p is the current subplot. p=1 p=2 p=3 p=4 p=[5 6]

Additional plotting features Enhanced Control of Plotted Lines plot(x,y,'PropertyName',value,…)

Additional plotting features Enhanced Control of Plotted Lines Example,

Additional plotting features Enhanced Control of Text Strings

Additional plotting features Enhanced Control of Text Strings

Additional plotting features Enhanced Control of Text Strings Examples:

Additional plotting features Polar Plots polar(theta,r) Example,

Additional plotting features Polar Plots polar(theta,r) Solution: