Fsolve don't work good with trigonometric

13 views (last 30 days)
Federico MegaMan
Federico MegaMan on 22 Jan 2020
Edited: Alex Sha on 6 Sep 2023
Hello,
I have to resolve a system with 8 equations and 8 unknows and I'm using Fsolve to do it. The problem is that the results isn't the awaited (I have the numerical solution).
Can it maybe depend on the trigonometric function?
What's my wrong?
Thank you in advance.
sol =fsolve(@(x)funzmia2(x), 1:8);
XA1=sol(1);
YA1=sol(2);
XB1=sol(3);
YB1=sol(4);
T12=sol(5);
T13=sol(6);
T14=sol(7);
T15=sol(8);
function G=funzmia2(x)
XA1=x(1);
YA1=x(2);
XB1=x(3);
YB1=x(4);
T12=x(5);
T13=x(6);
T14=x(7);
T15=x(8);
XA0=2.1;
YA0=0.6;
XB0=1.5;
YB0=4.2;
x1=1;
y1=1;
x=[2,3,2,1.5];
y=[0.5,1.5,2,1.9];
G(1)=XA1*XA0+YA1*YA0-x(1)*XA0-y(1)*YA0-x1*XA1-y1*YA1+(x1^2+y1^2+x(1)^2+y(1)^2)/2+cos(T12)*(-XA1*XA0-YA1*YA0+x1*XA0+y1*YA0-x1*x(1)-y1*y(1)+x(1)*XA1+y(1)*YA1)+sin(T12)*(XA0*YA1-YA0*XA1-y1*XA0+x1*YA0+x(1)*y1-x1*y(1)+y(1)*XA1-x(1)*YA1);
G(2)=XA1*XA0+YA1*YA0-x(2)*XA0-y(2)*YA0-x1*XA1-y1*YA1+(x1^2+y1^2+x(2)^2+y(2)^2)/2+cos(T13)*(-XA1*XA0-YA1*YA0+x1*XA0+y1*YA0-x1*x(2)-y1*y(2)+x(2)*XA1+y(2)*YA1)+sin(T13)*(XA0*YA1-YA0*XA1-y1*XA0+x1*YA0+x(2)*y1-x1*y(2)+y(2)*XA1-x(2)*YA1);
G(3)=XA1*XA0+YA1*YA0-x(3)*XA0-y(3)*YA0-x1*XA1-y1*YA1+(x1^2+y1^2+x(3)^2+y(3)^2)/2+cos(T14)*(-XA1*XA0-YA1*YA0+x1*XA0+y1*YA0-x1*x(3)-y1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B1*YB0-x(4)*XB0-y(4)*YB0-x1*XB1-y1*YB1+(x1^2+y1^2+x(4)^2+y(4)^2)/2+cos(T15)*(-XB1*XB0-YB1*YB0+x1*XB0+y1*YB0-x1*x(4)-y1*y(4)+x(4)*XB1+y(4)*YB1)+sin(T15)*(XB0*YB1-YB0*XB1-y1*XB0+x1*YB0+x(4)*y1-x1*y(4)+y(4)*XB1-x(4)*YB1);
end
  8 Comments
Steven Lord
Steven Lord on 22 Jan 2020
So I have a lot of solutions (because sin and cos if I understood) and to make the system converge in one of the solution I have to set start condition near to that solutions. Is it right?
Not necessarily. Basins of attraction can be tricky sometimes. See for example the picture associated with the Basins of Attraction section on Wikipedia's Attractor page. You could converge to the root corresponding to the big yellow region to the right of the picture from a few points on the left edge of the picture. Or you could move just a little bit up from that small yellow region on the left size of the image and end up converging to the roots corresponding to either the blue or red regions.
John D'Errico
John D'Errico on 23 Jan 2020
As it turns out, trig functions seem to create infinitely many solutions much of the time, although that is not lways true. I can easily create a simple problem with a trig function in it that has 0,1,2,3, ... solutions. In fact, any finite number of solutions. Or infinitely many solutions. But the presense of trig functions tends to often create infinitely many solutions. As your variables enter here, I would predict infinitely many of them, but that is just a guess.
Most nonlinear problems tend to have multiple solutions. I can make some simple generic arguments as to why that is so, but perhaps we can accept that claim to be true. There will usually be more solutions as you go higher in the number of dimensions. And the more highly nonlinear your function is, the more solutions you should expect. Products, squares, cubes of variables? More solutions. For example, how many roots does a cubic polynomial have? (3, if you count the complex ones.) Again, the argument would be a simple intuitive one.
So with 8 dimensions and a fairly complicated function, I will confidently predict the existence of many solutions.
To find a speciic solution, it HELPS to start near the solution. As Steven points out, a given basin of attraction is often not a simple set, and two start points near each other can easily diverge to different solutions. (It is very easy to design a function that does all sorts of wierd things.) But it does help to start near the "desired" solution.
How can you find ALL solutions? Since there may be infinitely many solutions, you cannot do so, because infinity is a long way away. If there are finitely many solutions, then you can try to use multi-start/random start methods to find them all, clustering the results, since many start points will converge to essentially the same solution. Is this necessarily trivial? Not always. In 8 dimensions, with a fairly nonlinear function, lets say we predict there may be dozens of solutions to your problem. But how do you know when to stop looking? An 8 dimensional space is a big place to search, a relatively huge one, in fact. Could there be some solution that is difficult to find hidden away, in a spot with a tiny basin of attraction? Yes, that can happen. Not all basins of attraction have the same volume. So for a tiny one, the probability of landing a random point in the necessary region by a random multi-start method can be small.
In fact, it is trivial to write a one variable problem with some solutions that can essentially never be found, without using billions or trillions of start points. Thus, find all roots of the function f(x) == 0...
f(x) = x + delta(x - pi) - 10
where delta(x) is a differentiable function that approaches a spike of infinitely small width, but is essentially zero everywhere else. We know that one root lies at essentially x==10. There will be two more roots that lie very near x==pi. But the basin of attraction of those roots can be made infinitesimally small, if the width of the delta function is made as small as we desire.
So you may need to have many millions of start points in some cases to be confident. And since it is highly probable that you have infinitely many solutions, when do you stop searching?
At best, a simple scheme can search until no more new, distinct solutions have been found recently. (Remember that clustering is highly important here, since multiple solutions will terminate in a ball around a given true solution.) Given some sample size, you can then declarer that if another unfound solution does exist, the associated basin of attraction must have a volume no larger than some value. So using a random multi-start method, you can assign some probability that you have found "all" solutions, at least all with a reasonably large basin of attraction. The problem is that in higher dimensions, volume is a nasty thing.
For example, what is the "volume" of an n-dimensional sphere, of radius R? Not difficult to compute, but easier to look up online.
We see that if I define V(n,r) as the volume of a sphere of radius r in n dimensions...
Vnr = @(n,r) pi.^(n/2).*r.^n./gamma(n/2+1);
What does the volume of an n-sphere of radius 10 grow to, as the dimension varies from 1 to 8?
format short g
Vnr(1:8,10)
ans =
20 314.16 4188.8 49348 5.2638e+05 5.1677e+06 4.7248e+07 4.0587e+08
That radius 10 sphere has volume of 408 million in 8 dimensions. So a sample size of 20 in one dimension grows by a factor of over 20 million in 8 dimensions to get a similar coverage density.

Sign in to comment.

Answers (2)

Are Mjaavatten
Are Mjaavatten on 22 Jan 2020
Edited: Are Mjaavatten on 22 Jan 2020
Your function has several different zeros. Which one fsolve finds will depend on the starting point. I ran:
f = 100; while norm(f)>1e-5;x0 = randn(1,8);x = fsolve(@funzmia2,x0);f = funzmia2(x); end;x
a few times and came up with these solutions:
x =
0.6074 -1.1271 -0.5864 0.9970 -0.0239 0.7690 1.5855 1.9381
x =
0.8133 -1.3517 3.0430 -0.0529 3.8244 0.5740 -1.1023 -0.7352
x =
1.3195 0.4235 -4.7913 10.3315 -1.2170 -0.7230 -0.1282 -0.2885
x =
1.4074 0.3822 -18.0967 27.4654 0.3974 -0.8546 -0.2216 -0.3651
To find the solution you seek, say xstar, you should set x0 to be not too different from xstar.
  2 Comments
Federico MegaMan
Federico MegaMan on 22 Jan 2020
Perfect, there are my solution in one of these x.
So if I go random i discover the different solution of system. Insted to pick one of that solution I have to set x0 near that solution, is it right?
Are Mjaavatten
Are Mjaavatten on 23 Jan 2020
Edited: Are Mjaavatten on 23 Jan 2020
The simplest way to find an x0 that gives your solution is probably to run the code in my previous comment until you get the solution you seek. Then you know that starting with the current x0 will converge to the sought soltion.
By the way: What makes this solution "better" than the others? If there are bounds on your variables (e.g. all elements of x must be positive) you should try another approach. Take a look at this page. Alternatively, you might try my broyden function. This is less robust than fsolve, but the formulation of bounds is quite straight-forward.

Sign in to comment.


Alex Sha
Alex Sha on 6 Sep 2023
Edited: Alex Sha on 6 Sep 2023
There are some solutions like below
No. 1 2 3 4 5
xa1 0.835289443057692 1.97769580537186 0.119396464246135 0.813305799520437 0.813305799523252
ya1 -0.911952025069297 2.56204412078944 -4.10419747212045 -1.3516952883515 -1.35169528829444
xb1 3.16956094184193 2.52727147266688 -0.478993427259013 3.04298779138584 3.04298779149674
yb1 -0.192974988365331 0.495158145943572 1.05384368309623 -0.0529051982349412 -0.0529051983643883
t12 0.52783376905384 3.44925679190791 -15.32615640401 3.82442761233437 -2.45875769478231
t13 0.576993557107166 -8.32808879128648 0.773504081931409 6.85721216626806 0.574026859090344
t14 -1.07931492658616 -26.3674349703953 1.62294371082123 -1.10225944072511 -1.10225944071642
t15 -0.709634319590394 -0.883474122659977 -16.8672147232274 -7.01835799147342 -7.01835799146812

Community Treasure Hunt

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

Start Hunting!