解:package shuzhifenxi;
public class Exam411 {
public static Boolean th = true;
public static int count = 1; // 统计迭代的次数
public static void main(String[] args) {
System.out.println("x=" + dichotomy(0, 1));
System.out.println("达到精度需要迭代的步数count=" + count);
}
public static double dichotomy(double lowLimit, double upLimit) {
double x = 0.0;
while (th) {
x = (lowLimit + upLimit) / 2; // 此处迭代一次,所以count=1
if (question(x) < 0) {
upLimit = x;
count++;
}
if (question(x) > 0) {
lowLimit = x;
count++;
}
if (Math.abs(x - (lowLimit + upLimit) / 2) < Math.pow(10, -4)) {
th = false;
}
}
return (lowLimit + upLimit) / 2;
}
public static double question(double x) {
double S = solveEquation(x) * solveEquation(0);
return S;
}
public static double solveEquation(double x) {
double S = 1 - x - Math.sin(x);
return S;
}
}
运行结果:x=0.51092529296875
达到精度需要迭代的步数count=14
解:
package shuzhifenxi;
public class Exam462 {
// public static int count = 0;
public static double x = 0;
public static double x1 = 0;
public static void main(String[] args) {
System.out.println("第一个函数迭代值");
iteration1(1.5, 5);
System.out.println("第二个函数迭代值");
iteration2(1.5, 5);
System.out.println("第三个函数迭代值");
iteration3(1.5, 5);
System.out.println("第四个函数迭代值");
iteration4(1.5, 5);
}
private static void iteration1(double d, int n) {
boolean th = true;
int count = 0;
while (th == true) {
x = 1 + 1.0 / Math.pow(d, 2);
count++;
System.out.println("迭代" + count + "次后" + "x1" + count + "=" + x);
//满足精度要求后将false赋值给th,中断循环
if (Math.abs(x - d) < Math.pow(10, -n)) {
th = false;
System.out.println("迭代" + count + "次后" + "S=" + d);
}
d = x; //若没有达到精度要求,继续循环
}
}
private static void iteration2(double d, int n) {
boolean th = true;
int count = 0;
while (th == true) {
x = Math.pow((1 + Math.pow(d, 2)), 1.0 / 3);
count++;
System.out.println("迭代" + count + "次后" + "x2" + count + "=" + x);
if (Math.abs(x - d) < Math.pow(10, -n)) {
th = false;
System.out.println("迭代" + count + "次后" + "S=" + d);
}
d = x;
}
}
private static void iteration3(double d, int n) {
boolean th = true;
int count = 0;
while (th == true) {
x = Math.pow((Math.pow(d, 3) - 1), 1.0 / 2);
count++;
System.out.println("迭代" + count + "次后" + "x3" + count + "=" + x);
if (Math.abs(x - d) < Math.pow(10, -n)) {
th = false;
System.out.println("迭代" + count + "次后" + "S1=" + d);
}
d = x;
//由自己的调试知,此函数迭代形式发散,所以在此处当迭代20次后,将false赋值给th,中断循环
if (count == 20) {
th = false;
}
}
}
private static void iteration4(double d, int n) {
boolean th = true;
int count = 0;
while (th == true) {
if (d - 1 < 0) {
th = false;
}
x = 1.0 / Math.pow((d - 1), 1.0 / 2);
count++;
System.out.println("迭代" + count + "次后" + "x4" + count + "=" + x);
if (Math.abs(x - d) < Math.pow(10, -n)) {
th = false;
System.out.println("迭代" + count + "次后" + "S1=" + d);
}
d = x;
//由自己的调试知,此函数迭代形式发散,所以在此处当迭代20次后,将false赋值给th,中断循环
if (count == 20) {
th = false;
}
}
}
}
运行结果:
第一个函数迭代值
迭代1次后x11=1.4444444444444444
迭代2次后x12=1.4792899408284024
迭代3次后x13=1.456976
迭代4次后x14=1.4710805833200253
迭代5次后x15=1.4620905354712408
迭代6次后x16=1.4677905760195855
迭代7次后x17=1.464164380462178
迭代8次后x18=1.4664663557170745
迭代9次后x19=1.465003040566855
迭代10次后x110=1.4659324390818347
迭代11次后x111=1.4653418257177924
迭代12次后x112=1.4657170180224512
迭代13次后x113=1.4654786212853665
迭代14次后x114=1.465630077074339
迭代15次后x115=1.4655338471620807
迭代16次后x116=1.4655949849544627
迭代17次后x117=1.4655561408589337
迭代18次后x118=1.4655808200184175
迭代19次后x119=1.4655651401645184
迭代20次后x120=1.4655751022361165
迭代20次后S=1.4655651401645184
第二个函数迭代值
迭代1次后x21=1.4812480342036851
迭代2次后x22=1.4727057296393942
迭代3次后x23=1.4688173136644993
迭代4次后x24=1.4670479732005974
迭代5次后x25=1.466243010114747
迭代6次后x26=1.4658768201688133
迭代7次后x27=1.465710240775865
迭代8次后x28=1.4656344652385098
迭代9次后x29=1.4655999958533155
迭代10次后x210=1.4655843161956508
迭代11次后x211=1.4655771837422105
迭代11次后S=1.4655843161956508
第三个函数迭代值
迭代1次后x31=1.541103500742244
迭代2次后x32=1.630987680598118
迭代3次后x33=1.8271902684081118
迭代4次后x34=2.2583847742893766
迭代5次后x35=3.243215056695436
迭代6次后x36=5.754439646254118
迭代7次后x37=13.767718493118355
迭代8次后x38=51.075160681190056
迭代9次后x39=365.016908562469
迭代10次后x310=6973.799699071487
迭代11次后x311=582376.9855929272
迭代12次后x312=4.4443300777032775E8
迭代13次后x313=9.36934992862004E12
迭代14次后x314=2.8679012223627768E19
迭代15次后x315=1.5358410937367452E29
迭代16次后x316=6.0189318417969335E43
迭代17次后x317=4.669594170406319E65
迭代18次后x318=3.1909405073782005E98
迭代19次后x319=5.7000420662841476E147
迭代20次后x320=Infinity
第四个函数迭代值
迭代1次后x41=1.414213562373095
迭代2次后x42=1.5537739740300376
迭代3次后x43=1.3437971925310592
迭代4次后x44=1.7054886638419886
迭代5次后x45=1.1905701238185404
迭代6次后x46=2.290723081999706
迭代7次后x47=0.8802042510207922
迭代8次后x48=NaN
由运行结果知四种不同的等价形式中(1),(2)收敛,且(2)的收敛效果比(1)好,(3),(4)不收敛。
解:package shuzhifenxi;
public class Exam481 {
public static double x1 = 0;
public static double NUMBER = 3; // 定义函数中a的值
public static void main(String[] args) {
iteration1(NUMBER, 5, 5);
}
// 方法中三个参数值分别为函数式中a的值,Newton迭代的起始值,需要达到的精度要求10^(-n)
private static void iteration1(double a, double x, int n) {
boolean th = true;
int count = 0;
while (th) {
x1 = (2.0 * x) / 3 + a / (3.0 * Math.pow(x, 2));
count++;
System.out.println("Newton迭代" + count + "次后" + "x" + count + "="
+ x1);
// 达到精度要求后将false赋值给th,中断循环
if (Math.abs(x - x1) < Math.pow(10, -n)) {
th = false;
System.out.println("Newton迭代" + count + "次后" + "S=" + x1);
}
x = x1;
}
}
}
运行结果:
Newton迭代1次后x1=3.3733333333333335
Newton迭代2次后x2=2.336767156007576
Newton迭代3次后x3=1.740978829257261
Newton迭代4次后x4=1.4905758774387161
Newton迭代5次后x5=1.443799436236203
Newton迭代6次后x6=1.4422512334365247
Newton迭代7次后x7=1.4422495703093263
Newton迭代7次后S=1.4422495703093263
对于的情形 ,为二阶收敛
对于的情形 ,x=0是三重根,此Newton为线性收敛