How to solve large system of nonlinear equations using fsolve?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Saurabh Madankar
el 28 de Mzo. de 2023
Comentada: Torsten
el 28 de Mzo. de 2023
I have a large system of nonlinear equations in matrix form. Somewhat like
X*A+X.*X==0;
where X is
matrix of unknowns, A is
scalar matrix . I wish to solve it using fsolve, like the simple example shown in documentation of fsolve by using root2d.m function
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1338184/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1338189/image.png)
function F = root2d(x)
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
which is then solved
fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0)
In the main matlab file I defined the starting point for X as
X0= zeros(1,20);
and to avoid writing all the 20 equations I wrote the root2d.m function like this
function F = root2d(X,A)
B=X*A+X.*X;
for i=1:20
F(i) = B(1,i);
end
But then it returns me with error as failure in initial objective function evaluation. So how do I avoid this and is there a way to solve large systems using fsolve OR any other built-in function?
0 comentarios
Respuesta aceptada
Torsten
el 28 de Mzo. de 2023
Editada: Torsten
el 28 de Mzo. de 2023
n = 20;
A = rand(n);
B = @(X)X*A-X.*X;
X0 = rand(1,n);
X = fsolve(B,X0)
% or
n = 20;
A = rand(n);
B = @(X)quadratic(X,A);
X0 = rand(1,n);
X = fsolve(B,X0)
function B = quadratic(X,A)
B = X*A-X.*X;
end
But I don't know if this is simply the numerical version of X = 0, the trivial solution for your equation.
2 comentarios
Saurabh Madankar
el 28 de Mzo. de 2023
Editada: Saurabh Madankar
el 28 de Mzo. de 2023
Torsten
el 28 de Mzo. de 2023
The function "fsolve" calls (root2d) expects the vector of unknowns x as input variable.
If you enlarge the list of input parameters (in your case you added the matrix A), you have to tell "fsolve" that you want to do so:
fun = @(x)root2d(x,A)
Más respuestas (0)
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!