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!