Created
November 29, 2014 21:18
-
-
Save jy95/1b5d81601dcdb9cb0487 to your computer and use it in GitHub Desktop.
Polynome.java (à tester pour voir si c'est correct)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Polynome { | |
private double[] coefficients; | |
private int degre; | |
/** | |
* Le constructeur construit le polynôme canonique du degré passé en | |
* paramètre (x^degré) | |
*/ | |
public Polynome(int degre) throws ArgumentInvalideException { | |
if (degre < 0)throw new ArgumentInvalideException("mauvais"); | |
coefficients = new double[degre+1]; | |
coefficients[degre] = 1; | |
this.degre = degre; | |
} | |
// Le constructeur construit le polynôme nul | |
public Polynome() { | |
coefficients = new double[1]; | |
coefficients[0] = 0; | |
} | |
/** | |
* Cette méthode remplace le coefficient de degré passé en paramètre par le | |
* coefficient passé en paramètre. Cette méthode ne permet pas de changement | |
* de degré du polynôme | |
*/ | |
public void setCoefficient(int degre, double coefficient) | |
throws ArgumentInvalideException { | |
if ( degre == 0 && coefficients.length == 1) | |
this.coefficients[degre] = coefficient; | |
if ( degre > coefficients.length-1 && coefficients.length-1 > 1 || degre == this.degre && coefficient == 0 && coefficients.length > 1 || degre < 0)throw new ArgumentInvalideException("mauvais"); | |
this.coefficients[degre] = coefficient; | |
} | |
/** | |
* Cette méthode calcule et renvoie la valeur que prend le polynôme au point | |
* x | |
*/ | |
public double evalueEn(double x) { | |
double result = 0; | |
for (int i = 0 ; i < coefficients.length ; i++) | |
result += coefficients[i] * Math.pow(x,i); | |
return result; | |
} | |
/** | |
* Cette méthode calcule et renvoie le signe de la valeur que prend le | |
* polynôme au point x | |
*/ | |
public int signeEn(double x) { | |
int result = 0; | |
if (evalueEn(x) < 0) | |
result = -1; | |
if (evalueEn(x) > 0) | |
result = 1; | |
return result; | |
} | |
public String toString() { | |
if (degre == 0) | |
return "" + this.coefficients[0]; | |
String result = "" + this.coefficients[degre] + " x^" + degre; | |
for (int i = degre - 1; i > 0; i--) { | |
if (this.coefficients[i] < 0) { | |
result += "-" + Math.abs(this.coefficients[i]) + " x^" + i; | |
} | |
else { | |
if (this.coefficients[i] > 0) { | |
result += " + " + this.coefficients[i] + " x^" + i; | |
} | |
} | |
} | |
if (this.coefficients[0] != 0) | |
result += " " + this.coefficients[0]; | |
return result; | |
} | |
/** | |
* Cette méthode calcule et renvoie l'approximation de la racine obtenue en | |
* appliquant la méthode dichotomique sur l'intervalle [a,b] (ou [b,a]). Le | |
* paramètre maxEtape précise le nombre maximal d'étapes qu'on veut | |
* effectuer et tol la tolérance voulue. | |
*/ | |
public double racineParBissection(double a, double b, int maxEtape, | |
double tol) throws NumeriqueException { | |
double c = a; | |
double d = b; | |
if (b < a) { | |
c = b; | |
d = a; | |
} | |
int signeFA = signeEn(c); | |
int signeFB = signeEn(d); | |
if (signeFA * signeFB > 0) throw new NumeriqueException("pas de racine"); | |
for (int i = 1 ; i < maxEtape ; i++) { | |
double p = c + (d-c)/2; | |
int signeFP = signeEn(p); | |
if (signeFA * signeFB == 0) { | |
if (signeFA == 0 ) | |
return c; | |
if (signeFB == 0 ) | |
return d; | |
} | |
if (signeFP == 0 || (d-c)/2 < tol) | |
return p; | |
if (signeFA * signeFP > 0) { | |
c = p; | |
} | |
else{ | |
d = p; | |
} | |
} | |
throw new NumeriqueException("trop d’itérations"); | |
} | |
public int getDegre() { | |
return degre; | |
} | |
public Polynome polynomeDerive() { | |
Polynome nouveau = this; | |
for (int i=0; i < nouveau.coefficients.length; i++) | |
{ | |
nouveau.coefficients[i] = this.coefficients[i+1]* (i+1); | |
} | |
return nouveau; | |
} | |
public double racineCombine(double a, double b) { | |
// faire 20 * la methode de la bissection ( la méthode racineParBissection2) | |
// faire 20 * la méthode de Newton (ci- dessous) | |
double pn = racineParBissection2(a,b,20); | |
for (int i = 0 ; i < 20 ; i++) { | |
double pn2 = pn - ( evalueEn(pn) / polynomeDerive().evalueEn(pn)); | |
if (pn2 == 0) | |
return pn; | |
pn = pn2; | |
} | |
return pn; | |
} | |
public double racineParBissection2(double a, double b, int maxEtape | |
) { | |
double c = a; | |
double d = b; | |
if (b < a) { | |
c = b; | |
d = a; | |
} | |
int signeFA = signeEn(c); | |
int signeFB = signeEn(d); | |
double p = c + (d-c)/2; | |
for (int i = 0 ; i < maxEtape ; i++) { | |
int signeFP = signeEn(p); | |
if (signeFA * signeFB == 0) { | |
if (signeFA == 0 ) | |
return c; | |
if (signeFB == 0 ) | |
return d; | |
} | |
if (signeFP == 0 || i == maxEtape-1) | |
return p; | |
if (signeFA * signeFP > 0) { | |
c = p; | |
} | |
else{ | |
d = p; | |
} | |
} | |
return p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment