{"id":147,"date":"2017-09-13T10:30:13","date_gmt":"2017-09-13T13:30:13","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=147"},"modified":"2017-09-13T10:30:13","modified_gmt":"2017-09-13T13:30:13","slug":"newton2-f","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/newton2-f\/","title":{"rendered":"newton2.f"},"content":{"rendered":"<pre>      program newton\r\nc\r\nc   Use a Newton iteration to solve the equation\r\nc\r\nc   John Mahaffy 2\/14\/95\r\nc\r\nc     x**3+x-10=0\r\nc\r\nc    xin  -     input initial guess to the solution\r\nc    x    -     current approximation to the solution\r\nc    f(x) -     x**3+x-10\r\nc    dfdx -  derivative of f with respect to x\r\nc    xo    -    previous guess for solution\r\nc    eps   -    convergence criterion\r\nc    dx    -    change in solution approximation\r\nc    it    -    number of iterations\r\nc    itmax -    maximum number of iterations\r\nc\r\nc\r\n      implicit none\r\n      real x,f,dfdx,xo,eps,fx,dx,xin,abs\r\n      integer it,itmax                                              \r\nc                                                                      c\r\nc     Use fortran statement functions to define f and its derivative\r\nc     Note that these statements must appear before other executable\r\nc     statements\r\nc\r\nc     Now start executable fortran statements\r\nc\r\n      eps=1.e-5\r\n      itmax=10\r\n      print *,'What is your initial guess for the solution?'\r\n      read *, xin\r\n      x=xin\r\nc\r\nc   The following is a more traditional Fortran do loop.  It is a more\r\nc   natural form for this particular task.  It executes the instructions\r\nc   from the \"do\" through the instruction with label \"100\" for all\r\nc   values of \"it\" between 1 and itmax.  Try setting itmax=0 and see\r\nc   what happens.  If itmax=0 are the contents of the loop executed?\r\nc\r\n      write(*,'(\/,\"Simple do loop\",\/)')\r\nc\r\nc  Note the direct use of a format above rather than a reference to a\r\nc  format number.  This is only useful for one time use of a given\r\nc  format structure\r\nc\r\n      do 100 it=1,itmax\r\n         xo=x\r\n         fx=f(xo,dfdx)\r\nc             ^^^^^^^\r\nc                ^  This area between the parentheses is often refered\r\nc                ^  to as the calling sequence, and contains arguments\r\nc                ^  passed as input to the function and passed back as\r\nc                ^  output from the function\r\nc \r\nc \r\n         dx = -fx\/dfdx\r\n         x=xo+dx\r\n         write (*,2000)it,x,fx,dx \r\n         if(abs(x-xo).lt.eps*abs(x)) go to 200\r\n  100    continue\r\n      print *, 'Iteration failed to converge'\r\n  200 x=xin\r\nc\r\nc   I could loop over only even values of \"it\" with the following structure.\r\nc   It basically says loop over values of \"it\" from 0 to itmax in steps of 2.\r\nc   Check to see what happens when you set itmax=3 and input a starting guess\r\nc   of xo=6.0.  Yes, this is a pointless way to do a Newton Iteration.  It\r\nc   is just here as another example of the workings of DO loop indices.\r\nc\r\n      write(*,'(\/,\"Do loop with a special increment on the index\",\/)')\r\n      do 250 it=0,itmax,2\r\n         xo=x\r\n         fx=f(xo,dfdx)\r\nc             ^^^^^^^\r\nc                ^  This area between the parentheses is often refered to as the\r\nc                ^  calling sequence, and contains arguments passed as input to\r\nc                ^  the function and passed back as output from the function\r\nc \r\n         dx = -fx\/dfdx\r\n         x=xo+dx\r\n         write (*,2000)it,x,fx,dx \r\n         if(abs(x-xo).lt.eps*abs(x)) go to 300\r\n  250    continue \r\nc  \r\nc  Note the value of \"it\" after the loop is completed.  Because of the step\r\nc  of 2 \"it\" never directly gives the number of times you've been through\r\nc  the loop.\r\nc  \r\n      print *,'------------------------------------------------'\r\n      print *,' Value of loop index after exit from the loop'\r\n  300 print *, 'it = ', it\r\nc\r\nc   \r\nc  \r\n  500 stop                       \r\n 2000 format(1x,'it=',i3,', x=',f8.3,', f(x)=',f8.3,', dx=',f8.3) \r\n      end\r\n      function f(x,deriv)\r\nc                ^^^^^^^\r\nc                ^  These are the \"dummy\" arguments of the function.  They need\r\nc                ^  not be the same variable names as those appearing\r\nc                ^  in the calling sequence where the function is referenced.\r\nc                ^  However the number of arguments here should match the\r\nc                ^  number in the reference to the function (There are\r\nc                ^  exceptions to this rule but worry about that when you\r\nc                ^  become much better with Fortran).\r\nc                ^  It is also very important to match the data types between\r\nc                ^  corresponding arguments here and in the calling sequence\r\nc                ^  (There may be times later when you violate this rule too\r\nc                ^  but, again, you'll need to understand why you are tricking\r\nc                ^  the machine.\r\nc\r\nc   Evaluate the function f(x)=x**3+x-10\r\nc   also return the derivative of the function\r\nc                                             \r\n      implicit none\r\n      real f,x,deriv\r\nc\r\nc   John Mahaffy 2\/14\/95\r\nc\r\nc   Function arguments can receive values from the current reference to\r\nc   the function, and can also pass data back to the program using the\r\nc   function.  Documentation of arguments should clearly delimit those\r\nc   receiving input for the function from those transmitting output.\r\nc\r\nc   INPUT\r\nc        \r\nc   x   -   independent variable\r\nc\r\nc   OUTPUT\r\nc\r\nc   f     -   value of the function\r\nc   deriv -   derivative of the function with respect to x\r\nc\r\n      deriv=3*x**2+1\r\n      f=x**3+x-10\r\n      return\r\n      end<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>program newton c c Use a Newton iteration to solve the equation c c John Mahaffy 2\/14\/95 c c x**3+x-10=0 c c xin &#8211; input initial guess to the solution c x &#8211; current approximation to the solution c f(x) &#8211; x**3+x-10 c dfdx &#8211; derivative of f with respect to x c xo &#8211; [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-147","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/147\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}