Define function with nonlinear equation system vercat error
11 ビュー (過去 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!