Too Many Output Arguments fseminf

6 ビュー (過去 30 日間)
Carleen McKenna
Carleen McKenna 2023 年 3 月 16 日
コメント済み: Carleen McKenna 2023 年 3 月 17 日
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))
Error using solution>myinfcon
Too many output arguments.

Error in solution>@(p,S)myinfcon(p,S,x0,xn,y0,yn,df) (line 13)
[x, fval] = fseminf(cf, p0, 1, @(p,S) myinfcon(p,S,x0,xn,y0,yn,df))

Error in fseminf (line 463)
[ctmp,ceqtmp,~,~] = feval(userconfcn{3},x,s,varargin{:});

Caused by:
Failure in initial evaluation of semi-infinite constraint function (seminfcon). FSEMINF cannot continue.
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.

回答 (1 件)

Image Analyst
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.
  2 件のコメント
Carleen McKenna
Carleen McKenna 2023 年 3 月 17 日
編集済み: Carleen McKenna 2023 年 3 月 17 日
There is no variable "p" or "S" defined in my script and those variables are not defined until fseminf is run, so I don't understand how this helps. I tried your suggestion anyway and predictably got "Unrecognized function or variable 'p'."
In addition, if I remove the "fValue" line and just use "func" in fseminf, I get the same issue.
Carleen McKenna
Carleen McKenna 2023 年 3 月 17 日
Nevermind. The issue was not with the function call. Apparently I needed to also output "S" in myinfcon. I thought this was optional due to the way it was written in the example I linked in my original question. This problem is solved.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLeast Squares についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by