0
$\begingroup$

I'm trying to solve a general 4th order equation in one variable in MATLAB. The equation is

$$a x^4 + b x^3 + c x^2 + d x + e = 0$$

I tried this code:

clear;
close all;
clc;

syms a b c d e
syms x

eq1 = a*x^4 + b*x^3 + c*x^2 + d*x + e==0;
x_sol = solve(eq1,x)

But I don't get explicit results. This is what I get:

x_sol=RootOf(a*z^4 + b*z^3 + c*z^2 + d*z + e, z)

I tried in a different way in this code:

clear;
close all;
clc;

syms a b c d e
syms x

coeffs = [a b c d e]; %eq = a*x^4 + b*x^3 + c*x^2 + d*x + e==0;
x_symbolic = roots(coeffs) %solving the equation

a=1; b=0; c=0; d=0; e=-16; %x^4-16=0 (solutions should be [2,-2,2i,-2i]
x_numeric = subs(x_symbolic);
x_numeric = double(x_numeric)

where x_symbolic should give me the symbolic results for the quartic equation. But when I checked the resutls and substituted $[a,b,c,d,e]=[1,0,0,0,-16]$ which means that the equation becomes $x^4-16=0$, I expected to get these 4 results: $[2,-2,2i,-2i]$. but I actually got these four results:

x_numeric =
-1.867e-36 -  1.867e-36i
1.867e-36 -  1.867e-36i
-1.867e-36 +  1.867e-36i
1.867e-36 +  1.867e-36i

Does anybody know why? The goal is to get the correct symbolic solution for the general equation.

$\endgroup$
4
  • $\begingroup$ My full code is in matlab. I need to somehow plant it in matlab even if I use Maple etc. But matlab doesn't say it can't solve the equation. it actually solves it. but when I checked it I got wrong results. so I'm trying to figure out why. $\endgroup$
    – David
    Aug 4, 2017 at 10:36
  • $\begingroup$ I wrote as a reply to the answer below. I actually need to solve thousand of numeric equations of the same form. and I don't want to tell matlab to solve thousand of numeric equations. I want to do it only once symbolically and then substitute the relevant values. my substitution in my code was just to check the symbolic solution (because I know the solution to x^4-16=0 and I could check the code). but this is not the real numeric equation I want to solve. in most of the equations all the coefficients will be different than zero. $\endgroup$
    – David
    Aug 4, 2017 at 13:15
  • $\begingroup$ Yes. I understand now it's a bad idea. I will solve thousands of numerical equations instead. thanks! $\endgroup$
    – David
    Aug 4, 2017 at 13:37
  • $\begingroup$ who ever wrote here, why did you delete your replies? $\endgroup$
    – David
    Aug 4, 2017 at 21:08

1 Answer 1

2
$\begingroup$

Computers: they have limited precision. Once you put floating point numbers in that (ridiculously enormous) root solution, numerical errors get in and screw the result, especially in something like:

https://pastebin.com/YemafsAc (I wanted to put it in here, but the SE doesnt allow me to put more than 30.000 chars in the post).

The solution is correct, but you have errors numerically evaluating the expression.

Is there anything impeding you from doing the following?

syms x
a=1; b=0; c=0; d=0; e=-16; %x^4-16=0 (solutions should be [2,-2,2i,-2i]
coeffs = [a b c d e]; %eq = a*x^4 + b*x^3 + c*x^2 + d*x + e==0;
x_symbolic = roots(coeffs) %solving the equation

Numerically solving these equations is also ridiculously cheap in a computer.

for ii=1:1000
   coeffs=rand(1,5)*20;
   tic;
   x_symbolic = roots(coeffs); %solving the equation
   t(ii)=toc;
end
mean(t)

ans
>> 4.5216e-05
% or a total of 0.045 in my PC.

Compared to the symbolic option:

syms a b c d e
syms x

coeffs = [a b c d e]; %eq = a*x^4 + b*x^3 + c*x^2 + d*x + e==0;
tic
x_symbolic = roots(coeffs); %solving the equation
toc


>> Elapsed time is 1.350836 seconds.

Note that in my PC, you need approximately 100.000 numerical solutions to get to the same time consumption as the symbolic equation solution, and that is only without evaluating the result after. I'll leave as an exercise to the reader the evaluation of the numerical accuracy of roots, but I'll say: its quite accurate!

$\endgroup$
11
  • $\begingroup$ In my code I need to solve thousands of equations of the same form (each time with different coefficients). I wanted to solve the symbolic equation only once and each time to substitute the relevant coefficients to get the numeric solution. Solving thousand of numeric equations might slow down the running (alot!). $\endgroup$
    – David
    Aug 4, 2017 at 13:08
  • $\begingroup$ I still don't understand, even if the symbolic solution is enormous, what is the problem in substituting values into the symbols and why solving the numeric equation should be better? Will I get (approximately) correct results even if I solve the equation numerically where all the coefficients are different than zero? (because that doesn't sound different than solving symbolically and then substituting the values into the symbols). $\endgroup$
    – David
    Aug 4, 2017 at 13:09
  • $\begingroup$ I understand there's limited precision but shouldn't I get something close to the correct solutions (like 1.99 instead of 2)? All the solutions I got were actually zero. it doesn't look like limited precision. $\endgroup$
    – David
    Aug 4, 2017 at 13:09
  • $\begingroup$ @David all your questions come from a limited understanding on the limitations of floating point numbers. In a nutshell: use numerical methods for numerical solutions. Even if the symbolic solution were good to be evaluated, it will still be faster to do it numerically, because symbolic maths are very slow in a machine that is purely numerical! $\endgroup$ Aug 4, 2017 at 13:13
  • 1
    $\begingroup$ @David honestly, it takes 0.0002 on average to compute the numerical solution of each on my PC. I just run 1000 of them and the total time was 0.26s. Yes, solve them numerically $\endgroup$ Aug 4, 2017 at 13:21

Not the answer you're looking for? Browse other questions tagged .