斩波matlab的结果或精确的实数

斩波matlab的结果或精确的实数,matlab,precision,equation-solving,Matlab,Precision,Equation Solving,我有一个关于在MATLAB中求解四次方程的问题,请参见 MATLAB功能如下所示: MATLAB 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*

我有一个关于在MATLAB中求解四次方程的问题,请参见

MATLAB功能如下所示:

MATLAB

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
试验

显然,四次方程$$-65x^4-312x^3-582x^2-488x-153=0$$有两个
实根

现在我想求四次方程的
实根,我的试验如下

function [res] = solveQuarticReals(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];
 %exact the real roots
 j = 0;
  for i = 1 : length(res)
   if imag(res(i)) == 0
    j = j + 1;
    mid(j) = res(i); 
   end
  end
  res = mid;
end
然而,它失败了

问题:
  • MATLAB中的实际根有多精确
  • 或者Mathematica中有内置的类似于
    Chop
a=-65;
b=-312;
c=-582;
d=-488;
e=-153;
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];
%精确地确定真正的根

realNumber=real(res((abs(imag(res))不,不,不!不要在MATLAB中使用增长向量/矩阵。跳过循环,改用逻辑索引!
realNumber=res(real(abs(imag(res))顺便说一句!请继续回答问题!非常感谢您的贡献。我无意吓跑您……这只是一个非常常见的错误,如果您使用这种方法,您的代码将变得非常慢,MATLAB编辑器甚至会警告您。您有我的更新,但我希望您花时间编辑答案。=)我发现我在上面的评论中至少忘记了一个括号,请你弄清楚我猜=)非常感谢你的评论。我会根据你的建议尝试更新我的代码。:-)谢谢~这是一个问题,有完整的代码,并解释你尝试过的内容、你期望的内容和你得到的结果!谢谢!=)
function [res] = solveQuarticReals(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];
 %exact the real roots
 j = 0;
  for i = 1 : length(res)
   if imag(res(i)) == 0
    j = j + 1;
    mid(j) = res(i); 
   end
  end
  res = mid;
end
a=-65;
b=-312;
c=-582;
d=-488;
e=-153;


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];

%exact the real roots  
realNumber = real(res((abs(imag(res)) <= 1e-10)))