Define function with nonlinear equation system vercat error
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Matt J
on 23 Jan 2022
Edited: Matt J
on 23 Jan 2022
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 Comments
Matt J
on 24 Jan 2022
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
on 24 Jan 2022
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 Comment
Walter Roberson
on 24 Jan 2022
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)
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!