I have homework to create a Quadratic formula class. I have the parts figured out for the roots and the Discriminate. However, the last part I'm having problems with :
**must be able to compute the value of the first derivative of the quadratic at any specific point.
Unfortunately I don't even know what that means, it has been decades since I've taken a calculus course. Here is my code so far.
import static java.lang.Math.*;//math.pow
public class Quadratic
{
//instance variables
private double a;
private double b;
private double c;
private double discriminant = b * b - 4 * a * c;
//constructors
//default constructor
public Quadratic ()
{
//just put default numbers in y(x) = x^2 + x + 1
a = 1;
b = 1;
c = 1;
}
//constructor for abc
public Quadratic(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
}
//users should be able to change / alter - gets and sets ...
///////////
//SETTERS//
///////////
//set a
public void setValue_a(double a)
{
this.a = a;
}
//set b
public void setValue_b(double b)
{
this.b = b;
}
//set c
public void setValue_c(double c)
{
this.c = c;
}
//set a b and c
public void setValue(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
}
///////////
//GETTERS//
///////////
//return a
public double get_a()
{
return a;
}
//return b
public double get_b()
{
return b;
}
//return c
public double get_c()
{
return c;
}
public double getDescrim()
{
return (b * b - 4 * a * c);
}
//Returns a String detailing whether there are complex or real roots
public String isReal()
{
if (discriminant >= 1)
{
return "There are real roots.";
}
else
{
return "There are complex roots";
}
}
//Is the descriminant negative
public String isNegative()
{
if (discriminant < 0)
{
return "The descriminant is negative";
}
else
{
return "The descriminant is positive";
}
}
public double getRootX()
{
return (-b + Math.sqrt(b*b - 4*a*c)) / (2 * a);
}
public double getRootY()
{
return (-b - Math.sqrt(b*b - 4*a*c))/ (2*a);
}
//roots = (-b +- sqrt(b^2 - 4ac))/2a
public double returnRootsX()
{
System.out.println(-b);
return (-b + Math.sqrt(b*b - 4*a*c)) / (2 * a);
}
public double returnRootsY()
{
return (-b - Math.sqrt(b*b - 4*a*c))/ (2*a);
}
//toString...Print to String;
public String toString()
{
String str = "The Quadratic Formula Data:\na: " + a + "\nb: " + b + "\nc: " + c + "\n" + isReal() + "\nRoot 1: " + getRootX() + "\nRoot 2: " + getRootY()+
"\n" + isNegative() + "\n" + getDescrim();
return str;
}
}//end class
Thank you SO MUCH for helping. Rachel
To calculate the derivative at any point You need this formula:
f'(x) = (f(x+d)-f(x))/d
Where d
should be very small but not zero. This is numerical derivation.
Other way is to use the general formula for derivative of f(x)=ax^2+bx+c
The derivative at any x
is:
f'(x) = 2ax+b
Derivative is rate of increase of function at some point, have some fun with it :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.