Too Many Output Arguments fseminf
6 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 16 Mar 2023
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.
2 Comments
See Also
Categories
Find more on Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!