本文介绍了为什么MATLAB无法给出四次方程的正确结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管 MATLAB 拥有内置 solve()来求解四次方程,但solve()函数在某些情况下仍会提供一些错误信息.因此,我编写了一个名为solveQuartic()的用户定义函数来求解四次方程的根.

Although the MATLAB owns the built-in solve() to solve the quartic equation, the solve() function gives some error information in some cases. So I write a user-defined function called solveQuartic() to solve the roots of quartic equations.

我找到的参考文献是此处

我的试用版

function [res] = solveQuartic(a, b, c, d, e)
 p = (8*a*c - 3*b^2)/(8*a^2);
 q = (b^3 - 4*a*b*c + 8^a^2*d)/(8*a^3);

 delta0 = c^2-3*b*d + 12*a*e;
 delta1 = 2*c^3 - 9*b*c*d + 27*b^2*e + 27*a*d^2 - 72*a*c*e;

 Q = ((delta1 + sqrt(delta1^2 - 4*delta0^3))*0.5)^(1/3);
 S = 0.5*sqrt(-2/3*p + (Q + delta0/Q)/(3*a));

 x1 = -b/(4*a) - S - 0.5*sqrt(-4*S^2-2*p + q/S);
 x2 = -b/(4*a) - S + 0.5*sqrt(-4*S^2-2*p + q/S);
 x3 = -b/(4*a) + S - 0.5*sqrt(-4*S^2-2*p - q/S);
 x4 = -b/(4*a) + S + 0.5*sqrt(-4*S^2-2*p - q/S);

  res = [x1; x2; x3; x4];
end

测试

res = solveQuartic(1,2,3,4,5)
-4.1425 - 0.0000i
 1.5669 + 0.0000i
 0.2878 + 3.3001i
 0.2878 - 3.3001i


但是,当我在 Mathematica 中实现公式时,如下所示:


However, when I implement the formula in Mathematica as below:

solveQuartic[a_, b_, c_, d_, e_] :=
 Module[{p, q, delta0, delta1, Q, S, x1, x2, x3, x4},
  p = (8*a*c - 3*b^2)/(8*a^2);
  q = (b^3 - 4*a*b*c + 8^a^2*d)/(8*a^3);
  delta0 = c^2 - 3*b*d + 12*a*e;
  delta1 = 2*c^3 - 9*b*c*d + 27*b^2*e + 27*a*d^2 - 72*a*c*e;
  Q = ((delta1 + Sqrt[delta1^2 - 4*delta0^3])*0.5)^(1/3);
  S = 0.5*Sqrt[-2/3*p + (Q + delta0/Q)/(3*a)];

  x1 = -b/(4*a) - S - 0.5*Sqrt[-4*S^2 - 2*p + q/S];
  x2 = -b/(4*a) - S + 0.5*Sqrt[-4*S^2 - 2*p + q/S];
  x3 = -b/(4*a) + S - 0.5*Sqrt[-4*S^2 - 2*p - q/S];
  x4 = -b/(4*a) + S + 0.5*Sqrt[-4*S^2 - 2*p - q/S];
  {x1, x2, x3, x4}
]

solveQuartic[1, 2, 3, 4, 5] // MatrixForm

此外,我可以使用Solve[]修改答案

In addition, I can use the Solve[] to varify my answer

Solve[{1, 2, 3, 4, 5}.Table[x^i, {i, 4, 0, -1}] == 0, x] // N // TableForm
  • 有人知道原因吗? THX很多:)

推荐答案

您在计算p时遇到了错字:您写了8^a^2*d,而它应该是8*a^2*d.

You have a typo in the calculation of p: You write 8^a^2*d, while it should be 8*a^2*d.

使用

q = (b^3 - 4*a*b*c + 8*a^2*d)/(8*a^3);

您得到的结果

res =

  -1.2878 - 0.8579i
  -1.2878 + 0.8579i
   0.2878 - 1.4161i
   0.2878 + 1.4161i

根据需要.

这篇关于为什么MATLAB无法给出四次方程的正确结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:52