Iam confuse with cubic equation alghoritma and code.

x^3 + ax^2 + bx + c =0

Please help me

You could try to use Google

Perhaps you tried to use ^ as "raise to power" and got some very weird answers.
What you need to realise is that you need to call the pow() function in math.h, because ^ is the bitwise-xor operator.

I need a program for solving Cubic equation " ax^3+bx^2+cx+d=0 " , too .

if anyone can write it , plz put it here , i really need that.

thanks. :-|

i is the root of -1.
Or more precisely i^2 = -1

Member Avatar for iamthwee

i is the root of -1.
Or more precisely i^2 = -1

Never knew dat. How do regular math on that. Is there some special nomenclature or something?

It is used to define the Complex Number Space. But I dont think calculations of it is that difficult. After the definition it is mostly algebra.

if u have to include i after all the calculations, the ans. will be some thing like a+ib, whr a and b are two numbers..... as example 2+3i or 4-7i ....... n ans.s shud look like this.

Never knew dat. How do regular math on that. Is there some special nomenclature or something?

No special nomenclature, except that i * i = -1. Then you can add, multiply, subtract, and divide complex numbers of the form (a + bi), where a and b are real numbers -- and get complex numbers back. For example, (a + bi) + (c + di) = (a + c) + (b + d)i. Or,

(a + bi) * (c + di) = (ac - bd) + (ad + bc)i

Division uses a bit of a trick:

(a + bi) / (c + di) =
(a + bi) * (c - di) / [(c + di) * (c - di)] =
(ac + bd + bci - adi) / (c*c + d*d) =
[(ac + bd) / (c*c + d*d)] + [(bc - ad) / (c*c + d*d)]i

Then, for example, to "simplify" the expression sqrt(-5), you can use:
sqrt(-5) = sqrt(5) * sqrt(-1) = sqrt(5) * i. That's not really much simpler than saying sqrt(-5).

here is the answer X are the roots :

x = -b/(3*a) - (2^(1/3)*(-b^2 + 3*a*c))/(3*a*(-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)) + (-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)/(3*2^(1/3)*a)

x = -b/(3*a) + ((1 + i*Sqrt[3])*(-b^2 + 3*a*c))/(3*2^(2/3)*a*(-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)) - (1 - i*Sqrt[3])*(-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)/(6*2^(1/3)*a)

x = -b/(3*a) + ((1 - i*Sqrt[3])*(-b^2 + 3*a*c))/(3*2^(2/3)*a*(-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)) - (1 + i*Sqrt[3])*(-2*b^3 + 9*a*b*c - 27*a^2*d + Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3)/(6*2^(1/3)*a)


but there is a big prob , this answer is in ASCII , if any can change it to C++ , :-| ,

plz change that to C++ , i use Borland for writing and compiling .

real thanks to u , :-| .
help plz , :-|

I Found this here :

http://www.josechu.com/ecuaciones_polinomicas/cubica_solucion.htm

y don't u try this way to solve:
at first solve for p=Sqrt[4*(-b^2 + 3*a*c)^3 + (-2*b^3 + 9*a*b*c - 27*a^2*d)^2])^(1/3) ..... n other parts, which r similar in all of those roots..... then just simplify the equations.

n for the ascii part, go thr: http://www.lispme.de/lispme/doc/lm_synt.htm ... i hope u can solve it now :)

Member Avatar for iamthwee

Just out of interest, is it possible to solve cubic equations with complex solutions, based purely on iterative methodologies?

@Strange_Man
I think the only way you can do that, is if you write your own complex number class.

The power functions, should be easy. As someone else mentioned earlier you can use pow from math.h

2^3 <=> pow(2,3.0)

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

Yes. Newton's method works pretty well with third-degree polynomials, even if you use complex numbers. I think it actually converges all the time. Here's an example written in Scheme

(define (cubic-eq a b c d)
  (vector a b c d))

(define term vector-ref)

(define (cubic-evaler eq)
  (lambda (x)
    (+ (* (+ (* (+ (* (term eq 3)
		      x)
		   (term eq 2))
		x)
	     (term eq 1))
	  x)
       (term eq 0))))

(define (diff cubic)
  (cubic-eq (term cubic 1)
	    (* 2 (term cubic 2))
	    (* 3 (term cubic 3))
	    0))

(define (newton cubic start thresh displayer)
  (let* ((f (cubic-evaler cubic))
	 (fp (cubic-evaler (diff cubic))))
    (define (rec pt n esc)
      (displayer pt)
      (newline)
      (if (= n 0)
	  pt
	  (rec (- pt (/ (f pt)
			(let ((yp (fp pt)))
			  (if (= 0 yp)
			      (esc #f)
			      yp))))
	       (- n 1)
	       esc)))
    (call-with-current-continuation
     (lambda (esc) (rec (exact->inexact start) thresh esc)))
    ))

(define (show-newtons eq guess n)
  (newton eq guess n display))

;;; Use show-newtons to play with Newton's method and cubics.
;;; This uses 4x^3 + 3x^2 + 2x + 1, with an initial guess of 1+1i,
;;; and ten iterations.
(show-newtons (cubic-eq 4 3 2 1) 1+1i 10)
Member Avatar for iamthwee

Yes. Newton's method works pretty well with third-degree polynomials, even if you use complex numbers. I think it actually converges all the time.

I can't see how? If one root is complex, which starting point do you choose. How do you find it?

[edit] can you port that to c++ please[/edit]

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

See my just-edited post (and, um, get a Scheme interpreter, hehe). You can choose complex numbers as your initial guess. You can also have cubic equations with complex coefficients!

Scheme happens to have built-in support for complex numbers, and I happened to be using it at the time. Maybe Python would have been a more readable choice.

[edit]port it yourself[/edit]

Member Avatar for iamthwee

I think I understand, as long as you write newton's formula to handle complex numbers in the first place???
I'm still new to complex numbers.


>get a Scheme interpreter, hehe

Not a chance he he.

>[edit]port it yourself[/edit]
:sad:

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

What do you mean, not a chance? There are countless free ones, such as DrScheme.

Member Avatar for iamthwee

What do you mean, not a chance? There are countless free ones, such as DrScheme.

I just meant I can't be bothered to waste my time to learn the semantics of another language. You on the other hand, seem to enjoy such pastimes. :cheesy:

;)

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

No one can change it to C++ ? :cry: :cry: :o

Member Avatar for iamthwee

No one can change it to C++ ? :cry: :cry: :o

One way to do that, is to use the formulae given by Rashakil Fol. And separate it into parts.

So, using an arbitary example, if you had to work out the formula for sqrt((5 + b)^3)

First, I would work out what 5+b was, then I would cube it and finally square root it...

double x = 5 + b
double y = pow(x,3.0);
double z = sqrt(y);

This way you are more likely to arrive at the correct solution.

The only bottleneck I can see is where the complex numbers come into play.

Like I said before you will have to implement a complex number class to deal with this or use a pre-existing complex number library from the net.

You can probably get away without defining a method for complex number division.

For example the following term, in the cubic formula:

[1 + i * sqrt(3)] / 2

could be simplified to

0.5 + 0.8660254038 i

This may introduce some rounding errors... I'm not entirely sure how bad this will affect the final solution.

Here is a sample complex number class which handles addition,subtraction and multiplication...

complex.cpp

#include <iostream>
#include <cmath>

using namespace std;

class Complex {
public:
  Complex();                  //Default constructor
  ~Complex();                 //Default destructor.
  void Set(double new_real, double new_imaginary); //Set data members.
  double Real();              //Return the real part.
  double Imaginary();         //Return the imaginary part.
  Complex operator=(Complex); //Overloaded = operator
  int operator==(Complex);    //Overloaded == operator
  Complex operator+(Complex); //Overloaded + operator
  Complex operator*(Complex); //Overloaded * operator
  Complex operator-(Complex); //Overloaded - operator
  //Complex operator/(Complex); //Overloaded / operator ~don't need this
private:
  double real;
  double imaginary;
};

//
//Complex constructor, initialises to 0 + i0.
Complex::Complex() {
  real = 0;
  imaginary = 0;
}

//Complex destructor.
Complex::~Complex() 
{
 //
};

//Overloaded = operator. 
Complex Complex::operator=(Complex c) {
  real = c.Real();
  imaginary = c.Imaginary();
  return *this;
}

//Overloaded + operator.
Complex Complex::operator+(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real + c.Real();
  new_imaginary = imaginary + c.Imaginary(); 
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded * operator.
Complex Complex::operator*(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real * c.Real() - imaginary * c.Imaginary();
  new_imaginary = real * c.Imaginary() + imaginary * c.Real();
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded - operator.
Complex Complex::operator-(Complex c) { 
Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real - c.Real();
  new_imaginary = imaginary - c.Imaginary(); 
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded / operator. **Not implemented**
/*
Complex Complex::operator/(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real * c.Real() - imaginary * c.Imaginary();
  new_imaginary = real * c.Imaginary() + imaginary * c.Real();
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}*/

//Overloaded == operator.  Small error tolerances.
int Complex::operator==(Complex c) {
  cout << "Oopsie";
  if (fabs(c.Real() - real) > pow(10,-14.0)) {
    return 0;
  }
  if (fabs(c.Imaginary()- imaginary) > pow(10,-14.0)) {
    return 0;
  }
  return 1;
}

//Sets private data members.
void Complex::Set(double new_real, double new_imaginary) {
  real = new_real;
  imaginary = new_imaginary;
}

//Returns the real part of the complex number.
double Complex::Real() {
  return real;
}

//Returns the imaginary part of the complex number.
double Complex::Imaginary() {
  return imaginary;
}

int main()
{   
    //test
    Complex a;
    a.Set(2,-4); // 2 - 4i
    
    Complex b;
    b.Set(-1,8); // -1 + 8i

    Complex c;
    
    c= a*b;
    
    
    cout<<c.Real()<<" + "<<c.Imaginary()<<"i"<<endl;
    
    c= a+b;
    cout<<c.Real()<<" + "<<c.Imaginary()<<"i";
    //
    
    cin.get();
}

Now it's just a case of slipping that complex number crap into your pre-existing cubic formula... Simple stuff.

Enjoy...

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

I think it actually converges all the time.

Newton's formula converges provided the initial approximation is chosen sufficiently close to the root.
It is basically used to improve the results obtained by other methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.