【发布时间】:2018-01-31 18:55:50
【问题描述】:
我尝试使用 fzero 在 -6pi 和 6pi 之间找到函数 f=(x^3)*cos(x) 的所有根。
我创建一个函数:
function y3=f(x)
f=(x^3)*cos(x);
end
% Then at the command window:
syms x;
fun=@f
x1=-6*pi;
x2=-5*pi;
r=zeros(1,12);
for i=1:12
x=fzero(@fun,[x1 x2]);
r(i)=x;
x1=x1+pi;
x2=x2+pi;
end
我收到了这个错误:
Error: "fun" was previously used as a variable, conflicting with its use here as the name of a function
or command. See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
我该如何解决?谢谢
【问题讨论】:
-
你的函数体应该是
y3=(x^3)*cos(x);而不是f=(x^3)*cos(x); -
你写
fun=@f,后来写@fun。在第二条语句中,您将fun视为一个函数,但它在您的第一条语句中声明为变量。解决办法是写fzero(fun,...)或fzero(@f,...)。
标签: matlab function for-loop function-parameter