Public feed #377702

Polynom

public java v1 · immutable
Raw Edit
·published 2007-03-01 23:10 UTC
rendered paste body
public class Polynom {	private double[] poly;	public Polynom(double[] angabe ) { this.poly = (double[]) angabe.clone(); }	//Hornerschema	public double evaluate(double x) {		if (poly.length == 1) { return poly[0]; }		else {			if (poly.length == 0) {return 0; }			double res = x * poly[poly.length - 1] + poly[poly.length - 2];			int plength = poly.length - 2;			while (plength > 0) {				plength--;				res = x * res + poly[plength];			}			return res;		}	}		//Ableitung	public Polynom calculateDerivative() {		int pnewlength = poly.length - 1;		double[] npoly = new double[pnewlength];		for (int i = 0; i < pnewlength; i++) {			npoly[i] = poly[i + 1] * (i+1);		}		Polynom polyder = new Polynom(npoly);		return polyder;	}		//Newton		public double newton(double x0) {			if( calculateDerivative().evaluate(x0) == 0 ) {return Double.NaN; }			double res = x0 - ( evaluate(x0) / calculateDerivative().evaluate(x0));			if (Math.abs(x0 - res) <= Math.abs(x0) * 10E-12) {return res;}			return newton(res);	}}