How can I ensure that the initial solution (x0) for fsolve does not result in Inf or NaN values?

8 visualizzazioni (ultimi 30 giorni)
I'm attempting to solve an equation for a system under a small external force. It's sensible to seek the solution near the solution (x0) of the same equation but without the external force term. However, fsolve fails, indicating that the function is undefined at the initial point or displaying the error "Error using trustnleqn, Finite difference Jacobian at the initial point contains Inf or NaN values. fsolve cannot continue."
The code structure is as follows:
for x1 = 1:1000
for x2 = 1:2000
% Calculate the solution of the equation without the external force
% to obtain the initial solution (x0) at each x1 and x2 for fsolve().
% calling fsolve() using x0:
fun = @(x) external_fun(x, a, b);
sol = fsolve(fun, x0);
end
end
I need help figuring out on how to modify my initial solution (x0) before passing it to fsolve to ensure successful execution and avoid failures.

Risposte (2)

Star Strider
Star Strider il 17 Feb 2024
The approach I use (if possible) is to plot the function first. Most of the time, it is possible to plot it in one or two dimensions with respect to specific parameter sets. That will give a general idea of how it behaves. Be careful to look at the expression itself and note where Inf or NaN (0/0, Inf/Inf, etc.) values might occur.
  3 Commenti
Star Strider
Star Strider il 17 Feb 2024
Since you are running it in a loop, evaluate the function with a specific ‘x0’ and then use the isfinite function to check it.
Without knowing what the function is, that is the only way I can think of to check it. If it has some specific characteristics, such as having a periodic function (sin, cos, etc.), that can have repeated roots, if it is not finite at a specific root will mean that it will likely behave the same way at others as well.
Sam Chak
Sam Chak il 18 Feb 2024
Modificato: Sam Chak il 18 Feb 2024
As a general approach, it is advisable to avoid singularities and indeterminates within the search space, as suggested by @Star Strider. I utilized your "original code" to test its ability to find solutions in an undamped pendulum system. The expected solutions for this system are , , , , and .
Edit: Fixing the code.
[x, y] = meshgrid(-2:4/14:2);
u = y;
v = - sin(pi*x);
l = quiver(x, y, u, v);
startx = -2:0.10:2;
starty = -1:0.05:1;
streamline(x, y, u, v, startx, -starty)
xlabel('x_{1}')
ylabel('x_{2}')
title('Pendulum System')
axis tight
%% Input parameters by the User
a = 1;
b = 1;
%% Code by the OP
for x1 = -2:2
for x2 = -2:2
% Calculate the solution of the equation without the external force
% to obtain the initial solution (x0) at each x1 and x2 for fsolve().
% calling fsolve() using x0:
x0 = [x1, x2]; % initial guess (missing code)
fun = @(x) external_fun(x, a, b);
sol = fsolve(fun, x0)
end
end
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.
sol = 1×2
-2 0
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.
sol = 1×2
-2.0000 0
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
-2 0
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.
sol = 1×2
-2.0000 0
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.
sol = 1×2
-2 0
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.
sol = 1×2
-1 0
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.
sol = 1×2
-1.0000 0
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
-1 0
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.
sol = 1×2
-1.0000 0
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.
sol = 1×2
-1 0
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.
sol = 1×2
0 0
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.
sol = 1×2
0 0
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
0 0
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.
sol = 1×2
0 0
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.
sol = 1×2
0 0
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.
sol = 1×2
1 0
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.
sol = 1×2
1.0000 0
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
1 0
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.
sol = 1×2
1.0000 0
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.
sol = 1×2
1 0
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.
sol = 1×2
2 0
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.
sol = 1×2
2.0000 0
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
2 0
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.
sol = 1×2
2.0000 0
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.
sol = 1×2
2 0
%% pendulum system
function dxdt = external_fun(x, a, b)
dxdt(1,1) = a*x(2);
dxdt(2,1) = - b*sin(pi*x(1));
end

Accedi per commentare.


Torsten
Torsten il 17 Feb 2024
Modificato: Torsten il 17 Feb 2024
You know your "external_fun" best. Try to deduce in advance which inputs will lead to failure. Maybe you can use "lsqnonlin" instead of "fsolve" and set bounds on the variables to avoid such problems.
Often it is advisable to set the initial guess to the solution of the last call if the inputs don't change much from call to call.
  4 Commenti
Luqman Saleem
Luqman Saleem il 17 Feb 2024
Since I require solutions close to zero, I'm using the initial guess:
x0 = (rand+1i*rand) - (rand+1i*rand);
About 5% of the iterations have been completed without encountering Inf or NaN errors. It seems that this x0 is working well.
Torsten
Torsten il 17 Feb 2024
Strange choice. But if it works...
Do you know the distribution of X-Y if X and Y are uniformly distributed on [0 1] ?
X = rand(1000000,1);
Y = rand(1000000,1);
Z = X-Y;
histogram(Z,'Normalization','pdf')

Accedi per commentare.

Tag

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by