I have written this code. But it shows the error
Function definitions are not permitted at the prompt or in scripts.
Can anyone help me out with it? what am i doing wrong?
function [npv, pvAtPeriod]= npvFlow([100 -5 100],0.04, [1 2 4])
if isscalar(periods)
t = 1:4;
else
t = periods;
end
if isscalar(payments)
c = zeros(1,length(t));
c = c+payments;
else
c = payments;
end
if numel(c)~=numel(t)
disp('Error: Payment or period missing.')
return
end
r=rate;
pvPeriod = c./(1+r).^t;
npv=sum(pvPeriod);
pvAtPeriod = [t', pvPeriod'];
end