[try Beta version]
Not logged in

 
 
To solve 4th Order equation

Feb 25, 2009 at 4:37pm
Hello

Is there a built-in function in C++ to solve a 4th order equation of the format:-
Ax^4+Bx^3+Cx^2+Dx+E=0

Any tips or hints?

Thanks in advance.
Feb 25, 2009 at 4:42pm
There isn't a built in function.
Try Googling http://www.google.com/search?q=equation+solving+C%2B%2B
Feb 25, 2009 at 4:49pm
Thanks bazzy. I found a code, but am not able to understand what IMAX does in this code.. Can anybody help me with that?

#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>

const int IMAX = 800;

class coeffs
{
public :
float a, b, c, d, e, x, y, p, q, r, z;

void getdata(void);
void divide(void);
void chkrealcmplx(void);
void dispreal(float, float, float);
void dispcomplx(float, float, float);
};


void coeffs :: getdata()
{
cout<<"Enter coefficients 'a' through 'e' :
<BR>;
cin>>a>>b>>c>>d>>e;
}

void coeffs :: divide(void)
{
int i;
float y1,z1;

b = b/a; c = c/a; d = d/a; e = e/a;
a = 1;
y = d/c; z = e/c;
x = 1;

for(i=1;i<=IMAX;i++)
{
y1 = (d-z*(b-y))/((c-z)-y*(b-y));
z1 = e/((c-z)-y*(b-y));
y = y1;
z = z1;

p = 1;
q = b-y;
r = (c-z)-y*(b-y);
}
}


void coeffs :: chkrealcmplx(void)
{
float delta1,delta2;
delta1 = q*q - 4*p*r;
delta2 = y*y - 4*x*z;

if(delta1<0)
{
cout<<"
Roots R1 and R2 are complex<BR>;
cout<<"Roots are :
<BR>;
dispcomplx(delta1,p,q);
}
if(delta2<0)
{
cout<<"
Roots R3 and R4 are complex<BR>;
cout<<"Roots are :
<BR>;
dispcomplx(delta2,x,y);
}
if(delta1>=0)
{
cout<<"
Roots R1 and R2 are real<BR>;
cout<<"Roots are :
<BR>;
dispreal(delta1,p,q);
}
if(delta2>=0)
{
cout<<"
Roots R3 and R4 are real<BR>;
cout<<"Roots are :
<BR>;
dispreal(delta2,x,y);
}
}

void coeffs :: dispreal(float delta,float A,float B)
{
float r1,r2;

r1 = (-B+sqrt(delta))/(2*A);
r2 = (-B-sqrt(delta))/(2*A);

cout<<r1<<endl;
cout<<r2<<endl;
}

void coeffs :: dispcomplx(float delta,float A,float B)
{
float rp,ip;

delta = -delta;
rp = -B/(2*A);
ip = (sqrt(delta))/(2*A);

cout<<rp<<" +j "<<ip<<endl;
cout<<rp<<" -j "<<ip<<endl;

}

void line()
{
char t = 0XC4;
for(int i = 1;i<=80;i++)
cout<<t;
}


void main()

{
clrscr();

int gdriver = EGA, gmode = VGAHI, errorcode;
initgraph(&gdriver, &gmode, "c:\tc\bgi");
setbkcolor(BLUE);
coeffs coefficients;
line();
cout<<" PROGRAM TO SOLVE A FOURTH ORDER ALGEBRAIC EQUATION ";
line();
coefficients.getdata();
line();
line();
coefficients.divide();
coefficients.chkrealcmplx();
line();
line();
getch();
closegraph();
}
Feb 25, 2009 at 4:53pm
I guess its not the best method as IMAX specifies the number of iterations, for the calculation of the approximate value.

Higher the value of IMAX, better the approximation.

Any Mathematicians here?
Feb 25, 2009 at 4:58pm
That value can be modified easily, that ( I think ) is the reason for declaring IMAX at the beginning of the code.

A higher value can lead to a higher precision but even to a longer execution time
Feb 25, 2009 at 5:08pm
http://en.wikipedia.org/wiki/Quartic_equation

If I understand this correctly,
x1=(3*a^2)/(8*a^2)+c/a;
x2=b^3/(8*a^3)-(b*c)/(2*a^2)+d/a;
x3=(-3*b^4)/(256*a^4)+(c*b^2)/(16*a^3)-(b*d)/(4*a^2)+e/a;
//"(+-)" means "plus or minus"
if !x2 then
x4=-b/(4*a)(+-)((-x1(+-)(x1^2-4*x3)^.5)/2)^.5
else
//Too long. You get the general idea.
Last edited on Feb 25, 2009 at 5:20pm
Feb 26, 2009 at 1:06pm
Alternatively there are a variety of root-finding algorithms for n-th order polynomials -- bisection method, secant method, newton-rhapson method, etc, all of which are explained in detail in Wikipedia.
Feb 27, 2009 at 1:02pm
Do you want to approximate the roots (numerically) or do you want to solve the equation (algebraically)? (The latter is still possible, since the degree isn't 5 or above, in which case you'd be out of luck...)
Mar 23, 2009 at 12:35pm
Hi above

I am sorry, I did not check the forum for a month due to examinations! I still have this work pending.

I need to implement a program which is very fast to solve this equation. So I guess an approximated value would also do! I cannot afford to use programs which runs iterations 1000 times to get the corrrect value. Speed of calculation is of utmost importance.

@Exception

Are you still there? Can you offer any help?
Apr 20, 2009 at 3:03pm
I found the solution for this. There is a perfect code for it in Google-codes and it runs splendidly. If anybody wants to use a function which analytically solves 4th order equations, use this :-

http://www.google.com/codesearch/p?hl=de#rmG-ZVXP6CY/geant4.8.2.p01/source/global/HEPNumerics/src/G4AnalyticalPolSolver.cc&q=QuarticRoots
Apr 20, 2009 at 3:03pm
I found the solution for this. There is a perfect code for it in Google-codes and it runs splendidly. If anybody wants to use a function which analytically solves 4th order equations, use this :-

http://www.google.com/codesearch/p?hl=de#rmG-ZVXP6CY/geant4.8.2.p01/source/global/HEPNumerics/src/G4AnalyticalPolSolver.cc&q=QuarticRoots
Topic archived. No new replies allowed.