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

7 views (last 30 days)

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.

##### 0 Comments

### Answers (2)

Star Strider
on 17 Feb 2024

##### 3 Comments

Star Strider
on 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
on 18 Feb 2024

Edited: Sam Chak
on 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

%% pendulum system

function dxdt = external_fun(x, a, b)

dxdt(1,1) = a*x(2);

dxdt(2,1) = - b*sin(pi*x(1));

end

Torsten
on 17 Feb 2024

Edited: Torsten
on 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 Comments

Torsten
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')

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!