Matlab 错误 - fplot();

发布于 2024-09-03 10:13:21 字数 312 浏览 2 评论 0原文

>> fplot(fh,[-2 4])
??? Undefined function or variable "e".

Error in ==> myfun at 3
Y(:,2) = e(:).^x;
Error in ==> fplot at 102
x = xmin; y = feval(fun,x,args{4:end});

我尝试使用此 m 文件绘制两个函数。

function Y = myfun(x)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;
>> fplot(fh,[-2 4])
??? Undefined function or variable "e".

Error in ==> myfun at 3
Y(:,2) = e(:).^x;
Error in ==> fplot at 102
x = xmin; y = feval(fun,x,args{4:end});

I tried to plot two function using this m file.

function Y = myfun(x)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2024-09-10 10:13:21

正如 Donnie 在他们的评论中提到的,变量 e 在您的 m 文件中未定义。

如果您在其他地方定义了e,则必须将其传递给myfun,以便函数知道它的值。由于 fplot 不接受具有多个输入值的绘图函数,因此您需要向其传递一个匿名函数。

首先,您需要更改 myfun 的定义以包含 e 作为输入:

function Y = myfun(x,e)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;

然后,您像这样创建函数句柄 fh (fh 仍然只接受一个输入,Matlab 使用e 的值,因为它是在创建函数句柄时在工作区中定义的):

fh = @(x)(myfun(x,e))

最后,您可以像以前一样调用 fplot

fplot(fh,[-2 4])

As Donnie mentioned in their comment, the variable e is undefined in your m-file.

If you have defined e elsewhere, you have to pass it to myfun so that the function knows its value. Since fplot does not accept plotting functions with more than one input value, you need to pass it an anonymous function.

First, you need to change the definition of myfun to include e as input:

function Y = myfun(x,e)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;

Then, you create your function handle fh like this (fh still only takes one input, Matlab uses the value of e as it was defined in the workspace at the time you create the function handle):

fh = @(x)(myfun(x,e))

Finally, you can call fplot like you used to

fplot(fh,[-2 4])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文