Max/Min of a quadratic equation

Hey guys, I was just wondering is there any way to use c++ to find the max/min of a quadratic equation?
Yea, but I wasn't talking about the roots. I was talking about the min and max. In other words, the point on the equation with the max value of y, and the min value of y.
In math you differentiate the equation and set the answer to zero, to find the min or max. I wanted to know if there is something similar that can be done in c++

mmmm i wright this for my h.w look out the output ^_^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
using namespace std;

int main()

{
	double value,min,max,avg,sum;
	int count;
	int Degree;

	count=0;
	min=0.0;
	max=0.0;
	sum=0.0;


	cout<<"put your value ^_^ ,if you want stope put -1 like a value please "<<endl;
	cin>>value;
	
	
		
		while(value!=-1)//if you want stope put -1 like a value please
		
		{	

			sum +=value;
		count++;

			if(value>max)

				max=value;

			else if(value<max)

				min=value;

		
			cin>>value;
		}

		
	
	
	cout<<" the max value is "<<max<<endl;
	cout<<"the min value is "<<min<<endl;
	cout<<"the sum is "<<sum<<endl;
	cout<<"the count is "<<count<<endl;

	avg=sum/count;

	cout<<"the avg is "<<avg<<endl;


	cout<<"enter your Degree please "<<endl;
	cin>>Degree;

if(Degree>=avg)

	cout<<"pass"<<endl;

else
	cout<<"Failed"<<endl;



	system("pause");
	

	return 0;

}





I started out posting regarding the roots (which is a different topic).
What you need is to find where the slope of the curve is zero. I suppose you could input the coefficients a,b and c, then follow some simple rules to get the derivative.

f(x) = a*x^2 + b*x + c

differentiate
f'(x) = 2*a*x + b

Max or min when 2*a*x + b = 0
Hence
x = -b/ (2 * a)
Then find the value of the original function f(x) for that value of x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class quadratic {
    double a, b, c;
public:
    quadratic(double A, double B, double C) : a(A) , b(B), c(C) {};
    double fx(double x) { return a*x*x + b*x + c; }
    double maxminX()    { return -b / (2.0 * a); }
    double maxmin()     { return fx(maxminX()); }
};

int main()
{
    quadratic equation(-2, 36, -177);
    std::cout << "max or min at x = " << equation.maxminX() << std::endl;
    std::cout << "max or min is  " << equation.maxmin() << std::endl;
}


Output:
max or min at x = 9
max or min is  -15

Last edited on
C++ doesn't do calculus for you, but as Chervil says, you can write a program in C++ to do calculus.

You can also pick up a library, this seems promissing:
http://www.gnu.org/software/gsl/
Last edited on
Thanks guys, any other thoughts on this topic?
That's an open-ended question. Can you be more specific please?
I suppose you could input the coefficients.. http://www.hqew.net
Topic archived. No new replies allowed.