求方程解的更简单方法
我有以下等式:
f(N): N = ((1+lam)^3 )/ ((1-lam)*(1+lam^2));
我需要创建一个函数来查找指定 N
的 lam
。
现在我正在使用简单的循环来完成它:
lam = 0.9999;
n = f(lam);
pow = 0;
delta = 0.1;
while(abs(N - n)) > 0.1 & pow < 10000)
lam = lam - 0.001;
n = f(lam)
pow = pow+1;
end
如何在不使用循环的情况下更准确地解决它?
I have following equation:
f(N): N = ((1+lam)^3 )/ ((1-lam)*(1+lam^2));
I need to create a function that finds lam
for specified N
.
Right now I'm doing it using simple loop:
lam = 0.9999;
n = f(lam);
pow = 0;
delta = 0.1;
while(abs(N - n)) > 0.1 & pow < 10000)
lam = lam - 0.001;
n = f(lam)
pow = pow+1;
end
How can I solve it more accurate and without using loops?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你有
那么你知道
假设你要扩展这些术语?合并成一个简单的三次方程,其实系数为零?有没有一个功能可以帮你解决这个问题?
答案是肯定的。一种解决方案可能是使用 fzero,但由于方程只是一个三次多项式,因此根就是答案,除非您需要符号解。使用符号工具箱解决符号问题。
If you have
then you know that
Suppose you were to expand these terms? Coalesce into one simple cubic equation, with real coefficients, equal to zero? Is there a function that will solve it for you?
The answer is yes. One solution might be to use fzero, but since the equation is just a cubic polynomial, roots is the answer unless you needed a symbolic solution. Use the symbolic toolbox for symbolic problems.
这是 Wolfram Alpha 提出的 N=10 的解决方案:
代数解决方案适用于您的特定情况,因为它并不是非常困难。问题在于,一般来说,非线性方程需要迭代解:从猜测开始,朝特定方向迈进,并希望收敛到解。如果没有迭代和循环,您就无法求解一般的非线性方程。
Here is a solution for N=10 by Wolfram Alpha:
An algebraic solution will work for your particular case, because it's not terribly difficult. The problem is that, in general, non-linear equations require an iterative solution: start with a guess, step in a particular direction, and hopefully converge to a solution. You can't solve non-linear equations in general without iteration and looping.
将方程重新排列为
0 = f(x)/g(x)
(其中f
和g
是多项式)。然后求解0 = f(x)
。这应该很容易,因为f
将是立方的(http:// en.wikipedia.org/wiki/Cubic_function#Roots_of_a_cubic_function)。事实上,Matlab 有roots()
函数来执行此操作。Rearrange the equation to be
0 = f(x)/g(x)
(wheref
andg
are polynomials). Then solve for0 = f(x)
. This should be easy enough asf
will be cubic (http://en.wikipedia.org/wiki/Cubic_function#Roots_of_a_cubic_function). In fact, Matlab has theroots()
function to do this.绘图表明,对于 N 个正数,在区间 [-1,1) 内只有一个解。您应该考虑牛顿法,它会很快收敛到零初始猜测。
Plotting suggest that for N positive, there is exactly one solution in the interval [-1,1). You should consider Newton's method, it will converge for a zero initial guess fairly quickly.
您可以以封闭形式求解该方程,正如其他答案中所讨论的那样,但说实话,次数 > 的多项式的封闭形式解2 在实践中并不是很有用,因为结果往往条件较差。
对于你的特定多项式,我同意亚历山大的观点,牛顿方法可能是正确的选择。
但从长远来看,我强烈建议编写(或从互联网上重用)Jenkins-Traub 寻根算法的实现。维基百科将其描述为“实际上是黑盒多项式求根器的标准”,他们并没有夸大其词。多年来它满足了我所有多项式求解的需求;根据我的经验,它比牛顿方法(不依赖于良好的初始猜测)和基于特征值的方法更稳健,并且启动速度相当快。
You can solve this equation in closed form, as discussed in other answers, but to be honest, closed-form solutions to polynomials of degree > 2 are not very useful in practice, because the results tend to be poorly conditioned.
For your particular polynomial, I agree with Alexandre that Newton's method is probably the way to go.
In the long run, though, I highly recommend writing (or reusing from the Internet) an implementation of the Jenkins-Traub root-finding algorithm. Wikipedia describes it as "practically a standard in black-box polynomial root-finders," and they're not exaggerating. It has served all of my polynomial-solving needs for years; in my experience it's more robust than Newton's method (no reliance on a good initial guess) and eigenvalue-based methods, and is quite fast to boot.
对于大多数 N 值,您的问题有一个代数解决方案。这是解决方案,由 Wolfram Alpha 解决:
是的,很丑。
如果你有一个精确的代数解,即使是像这样一个又大又难看的解,也总是优于数值解。正如达菲莫所指出的,用数值方法解决问题需要迭代(因此速度很慢),并且求解器可能会陷入局部最小值。
There is an algebraic solution to your problem for most values of N. Here is the solution, as solved by Wolfram Alpha:
Yes, it's ugly.
If you have one, an exact algebraic solution, even a big ugly one like this one, is always superior to a numerical solution. As duffymo indicated, solving a problem with numerical methods require iterations (so it's slow), and the solver can get stuck in local minima.