matlab - '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 必须是双精度数据类型。它目前是结构类型。检查变量的赋值位置
解决方案
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
推荐阅读
- glove - ValueError:需要超过 0 个值才能解包 - 手套
- amazon-web-services - 是否可以使用 OpenId Federation 提供 AWS 控制台访问?
- node.js - Nodejs在javascript中引入了异步性
- go - 如何在go中调用另一个函数?
- javascript - 传递一个 Socket.IO 实例来路由文件
- c# - 如何将我的外部 C# 代码的参数传递给 Unity3D exe 文件?
- php - setWrapText(false) 似乎不适用于 mPDF 和 phpExcel
- python - Python:如何在不覆盖的情况下编写 jsonline?
- python - “Qt:检测到未经测试的 Windows 10.0 版本!” 使用 python-vlc 时
- c# - 使用 Azure AD 进行指纹身份验证