Matlab returns wrong result

2 ビュー (過去 30 日間)
enlightenight
enlightenight 2016 年 10 月 11 日
コメント済み: enlightenight 2016 年 10 月 12 日
I am trying to calculate an annual return based on the given formula for a homework. I typed this code:
function A = untitled9(R, m, p)
k = ((1+p)^(m-1))*p
h = ((1+p)^m)-1
g = k/h
A = g.*R
s=sprintf('The annual installment for Katja is %.2f', A);
disp(s)
for the values R=75000, m=10 and p=0.04; the result should be 8887.5. but matlab insists on returning 8891.17, even though it calculates k, h and g right. (i purposefully coded it that way so I could see if I did something wrong in the initial formula.)
can someone please help me?
edit: it returns the same answer when i manually enter 75000 as the number to multiply g value by.

採用された回答

Preethi
Preethi 2016 年 10 月 12 日
編集済み: Preethi 2016 年 10 月 12 日
Hi,
the value displayed by Matlab is precise value since it uses g value as 0.118549. if you want 8887.5 then truncate the value of g to 0.1185. (I used a crude method)
g1 = str2num(sprintf('%.4f',g))
A = (g1.*R)
when you are displaying in command window it shows 4 characters after decimal(I think its standard method), hence the confusion.
  1 件のコメント
enlightenight
enlightenight 2016 年 10 月 12 日
oh my god thanks a lot! i spent an embarrassing amount of time on it, trying to figure out where i did a mistake. seriously, you saved my mind before my exam!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by