Message Boards Message Boards

0
|
2954 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Output data on iterations correctly

PLEASE help to edit this code to output data on each iteration because it outputs only on the last...
favorite
PLEASE help to edit this code to output data on each iteration because it outputs only on the last...
[b]step[f_,inter_] := Module[{x=Mean@First@(List @@ inter)}, [/b]
[b]    IntervalIntersection[inter, x - (f[x])/(f'[inter])]] [/b]
[b] newton[f_, inter0_, eps_] := Module[{iter = 0}, [/b]
[b]    Print["X1 = ", N@Mean@First@(List @@ [/b]
[b]    NestWhile[(++iter; step[f, #]) &, inter0, [/b]
[b]        Abs[Subtract @@ First@(List @@ #)] > eps & ])]; [/b]
[b]        Print["iter=", iter]; [/b]
[b]        Print["Iteration ",iter,".\n","Interval is: ",inter,"; \nx0_=", [/b]
[b]            x= Mean@First@(List @@ inter) ,"; \nf(x0_)=",f[x],"; \nf'(interval)=",f'[inter],";"]]; [/b]
[b]f[x_] := x*x - 8*x + 7; [/b]
[b]inter := Interval[{1,9}]; [/b]
[b]newton[f, inter, 0.00001];[/b]
POSTED BY: Julia Ilkiv
2 Replies
To print something at each step, try making the Print statements part of the step function.

 step[f_, iter_, inter_] :=
  Module[{x = Mean@First@(List @@ inter)},
   Print["Iteration ", iter, ".\n", "Interval is: ", inter,
    "; \nx0_=", x, "; \nf(x0_)=", f[x], "; \nf'(interval)=", f'[inter],
     ";"]; IntervalIntersection[inter, x - (f[x])/(f'[inter])]]
 
 newton[f_, inter0_, eps_] :=
  Module[{iter = 0},
   Print["X1 = ",
    N@Mean@First@(List @@
         NestWhile[(++iter; step[f, iter, #]) &, inter0,
          Abs[Subtract @@ First@(List @@ #)] > eps &])];]
POSTED BY: Ilian Gachevski
Thank You veeery much!

please, Could You help me to write this Newton's method not in interval but in the mean of Interval??? and in 'While' it could be while the function(point)>eps So I only has to change  
p := Mean[{5, 9}]; f'[p] ; f'[inter]; n0 = p - f[p]/f'[p];

x1 = IntervalIntersection[p,n0];
I tried to write it
but again iterations doesn't work correctly;(
 step[f_,inter_] := Module[{p:=Mean@First@(List @@ inter)},
 
 IntervalIntersection[inter, p - (f[p])/(f'[p])]]
 
 newton[f_, inter0_, eps_] := Module[{iter = 0},
 
 Print["X1 = ", N@Mean@First@(List @@
 
 NestWhile[(++iter; step[f, #]) &, inter0,

    Abs[f[p]] > eps & ])];

    Print["iter=", iter];

    Print[ "loop counts: ", iter, ". ", inter,"; p=",p=Mean@First@(List @@ inter), "; f[p]=",f[p],"; f'[p]=",f'[p]]];

f[x_] := x*x - 8*x + 7;

inter := Interval[{5,9}];

newton[f, inter, 0.00001];
POSTED BY: Julia Ilkiv
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract