Creating a function using a vector as a variable

4 ビュー (過去 30 日間)
Cameron
Cameron 2014 年 4 月 24 日
回答済み: dpb 2014 年 4 月 25 日
I'm trying to create a financial calculator, and have it run a calculation N number of times and display the result for each time as a new line in a new array created. I don't know where to start, and I have included the code of what I have so far. The output is only a 100X1 array with the "year" variable.
Thanks, Cameron
function [time,isimple,iperiodic,icontinuous] = interest(P,r,n,t)
time=[1:t]';
isimple=P*(1+(time*r));
iperiodic=P*((1+(r/n)).^(n*time));
icontinuous=P*((exp(1)).^(r*time));
%psimple=p+(isimple.*t);
% Detailed explanation goes here
%isimple,iperiodic,icontinuous
%psimple,pperiodic,pcontinuous
end
  1 件のコメント
Star Strider
Star Strider 2014 年 4 月 25 日
Does it produce the result you expect?
I suggest that instead of:
exp(1).^x
write:
exp(x)
Same result, and much more efficient.

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

採用された回答

dpb
dpb 2014 年 4 月 25 日
The output is only a 100X1 array with the "year" variable
You're not providing return locations for the other outputs when you call, it then...
function [time,isimple,iperiodic,icontinuous] = interest(P,r,n,t)
Use
[t,int_s,int_p,int_c]=interest(P,r,n,t);
for chosen values of the inputs and voila! you'll get all the results back.
Matlab by default only returns the first variable of multiple when don't provide any place for the function to put them in either the workplace or the calling function/script. That default location in the workplace is the special variable ans but it only gets the first and the rest go to the bit bucket in the sky...

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by