# How to improve accuracy in fsolve?

39 views (last 30 days)
dimocritos on 3 Apr 2020
Commented: dimocritos on 5 Apr 2020
Hello to everyone,
I am unfamiliar with MATLAB software and I am trying to solve some nonlinear equations. The problem that I have is that the results are not accurate enough. I don't have any initial guess, but I know that the results must be between 0 and pi/2 rads. Any idees?
f=@(a) [(4/pi)*(1-2*cos(a(1))+2*cos(a(2))-2*cos(a(3))+2*cos(a(4)))-0.5;
(4/(5*pi))*(1-2*cos(5*a(1))+2*cos(5*a(2))-2*cos(5*a(3))+2*cos(5*a(4)));
(4/(7*pi))*(1-2*cos(7*a(1))+2*cos(7*a(2))-2*cos(7*a(3))+2*cos(7*a(4)));
(4/(17*pi))*(1-2*cos(17*a(1))+2*cos(17*a(2))-2*cos(17*a(3))+2*cos(17*a(4)))];
[r]=fsolve(f,[1 1 1 1])
(4/pi)*(1-2*cos(r(1))+2*cos(r(2))-2*cos(r(3))+2*cos(r(4)))-0.5
(4/(5*pi))*(1-2*cos(5*r(1))+2*cos(5*r(2))-2*cos(5*r(3))+2*cos(5*r(4)))
(4/(7*pi))*(1-2*cos(7*r(1))+2*cos(7*r(2))-2*cos(7*r(3))+2*cos(7*r(4)))
(4/(17*pi))*(1-2*cos(17*r(1))+2*cos(17*r(2))-2*cos(17*r(3))+2*cos(17*r(4)))

#### 1 Comment

Walter Roberson on 4 Apr 2020
The equations in f cannot distinguish between a(1) and a(3), or between a(2) and a(4) .

Walter Roberson on 3 Apr 2020
fsolve cannot limit the range of values.
fzero can limit the range of variables but only for a single function in one variable.
vpasolve can limit the range of values.
You can often get useful results by minimizing sum of f squared over a range as fmincon can handle range constraints

dimocritos on 4 Apr 2020
I would like to thank you for your time and help. I would prefer to work with the functions that are already build in the matlab software.
Walter Roberson on 4 Apr 2020
To get this list of 20 approximate solutions, I used some software that I wrote in MATLAB. I formed the sum of squares of the original equations, and then run minimizers on the result, starting from many many many different locations.
You could do the same sort of thing: if you were to construct a 4D grid from 0:0.1:pi/2 and use the 50000 or so resulting 4D points as starting points for fmincon() and look for the places that get you a function value close to 0. Yes, that does take a bunch of computation, but it gets the job done.
dimocritos on 5 Apr 2020
Thank you for your help and time. I managed to find a method.

Alex Sha on 4 Apr 2020
There are multi-solutions with high enough accurancy, even given the range limition in [0, pi/2]:
1:
a1: 0.996298496311242
a2: 0.956610794085768
a3: 1.12078428609661
a4: 1.47219048531321
feval:
-1.58206781009085E-14
1.27222187258541E-15
-4.64461953483561E-15
-1.99564215307515E-16
2:
a1: 0.423201820072776
a2: 1.45258039750964
a3: 1.15702893242895
a4: 0.468494500989183
feval:
2.22044604925031E-15
-4.63654193564459E-15
7.59294323955735E-15
7.45663375320891E-15
3:
a1: 1.15702893242895
a2: 0.468494500989183
a3: 0.423201820072776
a4: 1.45258039750964
feval:
2.22044604925031E-15
-4.69308512998172E-15
7.59294323955735E-15
7.46702772275618E-15

Show 1 older comment
dimocritos on 4 Apr 2020
I would like to thank you for your time and help. Which function have you used for the results above?
Walter Roberson on 4 Apr 2020
Alex used a commercial package to find the roots. I do not know what algorithm it is using, but it does find good solutions quickly.
For this list of A1, A2, A3, A4 values, I used the approach I described above in https://www.mathworks.com/matlabcentral/answers/515215-how-to-improve-accuracy-in-fsolve#comment_820992 to find EQ4 in two variables, and then I found a numeric solution for A3 and A4, and then back substituted to find A1 and A2 and then cross-checked. I did the work in Maplesoft's Maple software, but I think you might be able to do the same thing in MATLAB (not sure; MATLAB gives up on roots of symbolic nonlinear equations relatively easily.)
dimocritos on 5 Apr 2020
Thank you both for your help and time. I managed to find a method.

R2019b

### Community Treasure Hunt

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

Start Hunting!