# Fsolve - Equation solved, inaccuracy possible.

6 views (last 30 days)
Berk Özdemir on 12 May 2020
Answered: Berk Özdemir on 30 May 2020
I'm trying to solve a second order nonlinear differential equation by using central difference. In this case, I obtain a system of nonlinear equations where T denotes time. First I create a vector of symbolic variables, by using these variables I create another vector where I put my nonlinear functions, and then by using
matlabFunction
I convert my nonlinear functions into a single function handle. Finally, I try to solve this system by fsolve using the code;
X=ones(N-1,1);
options=optimset('disp','iter','LargeScale','off','TolFun',.0001,'MaxIter',1000000000,'MaxFunEvals',1000000000);
P=fsolve(f,X,options);
where and f is the function handle I mentioned before. My code is working properly for , and , but when I try to take fsolve runs for a couple of minutes and then stops after giving nowhere near the actual values and this respond:
Equation solved, inaccuracy possible.
The vector of function values is near zero, as measured by the selected value
of the function tolerance. However, the last step was ineffective.
<stopping criteria details>
I tried to take,
X=zeros(N-1,1);
X=rand(N-1,1);
X=randn(N-1,1);
as initial values but none of them solve this issue, only
X=rand(N-1,1);
gave better results among the others. I solved this differential equation by another numerical method and by forward difference. In the case where I use forward difference, I used fsolve again and it worked perfectly fine. I also checked the similar discussions, but none of them helped. I'll be grateful for any help. Thanks in advance.
Regards.

Berk Özdemir on 30 May 2020
After hours of try, I solved this issue. I used another numerical solution as my inital vector and fsolve gave me improved results in terms of accuracy.

Alan Weiss on 12 May 2020
I think that you might be going about this the wrong way. If you want to solve an ODE numerically, usually you should use ode45 or maybe ode15s, not fsolve. Instead of solving the equations by central finite differences, these numerical methods are more suitable for finding numeric solutions, using variiable step sizes and predictor-corrector steps. Give them a try, I think that you will be pleased with the results.
Alan Weiss
MATLAB mathematical toolbox documentation

#### 1 Comment

Berk Özdemir on 12 May 2020
Thank you for your answer, but I've already find the numerical solution of the differential equation. What I want to do is to obtain another numerical solution by using central difference. In other words, what I want to do is to solve this system one way or another if it is possible of course.