Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Flow Charts, Loop Structures
Programming in Visual Basic
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Linear Simultaneous Equations
Chapter 5 Plotting Data Types of Graphs, Plotting with Excel Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrix Mathematics in MATLAB and Excel
Chapter 9 Numerical Integration Numerical Integration Application: Normal Distributions Copyright © The McGraw-Hill Companies, Inc. Permission required.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Non-Linear Simultaneous Equations
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6 Section 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives 1 The Fundamental Property of Rational Expressions Find the numerical.
Chapter 9 Numerical Integration Flow Charts, Loop Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
TH EDITION Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 1 Equations and Inequalities Copyright © 2013, 2009, 2005 Pearson Education,
Chapter 6 Finding the Roots of Equations
Chapter 1 Computing Tools Analytic and Algorithmic Solutions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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.
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
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 for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Working with Arrays in MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1.5 Solving Inequalities Remember the rules of solving inequalities.
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
Newton’s Method, Root Finding with MATLAB and Excel
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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Chapter 15 General Least Squares and Non- Linear.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
Copyright © 2014, The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. This work is only for non-profit use by instructors in courses for which this textbook has been adopted.
Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Checking Possible Solutions
Chapter 7 Matrix Mathematics
Chapter 4 MATLAB Programming
Chapter 4 MATLAB Programming
Linear Inequalities and Absolute Value
Matlab review Matlab is a numerical analysis system
Rational Expressions and Functions
What is an equation? An equation is a mathematical statement that two expressions are equal. For example, = 7 is an equation. Note: An equation.
Copyright © 2017, 2013, 2009 Pearson Education, Inc.
Graphs, Linear Equations, and Functions
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.
Working with Arrays in MATLAB
Presentation transcript:

Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Troubleshooting Examples We want to plot this equation for time values from zero to 10 seconds: for k = 0:10 t = k; y = 200*exp(-3*t); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result This file will run, with the following plot produced: Engineering Computation: An Introduction Using MATLAB and Excel

Problem The plot command creates a graph from two vectors In this case, our variables to be plotted, t and y, are scalars Therefore, there is only a single point to be plotted Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting Examples Modified to store results in vectors Is this file OK? for k = 0:10 t(k) = k; y(k) = 200*exp(-3*t); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result Error message: Note that it is OK for the loop counter to begin at zero (for k = 0:10), but the array index cannot be zero Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting Examples Modified so that the array indices begin with one Is this file OK? for k = 1:10 t(k) = k; y(k) = 200*exp(-3*t); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result Error message What is this message telling us? Engineering Computation: An Introduction Using MATLAB and Excel

Problem This message is harder to interpret, but relates to the fact that t has been assigned as an array, and is now being used as a scalar Engineering Computation: An Introduction Using MATLAB and Excel t(k) = k; y(k) = 200*exp(-3*t); ARRAY SCALAR

Troubleshooting Examples Modified so that reference to t is written as an array Is this file OK? for k = 1:10 t(k) = k; y(k) = 200*exp(-3*t(k)); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result File runs; here is the plot Note that the first point plotted is for t = 1 second, not t = 0 Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting Examples Modified so that time begins with zero. The for loop executes 11 times rather than 10 to include both endpoints Is this file OK? for k = 1:11 t(k) = k-1; y(k) = 200*exp(-3*t(k)); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result File runs; here is the plot We have now solved the problem as presented. Let’s now modify our solution so that the time domain is from 0 to 2 seconds, and include more points to produce a smooth curve Engineering Computation: An Introduction Using MATLAB and Excel Note y  0 at t = 2 seconds

Modifying Time Domain If we want to plot 101 points (for k = 1:101) for a time domain of zero to 2 seconds, what will the expression for t(k) be? for k = 1:101 t(k) = ? Engineering Computation: An Introduction Using MATLAB and Excel

Modifying Time Domain for k = 1:101 t(k) = ? The interval between time steps will be (2 seconds) / (100 steps) = 0.02 seconds/step The first value of time t(1) = 0 = k-1 So t(k) = (k-1)*(0.02) Check: t(101) = 100*0.02 = 2 seconds Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting Examples Is this file OK? for k = 1:101 t(k) = (k-1)*(0.02); y(k) = 200*exp(-3*t(k)); end plot (t,y) Engineering Computation: An Introduction Using MATLAB and Excel

Result File runs; here is the plot Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting Examples Here is the m-file that we wrote earlier to create an identity matrix. Can you find three errors? Size = 5; for k = 1:Size for l = 1: size if k = l A(k,l) = 1; else A(k,l) = 0; end End A Engineering Computation: An Introduction Using MATLAB and Excel

Error #1 This is a common error: when comparing two values in the if statement, you must use a double equal sign (==) rather that a single equal sign, since that is the assignment operator Engineering Computation: An Introduction Using MATLAB and Excel

Error #2 The last end statement is not recognized because it is capitalized. Note that the Editor shows the other for, if, else, and end statements in blue Engineering Computation: An Introduction Using MATLAB and Excel

Error #3 We are still missing one end statement. Every loop and if statement must have an end statement. Add an end after line 7 (following if-else) Engineering Computation: An Introduction Using MATLAB and Excel

Indenting Helps Troubleshooting Note the indenting that the Editor does automatically: Engineering Computation: An Introduction Using MATLAB and Excel

Error #4 In line 3, “size” is not recognized. Remember that MATLAB variables are case-sensitive. Change to “Size” Engineering Computation: An Introduction Using MATLAB and Excel

Now it Works Engineering Computation: An Introduction Using MATLAB and Excel

Troubleshooting MATLAB Files Although MATLAB’s error messages can help you find problems, it is much more efficient to plan programs carefully and add comment lines to help you later Engineering Computation: An Introduction Using MATLAB and Excel