How can I ensure that the initial solution (x0) for fsolve does not result in Inf or NaN values?
Show older comments
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.
Accepted Answer
More Answers (1)
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 Comments
Luqman Saleem
on 17 Feb 2024
Luqman Saleem
on 17 Feb 2024
Luqman Saleem
on 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')
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
