Introduction to Engineering – 7 MATLAB Script Files - 2

advertisement
Introduction to Engineering
MATLAB – 7
Script Files - 2
Agenda
 Script files continued
EXAMPLE
Write a script file that determines the balance in a saving account at
the end of every year for the first 10 years.
Initial investment of
$1,000 and interest rate of 6.5% compounded annually.
Display the information in a table.
For an initial investment of A, and interest rate of r, the balance B
after n years is given by:
B  A 1  r 
n
SOLUTION
Referenced in first
MATLAB lecture
format bank
yr = [1:10];
% Creating a vector of year numbers.
balance = 1000*1.065.^yr;
% Creating a vector of the balance.
table_yr_blnc(:,1)=yr';
% Substituting the yr vector in the first column of the table matrix.
table_yr_blnc(:,2)=balance';
% Substituting the balance vector in the second column of the table
matrix.
disp('
YEAR
ACCOUNT') % Display titles.
disp('
BALANCE ($)') % Display titles.
disp(' ')
% Display an empty line.
disp(table_yr_blnc)
% Display the table.
SOLUTION
Executing the script file in the command window gives:
>> Lecture4Example4
YEAR
ACCOUNT
BALANCE ($)
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
1065.00
1134.22
1207.95
1286.47
1370.09
1459.14
1553.99
1655.00
1762.57
1877.14
ASSIGNMENT 5:
1. Problem 32 page 61 in the textbook.
2. Problem 33 page 61 in the textbook.
3. Problem 4 page 161 in the textbook.
For each problem write a script file and execute it in the command
window.
For each problem, the first two lines of the script file are:
% Assignment 5, Problem (write the number of the problem)
% Name: (first name, last name)
Submit a printout of the script file, and a printout of the command
window.
Download