Download presentation
Presentation is loading. Please wait.
Published byClifton Ross Modified over 9 years ago
1
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
2
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
3
Result This file will run, with the following plot produced: Engineering Computation: An Introduction Using MATLAB and Excel
4
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
5
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
6
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
7
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
8
Result Error message What is this message telling us? Engineering Computation: An Introduction Using MATLAB and Excel
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
Result File runs; here is the plot Engineering Computation: An Introduction Using MATLAB and Excel
18
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
19
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
20
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
21
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
22
Indenting Helps Troubleshooting Note the indenting that the Editor does automatically: Engineering Computation: An Introduction Using MATLAB and Excel
23
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
24
Now it Works Engineering Computation: An Introduction Using MATLAB and Excel
25
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.