I have written 3 classes (function, parabola and polynomial), which represent functions with a variable . I must to extend my function class with a method named public void plot(double xmin, double xmax), which opens a frame with the graph of determined function. the parameter xmin and xmax give the x-area, where the graph should be drawn. A labeld coordinate axis must also appear in the frame. Can you help me?
Here are my classes :
/**
* Class to respresent parabola functions
*/
public class Function{
/**
* some static constants
*/
public final static int n = 1000;
public final static double eps = 0.000000001;
public final static Function NULL=new Function(){
public double map(double x){return 0.0; }
public String toString() {return "0.0"; }
};
/**
* Defines the mapping of the function. The default mapping is not-defined
* so the return value is Double.NaN for instances of this class. Subclass
* should override this method to define the mapping
* @param x
* @return
*/
public double map(double x){return Double.NaN;}
/**
* Evaluates the function at each arguemt of the array
* @param x array of function values
* @return
*/
public double[] map(double[] x){
double[] y=new double[x.length];
for (int i=0; i<x.length; i++) y[i]=map(x[i]);
return y;
}
/**
* Access to the derivation of the Function instances. Subclass should
* override this method. Use Double.NaN at non-differntiable positions
* @return derivative of this function instance
*/
public Function derivate(){
return this;
}
/**
* a
* approximate the integral of this function using n boxes
* @param a stating of the integration
* @param b ending of the integration
* @param n number of boxes (equal width)
* @return approximation of the integral over [a,b] of this function
*/
public double integrate(double a, double b, int n){
double delta=(b-a)/(double)n;
double area=0;
for (double i=0; i<n; i++) area+=map(a+i*delta);
return area*delta;
}
/**
* If possible this method should return the exact integral value.
* Subclasses should override this method.
* The default is to call the approximation integrate(a,b,n).
* @param a stating of the integration
* @param b ending of the integration
* @param n number of boxes (equal width)
* @return integral over [a,b] of this function
*/
public double integrate(double a, double b){
return this.integrate(a,b,this.n);
}
/**
* Computes a zero-crossing using the newton iteration
* @param x_0 initial argument
* @return Zero-Crossing or Double.NaN
*/
public double newtonIteration(double x_0){
double d=42.0;
Function f_1=this.derivate();
for (int i=0; ((++i<this.n) && (Math.abs(d)>eps)); x_0+=d)
d=-map(x_0)/f_1.map(x_0);
return (Math.abs(d)>eps) ? Double.NaN : x_0;
}
/**
* String representation of this function
* @return "undefined"
*/
public String toString(){
return "undefined";
}
}
/**
* Class to respresent parabola functions
*
*/
public class Parabola extends Polynomial{
/**
* Constructor for a parabola object ax? + bx + c
* @param a coefficient for x^2
* @param b coefficient for x^1
* @param c coefficient for x^0
*/
public Parabola(double a, double b, double c){
super(new double[] {c,b,a});
}
/**
* Compute the Zero Crossings of the parabola
* @return zero-crossings, i.e. x with f(x)=0
*/
public double[] zeroCrossing(){
double c=map(0);
double b=map(0.5)-map(-0.5);
double a=map(1)-b-c;
//Diskriminante
double d = b*b - 4.0*c*a;
if (this.getDegree()<=1){
if (b==0) return new double[0];
else return new double[] {-c/b};
}
if (d==0) return new double[] {-0.5*b/a};
if (d<0) return new double[0];
return new double[] { 0.5*(-b-Math.sqrt(d))/a, 0.5*(-b+Math.sqrt(d))/a};
}
}
/**
* Class to respresent polynomial functions
*
*
* Bug in limPosInf() for constant functions repaired
*
*/
public class Polynomial extends Function{
// Koeffizienten des Polynoms
private double[] a=new double[0];
/**
* Constructor for a polynomial function, sum a[i] * x^i
* @param a coeffients of the polynomial function
*/
public Polynomial(double[] a){
int deg = a.length-1;
for (int i=deg; ((i>=0) & (a[i]==0.0)) ; i--) deg--;
this.a = new double[deg+1];
for (int i=0; i<=deg; i++) this.a[i]=a[i];
}
/**
* Access to the degree of the polynomial, i.e. the maximal "nonzero" power
* @return degree of the polynomial
*/
public int getDegree(){
return a.length-1;
}
/**
* Access to the derivative of the polynomial
* @return the derivative of the polynomial
*/
public Function derivate(){
if (this.getDegree()==0) return Function.NULL;
double[] b=new double[this.a.length-1];
for (int i=0; i<b.length; )
b[i]=((double)++i)*this.a[i];
return new Polynomial(b);
}
/**
* Computes the integral of the polynomial over an interval
* @param a begining of the integration interval
* @param b ending of the integration interval
* @return integral over [a,b] of polynomial
*/
public double integrate(double a, double b){
double[] a_=new double[this.a.length+1];
a_[0]=0.0;
for (int i=1; i<a_.length; i++)
a_[i]=this.a[i-1]/((double)i);
Polynomial F = new Polynomial(a_);
return F.map(b)-F.map(a);
}
/**
* function value of the polynomial at x
* @param x argument of the function
* @return value f(x) of the function
*/
public double map(double x){
double y=0.0;
for (int i=getDegree(); i>=0; i--) y=y*x+a[i];
return y;
}
/**
* computes the limit for x->oo
* @return limit for x->oo
*/
public double limPosInf(){
if (getDegree()==0) return map(0);
return (a[getDegree()]>0)
? Double.POSITIVE_INFINITY
: Double.NEGATIVE_INFINITY;
}
/**
* computes the limit for x->-oo
* @return limit for x->-oo
*/
public double limNegInf(){
return (getDegree()%2==0)
? limPosInf()
: -limPosInf();
}
/** String representation
* @return awesome string representation
*/
public String toString(){
//sorry for this bad code ;)
String s="";
String vz="";
for (int i=0; i<a.length; i++)
if (a[i]!=0) {
s=((i==0) ? a[i] : (a[i]==1) ? "" : (a[i]==-1) ? "-" : ""+a[i])
+((i==0) ? "" :(i==1) ? "x" : "x^"+i)//aktuelle Potenz
+vz //zusätzliches Vorzeichen
+s; //Kleinere Potenzen
//Berechnung des aktuellen Vorzeichens
if (a[i]>0 ) vz="+"; else vz="";
}
return s;
}
}


Sign In
Create Account

Back to top









