很简单吧:
double x = 1.5f;
while((Math.pow(x, 3) - x - 1) >= 0.0001)//不可能等于0的,所以要设置允许误差
x = Math.pow(x + 1, 1f / 3);
System.out.println(x);
double x = 1.5f;
while((Math.pow(x, 3) - x - 1) >= 0.0001)//不可能等于0的,所以要设置允许误差
x = Math.pow(x + 1, 1f / 3);
System.out.println(x);
X(k+1) = (X(k) + 1) ^ 1/3 次方吗?这个就是迭代啊
public class WORK{
static double k;
public static void invoke(double i){
double j=i;
i=Math.cbrt(i+1);
if(j==i) k=i;
else{
invoke(i);
}
}
public static void main(String a[]){
invoke(1.5);
System.out.println(k);
}
}
Minute value
Intervaloutput:Root valueExample:x^3 - 2*x^2 - x + 2 = 0 (Root = -1, 1, 2)Calculation is made with the starting point being 0, the minute value being 10^(-4) and the interval being 0.5. b = ((x-2)*x-1)x+2Contents (Formula)x(n+1) = x(n) - f(x(n))/f'(x(n))When the absolute value of the difference between x(n) and x(n+1) become less than
10^(-8), x(n) is displayed as a root. The differential f'(x) is defined follows:f'(x) = (f(x+h)-f(x))/h (h: minute value)
f(X) = X*X*X-X-1;
用牛顿迭代法做:X(k+1)=X(k)-f(X(k))/f'(X(k))程序清单:
public class NewDun
{
public final static double precise = 0.0000000001;
public static void main(String args[])
{
double x0 = 1.5;
double temp = 5.0;
double x1 = 0.0;
while(temp > precise)
{
x1 = x0 - (x0*x0*x0-x0-1)/(3*x0*x0-1);
temp = Math.abs(x1-x0);
x0 = x1; }
System.out.println(x1);
System.out.println(x1*x1*x1-x1-1);
}
}
* RootOfAnEquation.java
*
* Created on 2005年10月11日, 下午4:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package javaapplication2;/**
*
* @author Trumplet
*/
public class RootOfAnEquation {
/** Creates a new instance of RootOfAnEquation */
public RootOfAnEquation() {
}
static double Calculate(double x){
double x1 = x*x*x - x - 1; //在这里写方程的表达式
return x1;
}
static double diff (double x, double h){
double d = (Calculate(x+h)-Calculate(x))/h;
return d;
}
public static void main(String[] args){
double start = 1.5; // 初值
double interval = 0.1; // 增量
double xn=start;
while (Math.abs(Calculate(xn))>0.00000001){
xn = xn - Calculate(xn)/(diff(xn,interval));
System.out.println(xn);
}
}
}运行结果:1.3590982286634463
1.3285775499587462
1.3250658031576152
1.3247483823824004
1.3247206111042094
1.3247181886739026
1.3247179774260416
1.3247179590046105
BUILD SUCCESSFUL (total time: 2 seconds)
---------------------------
是这个哪个意思吗?
x(k+1) = x(k) ^ (1/3) + 1或者
x(k+1) = (x(k)+1) ^ (1/3)