0
|
3172 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

Output data on iterations correctly

Posted 11 years ago
 PLEASE help to edit this code to output data on each iteration because it outputs only on the last...favoritePLEASE 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]
2 Replies
Sort By:
Posted 11 years ago
 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 11 years ago
 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 itbut 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];