rj
❔ How to create a cubic function calculator that also calculates the minimum and maximum value
Console.WriteLine("Please enter a, b, c, d for the cubic function ax^3+bx^2+cx+d");
Console.WriteLine("Input the value for a");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for b");
double b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for c");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for d");
double d = Convert.ToDouble(Console.ReadLine());
double x_min, x_max;
double f1 = 3 * a; // multiply 3 by the first input
double f2 = 2 * b; // multiply 2 times by the second input
double f3 = c; // associate the value of c to f3
double discriminant = f2 * f2 - 4 * f1 * f3; // formula for cubic function (delta)
if (discriminant < 0) // if discriminant is less that 0 it will output "The function has no relative maxima or minima."
{
Console.WriteLine("The function has no relative maxima or minima.");
}
else
{
x_min = (-f2 - Math.Sqrt(discriminant)) / (2 * f1); // formula to find the minimum value of x
x_max = (-f2 + Math.Sqrt(discriminant)) / (2 * f1); // formula to find the maximum value of x
double y_min = a * x_min * x_min * x_min + b * x_min * x_min + c * x_min + d;
double y_max = a * x_max * x_max * x_max + b * x_max * x_max + c * x_max + d;
Console.WriteLine("The relative minimum of the function is: ({0}, {1})", x_min, y_min);
Console.WriteLine("The relative maximum of the function is: ({0}, {1})", x_max, y_max);
Console.WriteLine("Please enter a, b, c, d for the cubic function ax^3+bx^2+cx+d");
Console.WriteLine("Input the value for a");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for b");
double b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for c");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Input the value for d");
double d = Convert.ToDouble(Console.ReadLine());
double x_min, x_max;
double f1 = 3 * a; // multiply 3 by the first input
double f2 = 2 * b; // multiply 2 times by the second input
double f3 = c; // associate the value of c to f3
double discriminant = f2 * f2 - 4 * f1 * f3; // formula for cubic function (delta)
if (discriminant < 0) // if discriminant is less that 0 it will output "The function has no relative maxima or minima."
{
Console.WriteLine("The function has no relative maxima or minima.");
}
else
{
x_min = (-f2 - Math.Sqrt(discriminant)) / (2 * f1); // formula to find the minimum value of x
x_max = (-f2 + Math.Sqrt(discriminant)) / (2 * f1); // formula to find the maximum value of x
double y_min = a * x_min * x_min * x_min + b * x_min * x_min + c * x_min + d;
double y_max = a * x_max * x_max * x_max + b * x_max * x_max + c * x_max + d;
Console.WriteLine("The relative minimum of the function is: ({0}, {1})", x_min, y_min);
Console.WriteLine("The relative maximum of the function is: ({0}, {1})", x_max, y_max);
6 replies