首页 > 解决方案 > 'double' 类型的输入参数的未定义函数 'equation'

问题描述

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

功能:

function x=newtonp(f, x, tol, h)
 
if nargin<4
   h=1e-8
end
 
if nargin<3
   tol=1e-8
end
 
while abs(f(x))>tol
   g=(f(x+h)-f(x))/h
   x=x-f(x)/g
end
end
 
function y=equation(M,epsilon,tol)
   y=M+epsilon*sin(x)-x
end

调用函数的代码:

例如:

newtonp(@equation(0.5,0.5,1e-5),2,1e-5,1e-8)

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

编辑:

使用FangQ的回答我有:

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 必须是双精度数据类型。它目前是结构类型。检查变量的赋值位置

标签: matlabfunctionoctavenewtons-method

解决方案


if you write a function inside a function unit, it is called a local function

https://www.mathworks.com/help/matlab/ref/localfunctions.html

this function is only visible to the first function, but you can make it visible by letting the main function return a handle, like in this tutorial

https://www.mathworks.com/help/matlab/matlab_prog/call-local-functions-using-function-handles.html


推荐阅读