0

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

Julia
  • 674
  • 1
  • 6
  • 18
  • Print the value of f'[ inter ] before you use it and think about that. – Bill Apr 07 '14 at 22:19
  • then it just outputs f'[inter] = Interval[{2,10}]; Iterations = 0 ; X1 = Interval[{7,7}]. SO it calculates f'[inter] correctly – Julia Apr 08 '14 at 09:16
  • there is a bunch of stuff going on here. Local symbol `old` is initially undefined. The statement `old;` does nothing. `step[old]:=` creates a delayed definition of `step` only for that undefined symbol `old`. Once you actually define `old` your function will no longer recognize it. Perhaps you mean `step[old_]`? but since the r.h.s doesn't depend on `old` I cant see what that is supposed to do. Likewise the `x0` in `newton` should likely be `x0_`. – agentp Apr 08 '14 at 12:23
  • my advice, put the interval arithmetic aside and first implement a plain old newton method approach. – agentp Apr 08 '14 at 12:40

1 Answers1

0

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.

agentp
  • 6,849
  • 2
  • 19
  • 37
  • AAAAND PLEASEEEE tell me how to output some calculated results like inter, x, f(x), f'[inter] after each iteration. e.g. in my example iterations=4, so after 1st iteration: inter=?, x=?, f(x)=?, f'[inter]=?..and after the other. – Julia Apr 08 '14 at 20:55
  • i put a print statement as an example. If you want to *save* those results for later use look up `Reap/Sow` – agentp Apr 08 '14 at 22:29