Question: Newton's method as procedure

Problem is here how to define the local variables?

Also the formatting of the code is not ideal

There is a error , but if the local variables are good defined , it could vanish    


 

task 6 newtons method

 Newton's Method (see worksheet)

   
 

Tasks:

CDExercises

(1)  Figure 3.9 shows the convergence of Newton's method to a root  r of  the equation sin x - xcos x = 0. This was for x[0] = 5.5 and `ε` = 0.1e-3, and gives  r = 4.503123428, approximately. Obtain a better approximation, as suggested in the book, by using `ε` = .000001. and altering the above code appropriately. Compare your answer with the approximation to r that you get by using Maple's  fsolve command. How close to zero is  E =| f (r)|  for each of these values of r ?

 

(2) Alter the above code for Newton's method and use it to draw the picture shown in Figure 3.10 in the book.

--------------------------------

 

I make a procedure out of it 

f is function

a.. b endpoints interval x -range

e is the cutoff in the accuracy (tolerance?)

N is number of iterations

 

restart:

with(plots,display):

#
# Parameters - change as necessary
#
  a:= 0.8: b:= 2: N:= 100: e:= 0.0001:
  f:= x->x^2-1:

NewtonM:= proc(f, a, b, N, e)
               local n, tps, x0, E, A, x1, ps, x, j,  pf, pic :  
               uses plots:  
   n:=0: tps:={}:
   x0:=2.0: E:=evalf(abs(f(x0))):
   while ( E>e and n<N ) do
     A[n,1]:=x0;
     A[n,2]:=E;
     x1:=evalf(T(x0));
     E:=abs(f(x1));
     ps:={plot([[x0,0],[x0,f(x0)],[x1,0]],color=black,
                 thickness=2)}:   
     tps:=tps union ps:
     n:=n+1:
     x0:=x1:
   end do:
 
 for j from 0 to n-1 do x[j]=A[j,1],'E'=A[j,2]end do;

 pf:={plot(f(x),x=a..b)}:
 pic:=pf union tps:
 display(pic,tickmarks=[3,3]);
end proc:

maplemint(NewtonM);

Procedure NewtonM( f, a, b, N, e )
  These names were used as global names but were not declared:  black, color,
      thickness, tickmarks
  These local variables were used but never assigned a value:  x

 

NewtonM(x->x^2-1,0.8,2,100,0.0001);

Error, (in NewtonM) cannot determine if this expression is true or false: 0.1e-3 < abs(T(2.0)^2-1)

 

 


 

Download exc_set_3_task_6.mw

Please Wait...