Define function with nonlinear equation system vercat error

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]

Answers (2)

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))
sol = 1×4
5.1967 7.5954 8.4908 89.9902
fval = 2.7201e-14
opts=optimoptions('fminunc','StepTol',1e-12,'OptimalityTol',1e-12,'FunctionTol',1e-12,'Display','none');
[sol, fval] = fminunc(f,[6 6 6 6],opts)
sol = 1×4
5.1283 7.4954 8.2884 89.7746
fval = 6.2284e-07
opts=optimoptions('fsolve','StepTol',1e-12,'OptimalityTol',1e-12,'FunctionTol',1e-12,'Display','none');
[sol,fval] = fsolve(F,[6 6 6 6],opts)
sol = 1×4
5.1967 7.5954 8.4908 89.9902
fval = 4×1
1.0e+-12 * 0.1137 -0.1705 -0.1137 0

7 Comments

Thank you! Can you elaborate why calling norm() is necessary?
Thank you!
You're welcome, but please Accept-click the answer if your question is settled.
Can you elaborate why calling norm() is necessary?
fminsearch and fminunc are function minimizers. It is not possible to minimize a vector-valued quantity. You can only do a comparison a>b when a and b are scalars.
Alternatively, you can use "fsolve" to solve 4 equations in 4 unknowns:
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]
xsol = fsolve(f,[6 6 6 6])
fminsearch and fminunc are function minimizers. It is not possible to minimize a vector-valued quantity. You can only do a comparison a>b when a and b are scalars.
Thank you for your answer. Is there also a way to define the equations system as a scalar right away? That would avoid the problem
You have four independent goals, and it might not be possible to satisfy all four of them simultaneously.
There is no way to put them all together into one equation without deciding how you want to weight the relative importance of each of them being satisfied.
Is there also a way to define the equations system as a scalar right away? That would avoid the problem
That's what taking the norm of the equations and using a minimizer to solve does.
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] );

Sign in to comment.

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

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]
eqn = 
sol = solve(eqn)
sol = struct with fields:
x1: 103822410932647867988108939361258584970909255712767107632984851/1956912675242977971478733823948770948256077913556186906492928 - (129051806647358847*2106559777848689611763319847455226230701878591815072007499937183516353063743909708404663119^(1/… x2: 426905901714079451496930566148879909559098800764964499621325585/3913825350485955942957467647897541896512155827112373812985856 - (136826147043425299*2106559777848689611763319847455226230701878591815072007499937183516353063743909708404663119^(1/… x3: 265396183855587512112924351704545980843661454136842211566905355/978456337621488985739366911974385474128038956778093453246464 - (88565665080818287*2106559777848689611763319847455226230701878591815072007499937183516353063743909708404663119^(1/2)… x4: 173764389483007670549038691540576895965144260875517/444950427491443854587199399236117403292173074432 - (46067*2106559777848689611763319847455226230701878591815072007499937183516353063743909708404663119^(1/2))/2224752137457219272935996996180587…
format long g
X1 = double(sol.x1)
X1 =
5.19673845646778
X2 = double(sol.x2)
X2 =
7.59542522463441
X3 = double(sol.x3)
X3 =
8.49082794634566
X4 = double(sol.x4)
X4 =
89.9901846810174

Sign in to comment.

Categories

Asked:

on 23 Jan 2022

Commented:

on 24 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!