Optimization problem with cost function containing definite integral with one endpoint being part of the solution vector
조회 수: 8 (최근 30일)
이전 댓글 표시
I am attempting to fit a curve between two points. My starting point was naturally this code here: https://www.mathworks.com/matlabcentral/answers/159931-given-two-points-fit-a-curve#answer_156452
However, the curve needs to be of a prescribed length (say 12 units). The endpoint y-coordinate is predefined, but the endpoint x-coordinate is part of the solution vector. The only thing I could think of to try to do this was to use the arc length formula
in my cost function, where a = 0 and b is part of the solution vector. This also means that the interval over which fseminf tests its solution (at least, I assume that's what w is) is part of the solution vector.
Unfortunately, when I attempted this, I received the error: "Failure in initial objective function evaluation. FSEMINF cannot continue."
My code is laid out below:
f=@poly;
df=@dpoly;
y0 = 0;
yn = -3;
x0 = 0;
p0 = [1, 12];
costfun = @(p,S) cf(p,S,x0,y0,yn,f);
func = @(p,S) myinfcon(p,S,x0,y0,yn,df);
[x, fval] = fseminf(costfun, p0, 1, func)
function [c,ceq,K1,S] = myinfcon(p,S,x0,y0,yn,df)
c=[]; ceq=[];
if isnan(S)
S=[(p(2)-x0)/10, 0];
end
w=x0:S(1):p(2);
K1=sign(y0-yn)*df(p,w);
end
function out = poly(p, x)
out = p(1)*x.^2;
end
function out = dpoly(p, x)
out = 2*p(1)*x;
end
function c = cf(p, x, x0, y0, yn, f)
syms intx
expr = sqrt(1 + (2*p(1)*intx)^2); %Arc length formula, 2*p(1)*intx is f'(x)
c1 = (int(expr, [x0, p(2)]) - 12)^2;
c2 = (f(p,x0)-y0)^2 + (f(p,p(2))-yn)^2;
c = c1+c2;
end
Is there any way to salvage this, or another way to approach my problem? The issue appears to be occuring in my cost function, so the fact that the stepsize S is variable depending on the current solution vector doesn't appear to be the problem.
댓글 수: 2
John D'Errico
2023년 3월 23일
There are infinitely many curves passing between two points, with some prescribed arc length. Or ZERO curves, if the arc length is too small.
But fitting a curve makes no sense, because you could arbitrarily choose any of the possible candidate curves. Would you be happy with ANY such curve, as long as it satisfies the given arc length?
Or would you want some how the entire family of curves that pass through those two points, in some way parameterized? This would be a much more difficult problem of course to characterize a complete set of curves with a given arc length. But you could arguably define the curves as a set of parametrix cubics, say x(t) and y(t), that pass through the given points. But even with a fairly simple form like that, there will be infinitely many such cubics with a given arc length, and no simple way to describe the entire set for any specific arc length.
Do you have some sort of goal for the overall shape of the curve?
채택된 답변
Matt J
2023년 3월 23일
You've written your functions in terms of 2 arguments (p,S), whereas fseminf expects all the unknowns to be part of one argument, e.g.,
costfun = @(pS) cf(pS(1),pS(2),x0,y0,yn,f);
댓글 수: 3
Torsten
2023년 3월 23일
편집: Torsten
2023년 3월 23일
syms intx
expr = sqrt(1 + (2*p(1)*intx)^2); %Arc length formula, 2*p(1)*intx is f'(x)
You have many problems here. "intx" is a symbolic scalar. So "expr" is a symbolic scalar, thus in principle a number like e.g. 4. I wonder what the intention then is with the following command:
int(expr, [x0, p(2)])
You want to integrate a constant from x0 to p(2) ? Simple: The result is expr*(p(2)-x0). But is this really what you intend ?
Furthermore, fseminf does not work with symbolic variables, only numerical. So whatever you try here, you will have to use "integral" instead of "int".
Could you perhaps explain in your own words what the intention of your code is ?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!