C
C#2y ago
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);
5 Replies
rj
rjOP2y ago
the program does what i ask, however i need it to keep going even if the discriminant is < 0
juustin
juustin2y ago
Do you mean that if the discriminant < 0 the program need to ask the user again for input or the program needs to just run all the code in the else-statement?
rj
rjOP2y ago
the program needs to just run all the code in the else-statement?
Anton
Anton2y ago
C# doesn't have built-in support for complex numbers. Find a math library that adds them
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server