System of nonlinear equations

조회 수: 3 (최근 30일)
Mahboubeh Shahrbaf
Mahboubeh Shahrbaf 2019년 8월 27일
편집: Walter Roberson 2019년 12월 24일
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
Star Strider 2019년 8월 27일
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
Mahboubeh Shahrbaf
Mahboubeh Shahrbaf 2019년 8월 27일
Thank you very much.
It works well.
Star Strider
Star Strider 2019년 8월 27일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Mahboubeh Shahrbaf
Mahboubeh Shahrbaf 2019년 8월 28일
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
Star Strider
Star Strider 2019년 8월 28일
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
Alex Sha 2019년 12월 24일
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

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by