Help! I'm trying to solve a quadratic equation

I keep getting the error :

term does not evaluate to a function taking 1 arguments


Heres the program. I highlighted the problem line

// Macros 3 - finding the root of a quadratic equation
// ax^2 + bx + c  --> positive root = (-b+ (sqrt(b^2 - 4*a*c))) / 2a
// This program still has 2 flaws: it only solves one of the two roots of a quadratic equation
// and if the c part of the equation is 0 it will not solve the input quadratic

#include <iostream>
using namespace std;

double Abs(double Nbr);
double SquareRoot(double Nbr);
double Subfunction(double a, double b, double c);
inline double ROOT(double a, double b, double Nmbr) {return ((-b + Nmbr)/(2*a));}

// solves the (b^2 - 4*a*c) part of the equation to be put into square root solving functions
double Subfunction(double a, double b, double c)
{
double SubfunctionResult;
SubfunctionResult = ((b*b)-4(a)(c));
return SubfunctionResult;
}
// This is part of a function for solving a square root.
// It works with SquareRoot()
double Abs(double Nbr)
{
if( Nbr >= 0 )
return Nbr;
else
return -Nbr;
}

// This is part of a function for solving a square root.
// It works with Abs()
double SquareRoot(double Nbr)
{
double Number = Nbr / 2;
const double Tolerance = 1.0e-7;

do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);

return Number;
}

int main()

{

double x=1;
double y=1;
double z=1;
double i;

for (;;)
{
cout << "Enter the three parts to a quadratic equation in the form a^2 + b + c (0 to quit): ";
cin >> x;
cin >> y;
cin >> z;
if (x || y || z==0)
break;
cout << "You entered: " << x;
cout << "You entered: " << y;
cout << "You entered: " << z << endl;

double SubfunctionResult1 = Subfunction(x,y,z);
double Number = SubfunctionResult1;
double Number1 = SquareRoot(Number);

cout << "ROOT(" << x << " " << y << " " << z << " ): ";
cout << ROOT(x,y,Number1) << "." << endl;

}
return 0;
}


Edited by pyTony: fixed formatting

4
Contributors
4
Replies
7
Views
9 Years
Discussion Span
Last Post by benji_k

This is programming, not algebra. You must show the multiplication operator, where you can omit it in mathematical expression.

SubfunctionResult = ((b*b)-4(a)(c));

must be

SubfunctionResult = ( b*b ) - (4 *a *c );

(All the parenthesis are actually not needed, but can add clarity for some.)

Also, in future posts, please put your code inside code tags, like:

[code]

[/code]

And, when referring to error messages, try to indicate which line in the code they refer to.

I am going to comment on the maths/computing interface.

You have two problems here, first is that although mathematically correct, the formula $$\frac{-b\pm\sqrt{b^2 - 4ac}}{ 2a}$$ is is awful as a computational base, since, as you partially discovered, if a or c are very small you get junk.

So you set
q=-\frac{1}{2}(b+sgn(b)\sqrt{b^2-4ac} )
and the roots are q/a and c/q. (sgn is the 1 if b>0 and -1 if b<0)

Note: additionally you need to check the condition that
b^2-4ac\ge0 or do the calculation in the complex plane.

Additionally, this still holds if the coefficients (a,b,c) are themselves complex or quaternions. (the condition is different).

There are many numerical discussions in scientific literature on
(a) approximating this (to speed up the calc at the cost of accuracy)

(b) the issue of machine accuracy. google scholar is helpful, but a membership of the ACM digital library is also going to help [and it is expensive].

(c) Numerical Recipes for X, have a discussion in chapter 5. http://www.nrbook.com/a/bookcpdf.php Note: I have given the link to the free to download pdf of the 2nd edition. You pay money for the third editions and NEVER put NR code in production code, as it is rarely robust.

p.s. I am not a proper mathematician. So if you talk to a REAL mathematician he/she will complain at my imprecision.

Edited by Dani: Formatting fixed