用java求函数的一阶导数及偏导
public class Test4 {
private static final double DELTA = 0.00000000000000000001;
public static void main(String[] args) {
DerivedFunction derivedFunction = new DerivedFunction(new Function());
System.out.println(derivedFunction.get(2));
System.out.println(derivedFunction.getY(2, 2));
}
public static class DerivedFunction {
public Function function;
public DerivedFunction(Function function) {
this.function =function;
}
public double get(double x) {
return (Function.f(x + DELTA) - Function.f(x)) /DELTA;
}
public double getX(double x,double y){
return (Function.f1(x+DELTA,y) - Function.f1(x,y)) /DELTA;
}
public double getY(double x,double y){
return (Function.f1(x,y+DELTA) - Function.f1(x,y)) /DELTA;
}
}
public static class Function {
public Double x;
public Double y;
public static double f(double x) {
return x*x;
}
public static double f1(double x,double y){
return x*x+y*y*y;
}
}
}