Question

I have to write the procedure with iterations of the first step Newton's method in Moore's form in Mathematica8, exactly on this example: GIVEN:

f[x_]:=x*x-8*x+7; inter={5, 9}; eps = 10^(-6);

TO CALCULATE:

x0 := Mean[{5, 9}]; f[x0] ; f'[x_] ; f'[inter]; n0 = x0 - f[x0]/f'[inter]; x1 = IntervalIntersection[inter,n0];

I tried to do it, but it doesn't calculate iterations correctly :

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]

HELP PLEASE !!!

Était-ce utile?

La solution

I fixed this up sticking with your basic loop structure as much as possible.

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];

result 7. in 4 iterations

of course in mathematical there are usually cleaner approaches than a do loop:

 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]]

Note this only works properly if the sign of the derivative does not change in the initial Interval. Otherwise you end up with multiple intervals. I'm not that familiar with interval arithmetic to readily see how to deal with that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top