Message Boards Message Boards

0
|
3417 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

A width of interval on each iteration

Tell me please , is Mathematica a standard function which can find a width of interval?
I have to find it for each iteration.
  (*Newton's method*)
 
  step[f_, iter_, inter_] :=
   Module[{x = Mean@First@(List @@ inter)//N},
    Print["Iteration ", iter, ".\n", "Interval is: ", inter,
     "; \nx0_=", x, "; \nf(x0_)=", f[x], "; \nf'(interval)=", f'[inter],
      ";","\nIntervalsWidth=",(*??????????*),"\nX1=",IntervalIntersection[inter, x - (f[x])/(f'[inter])]];
       IntervalIntersection[inter, x - (f[x])/(f'[inter])]]
 newton[f_, inter0_, eps_] :=
  Module[{iter = 0}, 
    N@Mean@First@(List @@
         NestWhile[(++iter; step[f, iter, #]) &, inter0,
          Abs[Subtract @@ First@(List @@ #)] > eps &])];
f[x_] := x*x - 8*x + 7 ;
inter := Interval[{5, 17}] ;
newton[f, inter, 0.00001];
POSTED BY: Julia Ilkiv
4 Replies
Posted 10 years ago
From the help page for NestWhile

   NestWhile[f, expr,test]

starts with expr, then repeatedly applies f until applying test to the result no longer yields true.

Unless I've made some mistake, avoiding for the moment NestWhile and all of that, just testing the single step

   step[f, 1, {5, 17}]

does not seem to return what I think you need.

Perhaps before trying to loop off towards infinity with NestWhile, just do one step and see what you get
and then do another step with the result of that and see if that is making progress.

It is not at all clear what you are planning on doing with # and & in this example.
POSTED BY: Bill Simpson
There are a couple of things: step takes a pair of endpoints {a, b} but returns an Interval -- which is not a suitable input for the next step call.
Also, the iterations will run forever, because a and b never change, so Abs[b -a] > eps is always true.
POSTED BY: Ilian Gachevski
Posted 10 years ago
If you click on Details on the help page for Interval you should find a sufficient hint.

ALWAYS click on Details on any Mathematica help page. That will almost always provide essential information.
POSTED BY: Bill Simpson
There are no standard function, so I tried to do
by this way: but then iterations are not printed on each step     HELP;(
     
 step[f_, iter_, {a_, b_}] :=
       Module[{inter = Interval[{a, b}],x =(a+b)/2},
        Print["Iteration ", iter, ".\n", "Interval is: ", inter,";\nIntervalsWidth=",b-a,
         "; \nx0_=", x, "; \nf(x0_)=", f[x], "; \nf'(interval)=", f',
          ";","\nX1=",IntervalIntersection[inter, x - (f[x])/(f')]]; IntervalIntersection[inter, x - (f[x])/(f')]]
     newton[f_, {a_, b_}, eps_] :=
       Module[{iter = 0}, 
         N@Mean@First@(List @@
              NestWhile[(++iter; step[f, iter, #]) &, {a,b},
              Abs[b-a] > eps &])];
    f[x_] := x*x - 8*x + 7 ;
    newton[f, {5, 17}, 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