首页 > 解决方案 > Matlab fplot:没有足够的输入参数

问题描述

我刚刚开始学习 Matlab,并且一直在寻找解决方案。

基本上,我只需要绘制一个函数,然后为以后的问题对其进行更多操作。

fplot(@(x) myfunc);

function y = myfunc(x)
    y = (x^3) - (4 .* x^2) - 1
end

产生此错误

Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape as
the input arguments. 
> In matlab.graphics.function.FunctionLine>getFunction
  In matlab.graphics.function.FunctionLine/updateFunction
  In matlab.graphics.function.FunctionLine/set.Function_I
  In matlab.graphics.function.FunctionLine/set.Function
  In matlab.graphics.function.FunctionLine
  In fplot>singleFplot (line 234)
  In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 193)
  In fplot>vectorizeFplot (line 193)
  In fplot (line 163)
  In HWA1_2 (line 1) 
Warning: Error updating FunctionLine.

 The following error was reported evaluating the function in FunctionLine update: Not
 enough input arguments.

当我单独使用 fplot 时它可以工作。

fplot((x^3)-(4*x^2)-1)

如果有人能指出我做错了什么,我将不胜感激。我需要将它定义为函数的原因是因为我需要稍后对其进行更多操作。

标签: matlab

解决方案


您调用 fplot 的语法是问题所在,而不是您的函数。如果您要传递一个简单的函数句柄,只需使用:

fplot(@myfunc)

您使用的语法是创建匿名函数的方式,但您忘记包含x在等式中。你也可以这样写,得到同样的结果:

fplot(@(x) myfunc(x))

推荐阅读