C
C#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);
R
rj396d ago
the program does what i ask, however i need it to keep going even if the discriminant is < 0
J
juustin396d 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?
R
rj396d ago
the program needs to just run all the code in the else-statement?
A
Anton396d ago
C# doesn't have built-in support for complex numbers. Find a math library that adds them
A
Accord395d 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
More Posts
❔ how to listen to the output of a virtual audio device on windows using C#?how to listen to the output of a virtual audio device on windows using C# to save the raw audio stre❔ How to wait for process to completei want to make program opens async but instead of giving 'hard coded' delays but i don't know how to❔ Problem with including C DLL in a C# console app project.So I have this C DLL imported into my console app (.NET 5). I simply cannot get to call it correctly✅ Win Form Event Handlers questionHi, I was just wondering if there is an event handler when the win form is idle that I can then run ❔ Problem with Dictionary<ulong, Image> (Strange work of memory access)Concept of my system: A system that draws a picture with stickers, each of the stickers can be moved❔ How do I refactor this?I am working with a grid of tiles, each tile has an int, which is made up of 4 bytes which represent❔ [AvaloniaUI] Visibility of item in ListView based on conditionAn Avalonia UI application is used to manage an evidence of items. It allows the user to add items. ✅ How to structure this login/signup page layoutSo I have this mockup of the layout I want for a login/signup page in my Blazor WASM app. Pretty sta❔ I am need of help before I give up and run my code on a server instead of a serverless solution.I have create an azure function locally and i've used the selenuim webdriver package for taking scre✅ .Net Core 6 Asymmetric Encryption with custom public and private keysHello all! How can i use the `Asymmetric Encryption` in .Net 6 but choosing/importing private and pu❔ No Design in C#Instances of this bug (1) 1. View Call Stack at System.Runtime.InteropServices.Marshal.ThrowExce❔ having issues on a reloading script on unity to reload weaponsthe code is used in a youtube video and i have pretty much copied to to get it to work but it doesnt