求方程解的更简单方法

发布于 2024-09-18 09:28:51 字数 381 浏览 4 评论 0原文

我有以下等式:

f(N):  N = ((1+lam)^3 )/ ((1-lam)*(1+lam^2));

我需要创建一个函数来查找指定 Nlam

现在我正在使用简单的循环来完成它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

红衣飘飘貌似仙 2024-09-25 09:28:51

如果你有

N = ((1+lam)^3 )/ ((1-lam)*(1+lam^2))

那么你知道

(1+lam)^3 = N*(1-lam)*(1+lam^2)

假设你要扩展这些术语?合并成一个简单的三次方程,其实系数为零?有没有一个功能可以帮你解决这个问题?

答案是肯定的。一种解决方案可能是使用 fzero,但由于方程只是一个三次多项式,因此根就是答案,除非您需要符号解。使用符号工具箱解决符号问题。

If you have

N = ((1+lam)^3 )/ ((1-lam)*(1+lam^2))

then you know that

(1+lam)^3 = N*(1-lam)*(1+lam^2)

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.

憧憬巴黎街头的黎明 2024-09-25 09:28:51

这是 Wolfram Alpha 提出的 N=10 的解决方案:

http://www.wolframalpha.com/input/?i=(1%2Bx^3)/((1-x)*(1%2Bx^2))%3D10

代数解决方案适用于您的特定情况,因为它并不是非常困难。问题在于,一般来说,非线性方程需要迭代解:从猜测开始,朝特定方向迈进,并希望收敛到解。如果没有迭代和循环,您就无法求解一般的非线性方程。

Here is a solution for N=10 by Wolfram Alpha:

http://www.wolframalpha.com/input/?i=(1%2Bx^3)/((1-x)*(1%2Bx^2))%3D10

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.

诠释孤独 2024-09-25 09:28:51

将方程重新排列为 0 = f(x)/g(x)(其中 fg 是多项式)。然后求解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) (where f and g are polynomials). Then solve for 0 = f(x). This should be easy enough as f will be cubic (http://en.wikipedia.org/wiki/Cubic_function#Roots_of_a_cubic_function). In fact, Matlab has the roots() function to do this.

说不完的你爱 2024-09-25 09:28:51

绘图表明,对于 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.

绝不放开 2024-09-25 09:28:51

您可以以封闭形式求解该方程,正如其他答案中所讨论的那样,但说实话,次数 > 的多项式的封闭形式解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.

九局 2024-09-25 09:28:51

对于大多数 N 值,您的问题有一个代数解决方案。这是解决方案,由 Wolfram Alpha 解决:

if N+1!=0
   x = (20 N^3+18 N^2+3 sqrt(3) sqrt(16 N^6+32 N^5-20 N^4-72 N^3-9 N^2+54 N+27)-27 N-27)^(1/3)/(3 2^(1/3) (N+1))-(2^(1/3) (2 N^2+3 N))/(3 (N+1) (20 N^3+18 N^2+3 sqrt(3) sqrt(16 N^6+32 N^5-20 N^4-72 N^3-9 N^2+54 N+27)-27 N-27)^(1/3))+N/(3 (N+1))

是的,很丑。

如果你有一个精确的代数解,即使是像这样一个又大又难看的解,也总是优于数值解。正如达菲莫所指出的,用数值方法解决问题需要迭代(因此速度很慢),并且求解器可能会陷入局部最小值。

There is an algebraic solution to your problem for most values of N. Here is the solution, as solved by Wolfram Alpha:

if N+1!=0
   x = (20 N^3+18 N^2+3 sqrt(3) sqrt(16 N^6+32 N^5-20 N^4-72 N^3-9 N^2+54 N+27)-27 N-27)^(1/3)/(3 2^(1/3) (N+1))-(2^(1/3) (2 N^2+3 N))/(3 (N+1) (20 N^3+18 N^2+3 sqrt(3) sqrt(16 N^6+32 N^5-20 N^4-72 N^3-9 N^2+54 N+27)-27 N-27)^(1/3))+N/(3 (N+1))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文