How can I ensure that the initial solution (x0) for fsolve does not result in Inf or NaN values?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luqman Saleem
el 17 de Feb. de 2024
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.
0 comentarios
Respuesta aceptada
Star Strider
el 17 de Feb. de 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 comentarios
Star Strider
el 17 de Feb. de 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
el 18 de Feb. de 2024
Editada: Sam Chak
el 18 de Feb. de 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
%% pendulum system
function dxdt = external_fun(x, a, b)
dxdt(1,1) = a*x(2);
dxdt(2,1) = - b*sin(pi*x(1));
end
Más respuestas (1)
Torsten
el 17 de Feb. de 2024
Editada: Torsten
el 17 de Feb. de 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 comentarios
Torsten
el 17 de Feb. de 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')
Ver también
Categorías
Más información sobre Gain Scheduling 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!