解 f(x)=x^3 -4x +2 =0 非線性方程式的解
[JAVA程式]
/* ex2-1.java is used for finding out the domains
* which exist the roots of f(x).
*/
public class Main {
double fx(double x1) {
return (Math.pow(x1, 3)-4*x1+2);
}
public static void main(String args[]){
Main fun = new Main();
double a , b ,x;
int i;
a=0.0;
b=2.0;
x=a;
System.out.printf(" i x f(x)\n");
for(i=1;;i++) {
System.out.printf("%2d %5.2f %10.5f\n",i,x,fun.fx(x));
if(x > b){
break;
}
x+=0.1;
}
}
}
輸出畫面
$javac Main.java $java -Xmx128M -Xms16M Main i x f(x) 1 0.00 2.00000 2 0.10 1.60100 3 0.20 1.20800 4 0.30 0.82700 5 0.40 0.46400 6 0.50 0.12500 7 0.60 -0.18400 8 0.70 -0.45700 9 0.80 -0.68800 10 0.90 -0.87100 11 1.00 -1.00000 12 1.10 -1.06900 13 1.20 -1.07200 14 1.30 -1.00300 15 1.40 -0.85600 16 1.50 -0.62500 17 1.60 -0.30400 18 1.70 0.11300 19 1.80 0.63200 20 1.90 1.25900 21 2.00 2.00000
沒有留言:
張貼留言