System of nonlinear equations
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mahboubeh Shahrbaf
el 27 de Ag. de 2019
Editada: Walter Roberson
el 24 de Dic. de 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.
0 comentarios
Respuesta aceptada
Star Strider
el 27 de Ag. de 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 comentarios
Más respuestas (2)
Mahboubeh Shahrbaf
el 28 de Ag. de 2019
1 comentario
Star Strider
el 28 de Ag. de 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
el 24 de Dic. de 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
0 comentarios
Ver también
Categorías
Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!