问题描述
我已经编写了这段代码.但这显示了错误
I have written this code. But it shows the error
有人可以帮我吗?我究竟做错了什么?
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
推荐答案
正如错误中指出的那样,脚本中不允许使用函数.我们看不到此脚本的启动方式,但我敢打赌它不具有 function somefunction()
...请参阅本文档.
As it states in the error, functions aren't allowed in scripts. We can't see how this script starts, but I'd wager it isn't with function somefunction()
... See this documentation.
尤其是:
您必须将函数 npvFlow
保存在单独的文件中(在Matlab路径上,称为 npvFlow.m
),或者将整个脚本放到里面函数或以开头的函数.请注意,如果将脚本更改为函数,则运行后将不会在工作区中留下任何东西.
You must either save your function npvFlow
in a separate file (on the Matlab path, called npvFlow.m
) or put the whole script inside a function or starting with a function. Note that if you change your script into a function then you won't be left with anything in your workspace after running.
您用于传递参数的语法也是无效的(请参阅Steve的注释).如果您只想固定这些参数,则不需要 npvFlow
作为函数,也可以通过使整个事物成为有效的 script 来解决此问题.>没有任何功能!
Your syntax for passing arguments is invalid too (see Steve's comment). If you only ever want those arguments to be fixed, then there is no need for npvFlow
to be a function and you could also resolve this issue by making the entire thing a valid script with no functions!
这篇关于Matlab错误:提示或脚本中不允许使用函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!