# System of nonlinear equations

2 views (last 30 days)
Mahboubeh Shahrbaf on 27 Aug 2019
Edited: Walter Roberson on 24 Dec 2019
Dear all, I am trying to solve a system of 4 nonlinear equations. I act as below:
Creat a file as : equationssystem.m
The content of file is:
function F = equationssystem(x)
F = [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3) ...
-0.0004677*x(2)^2+0.6711*x(2)-158.14;...
2*x(1)*x(2)-2292.8*x(1)+x(3)-0.0009124*x(2)+0.6382;...
x(1)*x(4)^2+1314232.96*x(1)-2292.8*x(1)*x(4)+x(3)*x(4)-1146.4*x(3) ...
-0.001071*x(4)^2+1.939*x(4)-819.55;...
2*x(1)*x(4)-2292.8*x(1)+x(3)-0.002141*x(4)+1.941];
end
Then in command window, I write:
x0 = [0; 1200; -1; 1100];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@equationssystem,x0,options)
The initial guesses are close to the real results. But I am facing these errors:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in equationssystem (line 8)
F = [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3) ...
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
I would really appreciate that if somebody help me to fix these errors and solve my equations since I have a huge sets of coefficients for which I have to solve these equations and I am a beginner in matlab.

Star Strider on 27 Aug 2019
The spaces and continuation ellipses in ‘equationssystem’ are the problem.
I slightly edited the function and defined it as an anonymous function to get:
equationssystem = @(x) [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3)-0.0004677*x(2)^2+0.6711*x(2)-158.14;
2*x(1)*x(2)-2292.8*x(1)+x(3)-0.0009124*x(2)+0.6382;
x(1)*x(4)^2+1314232.96*x(1)-2292.8*x(1)*x(4)+x(3)*x(4)-1146.4*x(3)-0.001071*x(4)^2+1.939*x(4)-819.55;
2*x(1)*x(4)-2292.8*x(1)+x(3)-0.002141*x(4)+1.941];
that then ran correctly:
x0 = [0; 1200; -1; 1100];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(equationssystem,x0,options)
to produce:
x =
-7.994193158578257e-02
1.139800767940268e+03
-6.533564950069347e-01
1.139198642923269e+03
fval =
-4.001776687800884e-11
-1.043609643147647e-14
-3.183231456205249e-11
-3.042011087472929e-14
##### 2 CommentsShowHide 1 older comment
Star Strider on 27 Aug 2019
As always, my pleasure!

### More Answers (2)

Mahboubeh Shahrbaf on 28 Aug 2019
Dear Star Strider,
Again, I encountered with problems when solving my equations.
I told you, I have a huge sets of parameters for solving these equations. For most of set, the solver can not solve the function by different gussed initial values. It is so sensitive to these values, for example this one:
equationssystem = @(x) [x(1)*x(2)^2+1298460.25*x(1)-2279.*x(1)*x(2)+x(3)*x(2)-1139.5*x(3)-0.0004677*x(2)^2+0.6711*x(2)-161.435;
2*x(1)*x(2)-2279.*x(1)+x(3)-0.0009124*x(2)+0.6382;
x(1)*x(4)^2+1298460.25*x(1)-2279.*x(1)*x(4)+x(3)*x(4)-1139.5*x(3)-0.001071*x(4)^2+1.939*x(4)-822.84;
2*x(1)*x(4)-2279.*x(1)+x(3)-0.002141*x(4)+1.941];
x0 = [-1; 1300; -2; 1000];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(equationssystem,x0,options)
I've changed my guess several times but there is no answer. Would you please help me how I can choose the best guess for solving different equations?
##### 1 CommentShowHide None
Star Strider on 28 Aug 2019
As a general rule, plotting the function first is the best way to understand what it is doing. With four parameters however, that is not an option.
The Global Optimization Toolbox has several functions you can use that will search for the best parameter set. See for example MultiStart. There are several related functions linked to in and at the end of that page that are also applicable.
If you need a single scalar output for any of the global optimization functions, using norm(fval) or the equivalent would likely be the most applicable.

Alex Sha on 24 Dec 2019
Hi，Mahboubeh， the results of your second case are much different with that of your first one, so in second case you use approximate initial values of first case will absolutely lead to wrong outcome. A close results for second case are:
x1: -1282.80143132536
x2: 1139.55588272278
x3: 143.77440420505
x4: 1139.55584481414

### Community Treasure Hunt

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

Start Hunting!