Creating a function using a vector as a variable

3 visualizaciones (últimos 30 días)
Cameron
Cameron el 24 de Abr. de 2014
Respondida: dpb el 25 de Abr. de 2014
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 comentario
Star Strider
Star Strider el 25 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 25 de Abr. de 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...

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by