Too Many Output Arguments fseminf
1 次查看(过去 30 天)
显示 更早的评论
I am using a solution to a problem involving fitting a curve to only two points, found here: https://www.mathworks.com/matlabcentral/answers/159931-given-two-points-fit-a-curve#answer_156452
However, using this solution with a few minor changes results in
Error using find_sweep_polynomials>myinfcon
Too many output arguments.
My code is as such:
f=@cubic; %functions of (p,x)
df=@dcubic;
x0 = 0;
xn = 12;
y0= 0;
yn = 1;
p0 = [1, 1, 1];
cf = @(p,x) (f(p,x0)-y0)^2 + (f(p,xn)-yn)^2;
[x, fval] = fseminf(cf, p0, 1, @(p,S) myinfcon(p,S,x0,xn,y0,yn,df))
function [c,ceq,K1] = myinfcon(p,S,x0,xn,y0,yn,df)
c=[]; ceq=[];
if isnan(S)
S=[(xn-x0)/10, 0];
end
w=x0:S(1):xn;
K1=sign(y0-yn)*df(p,w);
end
function out = cubic(p, x)
out = p(1)*x.^3 + p(2)*x.^2 + p(3)*x;
end
function out = dcubic(p, x)
out = 3*p(1)*x.^2 + 2*p(2)*x + p(3);
end
I checked the documentation and there should be nothing wrong with this syntax, especially since I took it from an expert on these forums. I don't believe I am redefining any existing matlab functions, and there is no issue with there being files elsewhere because this is all self-contained in one script.
0 个评论
回答(1 个)
Image Analyst
2023-3-16
Maybe try not defining your anonymous function inside the argument list of the function you're trying to call. So instead of
[x, fval] = fseminf(cf, p0, 1, @(p,S) myinfcon(p,S,x0,xn,y0,yn,df))
try
func = @(p,S) myinfcon(p,S,x0,xn,y0,yn,df)) % Define an anonymous function.
fValue = func(p,S,x0,xn,y0,yn,df) % Get the value for a set of inputs.
[x, fval] = fseminf(cf, p0, 1, fValue) % Pass that value into the fseminf() function.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!