MATLAB Answers

Fsolve don't work good with trigonometric

10 ビュー (過去 30 日間)
Federico MegaMan
Federico MegaMan 2020 年 1 月 22 日
コメント済み: John D'Errico 2020 年 1 月 23 日
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);
function G=funzmia2(x)
  8 件のコメント
John D'Errico
John D'Errico 2020 年 1 月 23 日
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
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.


回答 (1 件)

Are Mjaavatten
Are Mjaavatten 2020 年 1 月 22 日
編集済み: Are Mjaavatten 2020 年 1 月 22 日
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 件のコメント
Are Mjaavatten
Are Mjaavatten 2020 年 1 月 23 日
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.


Community Treasure Hunt

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

Start Hunting!

Translated by