前两天需要解个高次复杂的函数方程 要精确到6位小数
用指定的first guess & 用牛顿定理要try n久 -- Newton method: Xn+1 = Xn - f(Xn)/(dy/dx|x=Xn)
没办法写个了简易的程序--非GUI界面 (1。用swing或者applet画个函数图像从图像上得到值显得没这个必要 2。要短时间内列出所有牛顿定理try的过程)
比如要求4X^3-42X^2-19X=28的根:
/**
* Note: the following solution calculates using full digits of a double number
*/
import java.text.DecimalFormat;
public class NewTonCA
{
public static void main(String [] args)
{
String s = args[0];
double d = Double.parseDouble(s),m=d;
double f;
DecimalFormat df = new DecimalFormat("#.000000"); //set num of decimal digits
while(!df.format(m).equals(df.format(m-((4*Math.pow(m,3)-42*Math.pow(m,2)-19*m-28)/(12*Math.pow(m,2)-84*m-19))))) //only compare first 6 decimal digits
{
f = m-((4*Math.pow(m,3)-42*Math.pow(m,2)-19*m-28)/(12*Math.pow(m,2)-84*m-19)); //Newton method
m = f;
System.out.println(m+":-----------:"+f);} //output to screen with current Xn and value of Xn+1
}
}