我必须在Mathematica8中以摩尔的形式编写第一步Newton方法的程序迭代,正是在这个例子中: GIVEN:
F [X _]:= X * X-8 * X + 7; inter = {5,9}; eps = 10 ^( - 6);
计算:
x0:=平均值[{5,9}]; f [x0]; f' [x_]; F' [除]; n0 = x0 - f [x0] / f' [inter]; x1 = IntervalIntersection [inter,n0];
我试图这样做,但它没有正确计算迭代次数:
f[x_]:=x*x-8*x+7
inter := Interval[{5, 9}]
x0 := Mean[{5, 9}]
newton[f_,x0,eps_]:=Module[{old,new,iter,step},
old ;
step[old] := IntervalIntersection[inter, x0 - (f[x0])/(f'[inter])];
new = step[old];
iter=0;
While[Abs[old-new]>eps,
old = new;
new = step[old];
iter++];
Print["Iterations = ", iter];
Print["X1 = ", new];
]
newton[f,x0,0.00001]
请帮助!!!
答案 0 :(得分:0)
我尽可能地坚持你的基本循环结构。
step[f_, inter_] := Module[{x = Mean@First@(List @@ inter)},
IntervalIntersection[inter, x - (f[x])/(f'[inter])]]
newton[f_, inter0_, eps_] := Module[{iter,inter},
inter = inter0;
iter = 0;
While[Abs[Subtract @@ First@(List @@ inter)] > eps,
inter = step[f, inter];
iter++;
Print["loop count",iter,inter,
x = N@Mean@First@(List @@ inter),f[x],f'[inter]]];
Print["Iterations = ", iter];
Print["X1 = ", N@Mean@First@(List @@ inter)];]
f[x_] = x^2 - 8 x + 7 ;
inter = Interval[{5, 10}] ;
newton[f, inter, 0.00001];
在7.
次迭代中结果4
newton[f_, inter0_, eps_] := Module[{iter = 0},
Print["X1 = ", N@Mean@First@(List @@
NestWhile[(++iter; step[f, #]) &, inter0,
Abs[Subtract @@ First@(List @@ #)] > eps & ])];
Print["iter=", iter]]
请注意,只有在衍生符号在初始Interval
中没有变化时,这才能正常工作。否则你最终会有多个间隔。我不熟悉区间运算,很容易看到如何处理它。