Engineering Fundamentals: An Introduction to Engineering, 4th ed.c

(Steven Felgate) #1

15.2 Using MATLAB Built-in Functions 479


■Figure 15.9 The M-file for Example 15.3.


■Figure 15.10 The results of Example 15.3.


Next, we will write a computer program using an M-file that asks a user to input a value for n
and computes the sum of 1 through n. To make the program interesting, we will not make
use of Pascal’s formula; instead, we will use a forloop to solve the problem. We have used
MATLAB’s Editor to create the program and have named it For_Loop_Example.m, as shown
in Figure 15.9. In the shown program, the % symbol denotes comments, and any text follow-
ing the % symbol will be treated as comments by MATLAB. Also, note that you can find the
Line (Ln) and Column (Col) numbers corresponding to a specific location in your program by
moving the cursor. The line and the column numbers are shown in the right side, bottom cor-
ner of the Editor window. As you will see, the knowledge of line and column numbers are use-
ful for debugging your program. We run the program by clicking on Debug SSSave File and
Run, and the result is shown in Figure 15.10.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀

Free download pdf