Creating a function using a vector as a variable

16 visualizzazioni (ultimi 30 giorni)
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 Commento
Star Strider
Star Strider il 25 Apr 2014
Does it produce the result you expect?
I suggest that instead of:
exp(1).^x
write:
exp(x)
Same result, and much more efficient.

Accedi per commentare.

Risposta accettata

dpb
dpb il 25 Apr 2014
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...

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by