How to solve large system of nonlinear equations using fsolve?

23 Ansichten (letzte 30 Tage)
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
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?

Akzeptierte Antwort

Torsten
Torsten am 28 Mär. 2023
Bearbeitet: Torsten am 28 Mär. 2023
n = 20;
A = rand(n);
B = @(X)X*A-X.*X;
X0 = rand(1,n);
X = fsolve(B,X0)
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 2.000000e+03.
X = 1×20
0.6342 -0.1889 0.7591 0.4164 0.3940 0.1531 -0.2403 -0.0386 0.4819 -0.4830 -0.1495 0.5164 -0.6420 -0.1311 -0.3642 0.1766 -0.3641 -0.4993 -0.5372 0.1793
% or
n = 20;
A = rand(n);
B = @(X)quadratic(X,A);
X0 = rand(1,n);
X = fsolve(B,X0)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
X = 1×20
1.0e-08 * -0.3578 0.6841 -0.5536 0.6421 -0.7101 -0.8738 -0.3448 -0.2205 0.6826 0.3408 0.2041 -0.2995 0.3190 0.6293 0.9361 -0.6555 -0.5841 0.1874 0.5407 -0.3666
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 Kommentare
Saurabh Madankar
Saurabh Madankar am 28 Mär. 2023
Bearbeitet: Saurabh Madankar am 28 Mär. 2023
The given equation is not exactly the one I am trying to solve, in fact I have more than one of such matrix equations. The way you defined B like this
B = @(X)X*A-X.*X;
worked for me, but as I had multiple such equations I just put them in the square bracket separated by space or semicolon, either of which works. Thanks a lot for the answer, as I was working on it whole day and finally at the end of the day got the correct answer. Any idea why my earlier attempt didn't work? Is it because I defined B like the way I have defined?
Torsten
Torsten am 28 Mär. 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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by