Matlab 未定义函数';方程';对于类型为';双倍';

Matlab 未定义函数';方程';对于类型为';双倍';,matlab,function,octave,newtons-method,Matlab,Function,Octave,Newtons Method,写一个函数方程(M,epsilon,tol),设置x=M+epsilon*sin(x)的解 功能: 然后我得到类型为“double”的输入参数的未定义函数“等式”。,但我不知道为什么。谁能给我解释一下吗 编辑: 根据方的回答,我有: function y=equation(M,epsilon,tol) y.newtonp=@computeNewtonp y.sin=@computeSin end function x=computeNewtonp(f,x,tol,h) if nargin&

写一个函数方程(M,epsilon,tol),设置x=M+epsilon*sin(x)的解

功能:

然后我得到类型为“double”的输入参数的未定义函数“等式”。,但我不知道为什么。谁能给我解释一下吗

编辑: 根据方的回答,我有:

function y=equation(M,epsilon,tol)
y.newtonp=@computeNewtonp
y.sin=@computeSin
end
 
function x=computeNewtonp(f,x,tol,h)
if nargin<4
h=1e-8
end
if nargin<3
tol=1e-8
end
fx=f(x)
while abs(fx)>tol
g=(f(x+h)-fx)/h
x=x-fx/g
abs(fx)
end
end
 
function z=computeSin(x,epsilon,M)
x=computeNewtonp(f,x,tol,h)
z=epsilon*sin(x)-x+M
end
函数y=方程(M,ε,tol) y、 newtonp=@computeNewtonp y、 sin=@computeSin 结束 函数x=computeNewtonp(f,x,tol,h)
如果在函数单元内编写函数,则称为局部函数

此函数仅对第一个函数可见,但可以通过让主函数返回句柄使其可见,如本教程所示


我认为当您想在
newtonp
中使用
方程时,可能需要将其定义为
x
的函数:

function y=equation(x,M,epsilon,tol)
   y=M+epsilon*sin(x)-x;
end

newtonp(@(x) equation(x,0.5,0.5,1e-5),2,1e-5,1e-8) % use anonymous function
你会得到

ans =  0.88786

你能给我解释一下在我的任务中会是什么样子吗?我是这样尝试的:函数y=方程y.newtonp=@computenewttonp y.sin=@computeSin end函数x=computeNewtonp(f,x,tol,h),如果你的函数足够简单,可以作为内联函数编写,比如
myfun=@(x,M,epsilon)y=M+epsilon*sin(x)-x然后可以将其作为函数句柄传递给函数`不,因为在这个任务中,我必须使用像newtonp这样的函数来计算x,然后我得到方程(M,epsilon,tol),其中tol是一个近似误差。所以我不能跳过函数newtonp,写出方程(x,M,epsilon)
function y=equation(x,M,epsilon,tol)
   y=M+epsilon*sin(x)-x;
end

newtonp(@(x) equation(x,0.5,0.5,1e-5),2,1e-5,1e-8) % use anonymous function
ans =  0.88786