Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C

Equation Solver in C

Rate me:
Please Sign up or sign in to vote.
4.67/5 (15 votes)
1 Oct 2009CPOL3 min read 129.2K   2.3K   16   14
Solves linear equation in one variable, 2 linear equations in 2 variables, quadratic equations and cubic equations
Image 1

Introduction

This program can solve 4 types of equations.

Background

I'm a ninth grader from India. I was too bored to solve equations and check them all by myself. So I wrote this program.

Knowledge of the quadratic formula (including imaginary roots) and synthetic division will help a lot.

Using the Code

The method for solving linear equations in one variable is quite simple.

ax+b = 0 is the format for one variable equations. The variables a and b are determined and x is computed by -b/a.

The method I have implemented for solving linear equations in two variables is a formula which can be derived by operating on both sets of the equation. The formula for the equations is:

ax + by + c = 0  
dx + ey + f = 0  
x = (fb-ce)/(ae-db) 
y = (cd -fa)/(ae-db) 

To solve quadratic equations, we use the quadratic formula:

Root 1 = (-b + Sqrt(b2 - 4ac))/2a  
Root 2 = (-b - Sqrt(b2 - 4ac))/2a   

b<sup>2</sup>-4ac is called the "Discriminant" which is generally denoted by D.

If D >= 0, we will get Real roots.

Things get complicated when we get imaginary roots.

If D<0, then we get imaginary roots and the roots will change as follows:

Root 1 = (-b + Sqrt(4ac - b<sup>2</sup>)i)/2a   
Root 2 = (-b + Sqrt(4ac - b2)i)/2a

C does not support complex numbers. So, I decided to print the root as a string:

C++
printf("\nRoot 1 : %f+%fi",((-b)/(2*a)),disc);

The first %f in the roots is -b/2a which is the real part of the root and the second %f prints 4ac-b<sup>2</sup> with 'i' representing the imaginary part.

I don't know a method to solve all sorts of cubic equations. If anyone gets to know of a method, please inform me of the method in a language a ninth grader like me can understand.

C++
float a,b,c,d,x1,x2,x3,disc;
int i;
float expr;
clrscr();
printf("ax^3 + bx^2 + cx + d = 0\n Enter a,b,c,d : \n");
scanf("%f,%f,%f,%f",&a,&b,&c,&d);
i = 0;
while(i<abs(d))
{
expr = a*pow(i,3)+b*pow(i,2)+c*i+d;
if(expr==0)
{
x1=i;
break;
}
expr = a*pow(-i,3)+b*pow(-i,2)+c*(-i)+d;
if(expr==0)
{
x1=-i;
break;
}
i++;
}
printf("Root 1 = %f",x1);
b = b + (a*(x1));
c = c + (b*(x1));
disc = (b*b)-(4*a*c);
if(disc>=0)
{
x2 = (-b+sqrt(disc))/(2*a);
x3 = (-b-sqrt(disc))/(2*a);
printf("\nRoot 2 = %f\nRoot 3 = %f",x2,x3);
}
else
{
disc = ((4*a*c)-pow(b,2))/(2*a);
printf("\nRoot 2 : %f+%fi",((-b)/(2*a)),disc);
printf("\nRoot 3 : %f-%fi",((-b)/(2*a)),disc);
}	

One root is found by trial and error method. If this fails, we cannot find any other root of the expression in this program. This is the method which I have implemented in the program. All the numbers (both positive and negative) till d are checked by substitution. If it succeeds, then the program moves to the next step.

C++
b = b + (a*(x1));
c = c + (b*(x1));  

This is the place where synthetic division comes into play.

Synthetic division is a shortcut method for dividing a polynomial by a linear polynomial instead of using the long division method.

I will explain the process with an example.

Let the polynomial be (x<sup>3</sup> + 2x<sup>2</sup> - 4x + 8) and the linear polynomial (x + 2).

We have to divide them.

  1. Reverse the sign of the constant in the divisor. In this case, we have to make 2 into -2.
  2. Then write the co-efficients a, b, c and d in order. It will look like this: -2|1 2 -4 8
  3. Bring down the first co-efficient and multiply it by the divisor. Then add this to the second co-efficient. 2+(-2*1) = 0. The general form is b<sub>new</sub> = b+(divisor * a).
  4. Then multiply the divisor again by the obtained result and add with the next co-efficient. -4+(-2*0) = -4. The general form is c<sub>new</sub>= c+(divisor * b<sub>new</sub>).
  5. Again multiply the divisor by the result obtained and add this to the next (last in this case) co-efficient. 8+ (-2*-4) =16.

In this example, we get the remainder as 16. If the polynomial is divided by one of its roots, we will get zero as the remainder in the last step. Then, the polynomial will be reduced to a quadratic equation of the form Ax<sup>2</sup>+Bx+C=0 where A is a, B is b<sub>new</sub> and C is c<sub>new</sub>.

Then the quadratic equation is solved.

Now, we have all 3 roots of the equation.

Any suggestions or improvements are welcome. Please inform me if there is another fool-proof method for solving 3rd degree equations. Methods suggested for solving 4th degree and higher degree equations are welcome with open hands.

History

  • 1st October, 2009: Initial post

Contact

You can either post a comment or mail me at r.anshul@gmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to Make graph of a function of one variable equation Pin
fatih kaya 10717-Jun-13 2:25
fatih kaya 10717-Jun-13 2:25 
AnswerRe: How to Make graph of a function of one variable equation Pin
Anshul R7-Jun-13 16:14
Anshul R7-Jun-13 16:14 
Questionoh hi Pin
NoPoinT ba DA10-Feb-13 0:36
NoPoinT ba DA10-Feb-13 0:36 
AnswerRe: oh hi Pin
Anshul R10-Feb-13 0:59
Anshul R10-Feb-13 0:59 
GeneralCubic Equations Pin
Michael Waters7-Oct-09 9:16
Michael Waters7-Oct-09 9:16 
GeneralRe: Cubic Equations Pin
Anshul R7-Oct-09 15:10
Anshul R7-Oct-09 15:10 
GeneralRe: Cubic Equations Pin
Michael Waters8-Oct-09 4:34
Michael Waters8-Oct-09 4:34 
GeneralRe: Cubic Equations Pin
Anshul R8-Oct-09 4:37
Anshul R8-Oct-09 4:37 
GeneralProblem with Quadatic Answers Pin
MicroImaging5-Oct-09 10:48
MicroImaging5-Oct-09 10:48 
GeneralRe: Problem with Quadatic Answers Pin
Anshul R6-Oct-09 2:21
Anshul R6-Oct-09 2:21 
GeneralNice Article Pin
stoni2-Oct-09 3:48
stoni2-Oct-09 3:48 
GeneralRe: Nice Article Pin
Anshul R2-Oct-09 19:34
Anshul R2-Oct-09 19:34 
GeneralGood Job! Pin
User 16732521-Oct-09 19:51
User 16732521-Oct-09 19:51 
GeneralGood job Pin
Marius Iulian Mihailescu1-Oct-09 19:08
professionalMarius Iulian Mihailescu1-Oct-09 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.