Define function with nonlinear equation system vercat error
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I am trying to define a nonlinear equation system in a function in order to solve it using fsolve.
Already calling the function it self raises the error
"Error using vertcat
Dimensions of arrays being concatenated are not consistent."
running fminunc results in
Error in fminunc (line 307)
f = feval(funfcn{3},x,varargin{:});
Error in GPS_Calculation (line 49)
sol = fminunc(f,[6 6 6 6])
Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue.
How can I fix this?
f = @(x)[sqrt( (101 - x(1)).^2 + (16 - x(2)).^2 + (207 - x(3)).^2 ) + x(4) - 310.5685;
sqrt( (52 - x(1)).^2 + (21 - x(2)).^2 + (302 - x(3)).^2 ) + x(4) - 387.5097;
sqrt( (17 - x(1)).^2 + (53 - x(2)).^2 + (350 - x(3)).^2 ) + x(4) -434.7066;
sqrt( (-15 - x(1)).^2 + (159 - x(2)).^2 + (208 - x(3)).^2 ) + x(4) - 341.25730]
f([6 6 6 6])
sol = fminsearch(f,[6 6 6 6])
sol = fminunc(f,[6 6 6 6])
sol = fsolve(F,[6 6 6 6]
댓글 수: 0
답변 (2개)
Matt J
2022년 1월 23일
편집: Matt J
2022년 1월 23일
F = @(x)[sqrt( (101 - x(1)).^2 + (16 - x(2)).^2 + (207 - x(3)).^2 )+ x(4)- 310.5685;
sqrt( (52 - x(1)).^2 + (21 - x(2)).^2 + (302 - x(3)).^2 )+x(4)-387.5097;
sqrt( (17 - x(1)).^2 + (53 - x(2)).^2 + (350 - x(3)).^2 )+x(4)-434.7066;
sqrt( (-15 - x(1)).^2 + (159 - x(2)).^2 + (208 - x(3)).^2 )+x(4)-341.25730];
f=@(x) norm(F(x))^2;
[sol,fval] = fminsearch(f,[6 6 6 6],optimset('TolFun',1e-12','MaxIter',1e5,'MaxFunEvals',inf))
opts=optimoptions('fminunc','StepTol',1e-12,'OptimalityTol',1e-12,'FunctionTol',1e-12,'Display','none');
[sol, fval] = fminunc(f,[6 6 6 6],opts)
opts=optimoptions('fsolve','StepTol',1e-12,'OptimalityTol',1e-12,'FunctionTol',1e-12,'Display','none');
[sol,fval] = fsolve(F,[6 6 6 6],opts)
댓글 수: 7
Matt J
2022년 1월 24일
You caould have done
f = @(x)norm( [sqrt( (101 - x(1)).^2 + (16 - x(2)).^2 + (207 - x(3)).^2 )+ x(4)- 310.5685;
sqrt( (52 - x(1)).^2 + (21 - x(2)).^2 + (302 - x(3)).^2 )+x(4)-387.5097;
sqrt( (17 - x(1)).^2 + (53 - x(2)).^2 + (350 - x(3)).^2 )+x(4)-434.7066;
sqrt( (-15 - x(1)).^2 + (159 - x(2)).^2 + (208 - x(3)).^2 )+x(4)-341.25730] );
Walter Roberson
2022년 1월 24일
You have a multi objective search, trying to simultaneously minimize four different objectives. fmincon is only able to minimize a single objective. You need to switch to a Pareto search using gamultiobj() or paretosearch()
Remember that Pareto searches are not global minima searches: they correspond to finding local minima such that moving the point in any direction makes at least one of the objectives worse.
댓글 수: 1
Walter Roberson
2022년 1월 24일
There happens to be a unique solution. But notice that I did not use fmincon()
syms x [1 4]
eqn = [sqrt( (101 - x(1)).^2 + (16 - x(2)).^2 + (207 - x(3)).^2 ) + x(4) - 310.5685;
sqrt( (52 - x(1)).^2 + (21 - x(2)).^2 + (302 - x(3)).^2 ) + x(4) - 387.5097;
sqrt( (17 - x(1)).^2 + (53 - x(2)).^2 + (350 - x(3)).^2 ) + x(4) - 434.7066;
sqrt( (-15 - x(1)).^2 + (159 - x(2)).^2 + (208 - x(3)).^2 ) + x(4) - 341.25730]
sol = solve(eqn)
format long g
X1 = double(sol.x1)
X2 = double(sol.x2)
X3 = double(sol.x3)
X4 = double(sol.x4)
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!