Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,900 people online right now. Registration is fast and FREE... Join Now!




Solving higher degree equations

 
Reply to this topicStart new topic

Solving higher degree equations, A simple and efficent method to solve the equations upto degree=5

umer_alvi
21 Feb, 2007 - 03:14 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2007
Posts: 7


My Contributions
Actually i dont have any problem with coding. Im just stucked at any method which can solve equations of higer degree (upto degree =5)

for example solution of
ax^3+bx^2+cx+d=0


i want to Solve the equations in C Programming.
i want to numerically solve the equations an i only want real values not imaginary.

This post has been edited by umer_alvi: 21 Feb, 2007 - 04:15 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Solving Higher Degree Equations
21 Feb, 2007 - 03:54 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Did you want to symbolicly solve these equations, or numericly solve the equation? If numericly did you want to include the imaginary results?

You could of course solve this ingeneral and then use the formulas to calculate the results... one of the roots looks like this:

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

(as done by mathematica anyway).

however there are lots of numerical methods to solve this much easier than the algebreic sollution.

Newton's method.
x(n+1) <- x(n) - F(x(n))/F'(x(n))

or in the case x(n+1)=(ax(n)^3+bx(n)^2+cx(n)+d)/(3ax(n)^2+2bx(n)+c)

where each x(n) is an approximation of the root and x(0) is an initial guess.

Bisection
Works like a binary search for the root in a interval [a, b].

If f(a)f(b ) < 0 then f(x) crosses the x-axis in this intevel

B1. define c=(a+b )/2
B2. if b-c <= some error tollerance the accept c and stop
B3. else if the sign of f(b ) time the sign of f(c ) <= the a=c else b=c
B4. return to step B1.

This is nice because works with any function and you don't have to calculate a derivitive and you can write a routine to search for a valid interval [a, b].

There are also the secant method, and other Fixed point interation algorithems (both Newton's and Secant methods are versions of this).

A google search on any of those should set you on the path.

This post has been edited by NickDMax: 21 Feb, 2007 - 03:58 PM
User is offlineProfile CardPM
+Quote Post

drr5033
RE: Solving Higher Degree Equations
29 Oct, 2007 - 01:51 PM
Post #3

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 5



Thanked: 1 times
My Contributions
i am trying to create a function for bisection so i can plug in different eqautions and intervals to find the root.
Here's what i have so far. i am very confused.


#include <iostream>
#include <cmath>

using namespace std;

void equation(double z, double y)
{

double fa = (z*z*z) - (7*z*z) + (5*z) + 3;
double fb = (y*y*y) - (7*y*y) + (5*y) + 3;

}




void Bisec(float ab, float bc)
{

float fa;
float fb;

double error = .001;
float root;
float c;
if (fa*fb < 0)
{
c = (ab + bc) / 2;

if (ab - bc <= error)
{
c = root;

cout << "The root is " << root << endl;

if (fb*fa > 0)
{
bc = c;
c = root;
cout << "The root is " << root << endl;
}
else
{
ab = c;
c = root;
cout << "The root is " << root << endl;

}}}}


int main()
{
cout << "The root in the equation x^3 - 7x^2 + 5x +3 in the interval [0, 2] is " << endl;
cout << Bisec(0, 2) << endl;



}
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 02:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month