필터 지우기
필터 지우기

How can I display the result in table for an user defined functions?

조회 수: 3 (최근 30일)
NIANNIAN
NIANNIAN 2014년 11월 2일
댓글: NIANNIAN 2014년 11월 3일
An amount of money P is invested in an account where interest is compounded at the end of the period. The future worth F yielded at an interest rate i after n periods may be determined from the following formula: F = P(1 + i)n
Write a script to calculate the future worth of an investment for each year from 1 through n. The input from the user should include the initial investment P, the interest rate i (as a decimal), and the number of years n for which the future worth is to be calculated. The output should consist of a table with headings and columns for n and F. Use the fprintf command to achieve a clean and readable format for the table. The following is the expected output from the program for P = $100, 000, i = 0.05 (that is, a 5% interest rate), and n = 10 years.
Enter initial investment in dollars: 100000
Enter the interest rate: 0.05
Enter number of years: 10
year future worth
0 100000.00
1 105000.00
2 110250.00
3 115762.50
4 121550.63
5 127628.16
6 134009.56
7 140710.04
8 147745.54
9 155132.82
10 162889.46
  댓글 수: 3
NIANNIAN
NIANNIAN 2014년 11월 3일
function furtureworth P=input('Enter initial incestment (dollars):'); i=input('Enter the interest rate (decimal):'); n=input('Enter the number of years:');
x = 0:n;
if (i>=0) F=P*(1+i).^x; y = [x;F]; end
if(i<0)||(P<0) fprintf('sorry,the value of i is not valid for calculation \n'); end
fprintf('\n year future worth\n'); fprintf('%5d %14.2f\n',y)

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 11월 2일
Hint: for "The input from the user" use the input() function. Then use a for loop , and put the future worth in there and also the fprintf
for y = 1 : numberOfYears
futureWorth = .......some equation............
fprintf('%d %.2f\n',......................
end
I hope that's not giving too much away.

추가 답변 (1개)

Rick Rosson
Rick Rosson 2014년 11월 2일
doc fprintf
doc for

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by