How to solve complex equation
显示 更早的评论
Hello, I'm completely new to Matlab and was wondering if there is a simple way to solve the following equation:
x^2 - 100*x + 120 = 400*sin(x)
It can't really be solved symbolically for all solutions, so it should be solved numerically? How can Matlab solve and show all solutions in a simple way for me to understand?
回答(3 个)
Star Strider
2018-1-19
Use fzero, a loop, and uniquetol:
f = @(x) x^2 - 100*x + 120 - 400*sin(x);
for k1 = 1:100
s(k1) = fzero(f, (k1-1)*pi);
end
s = uniquetol(s, 1E-3)
s =
0.2420 3.8037 5.1258 97.6633 102.4703
It is not efficient, though has the virtue of finding all the solutions.
5 个评论
Birdman
2018-1-19
Then this answer should do it for you Brahma.
Brahma
2018-1-19
Brahma
2018-1-19
Star Strider
2018-1-19
- Not that I am aware of
- Correct. They all do require some knowledge of MATLAB and programming in general. That is the essence of this forum!
Wolfram Alpha (link) will give you a list of the roots.
David Goodmanson
2018-1-20
It found all the solutions except for the one that it missed:
x0 = 101.16908798537
x0.^2 - 100*x0 + 120 -400*sin(x0)
ans = 5.9433e-10
It can't really be solved symbolically for all solutions, so it should be solved numerically? How can Matlab solve and show all solutions in a simple way for me to understand?
There is no general way to numerically determine either the number of roots or their locations for an arbitrary function. How do you even know that the function in your example has 5 roots? From a plot? You can't rely on that. As a different example, a plot of sin(1/x) will show some of the roots, but never all of them, no matter how finely you plot nor how much you zoom in.
6 个评论
David Goodmanson
2018-1-20
Hi Matt,
Actually I think that in most situations relying on a plot is a good thing, and that people don't do nearly enough of it. It's an easy way to find out what you should expect. Of course there are exceptions as you mentioned, but see the comment to the second answer above. A sixth root is obvious on a plot.
Matt J
2018-1-20
Hi David,
A plot is better than nothing, just not enough. How do you know that there isn't a 7th root?
David Goodmanson
2018-1-20
Hi Matt,
Since you are asking about the seventh root in particular, are you referring to this particular case? From the plot it's 100% obvious that there are six and only six roots. Especially if the x array spacing is made small enough so that there are several hundred points per sine wave cycle, which I did. The parabola is covered since its second derivative is 200 times smaller than that of the sine wave.
The general problem, of course, that's a different thing. As you say, plots can't prove everything all the time. But compared to the problematic cases, there there are so many more cases of people (especially people with less experience, as are often on this site) turning on a solver, getting a certain number of roots and calling it good. Or searching for roots symbolically, not finding any, wondering if they exist, when simply looking at a plot would say that they are there.
The parabola is covered since its second derivative is 200 times smaller than that of the sine wave.
Ah, but that's different. You used not just the plot, but problem-specific calculus to figure out analytically what the minimum spacing between roots could be. You also presumably did similar analysis to figure out a bound on the interval where roots could lie (else how did you know where to plot?).
In any case, I agree with you that there are probably people out there who under-utilize plots. As for whether there are "so many more cases", I think that's difficult to count, but it might be true.
Walter Roberson
2018-1-20
I am not convinced that all of the roots are real-valued.
David Goodmanson
2018-1-20
编辑:David Goodmanson
2018-1-21
Hi Matt & Walter,
Clearly one can't rely uncritically on either plots or solvers. I should have stated it better initially, that plots are valuable but, as you point out, only when combined with a healthy dose of analysis plus common sense or whatever you want to call it.
In one of the other answers to this question it's claimed that the function has just five (real) roots. In this case a plot was invaluable (and I believe the quickest and easiest way) at showing that there are exactly six real roots. **
As to Walter's comment, no doubt there are a lot of complex roots, quite possibly an infinite number. For example
z = 17.181087010965761 + i*1.857398520646881
and its complex conjugate. It's true that the title of the question does include 'complex', but I think that finding the real roots was the intent.
** There is also the topological evidence that if you think that it's highly unlikely that the parabola is going to be tangent to the sine wave at any single point, then since the parabola goes to +oo as x goes to +-oo, and the sine wave stays basically along a horizontal line, those two curves have to cross an even number of times. So five roots is suspicious on that basis, and the plot confirms it.
类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!