0

I need to write a program to find the square root using the Newton-Raphson Method using a guess estimate. the equation for the Newton-Raphson Method is:

xn+1 = xn-xn**2 − a/2*xn

whereby n is the number of iterations.

The assignment tells me that a loop should be performed between 1 and the total number of specified iterations. and also Each step of the loop, the current value of the ‘solution’ should be used to calculate the next value of the solution, in the form specified by the equation.

Hint: Remember, the assignment of a value for a variable evaluates the right side of the equals before performing the assignment itself. This means only one variable for x is needed to calculate each new value; you don’t need ‘old_x’ and ‘new_x’ or similar.

This is what i have so far:

program assign_10_2
implicit none

real :: a, b, x
integer :: c, i, j 


write(*,*) 'please enter a value to determine the square root of'
read(*,*) a
write(*,*) 'please enter a value to use as the initial guess for the solution'
read(*,*) b
write(*,*) 'please enter a value for how many iterations it should perform to calculate the value'
read(*,*) c
write(*,*) a, b , c


do i= 1, c, 1
 x = b-((b**2-a)/2*b)
write(*,*)  i, x
end do

end program assign_10_2

I am by no means asking for the answer just a push in the right direction I am completely new to programming and its all very confusing to me. I know that I have done something wrong with regards to the equation.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • I would expect to see some initialization for `x` prior to the loop, and inside the loop, for `x` to appear on both sides of the assignment. – njuffa Mar 28 '15 at 21:00
  • what does that mean? sorry – alen jacobs Mar 28 '15 at 21:01
  • "This means only one variable for x is needed to calculate each new value; you don’t need ‘old_x' and ‘new_x’ or similar.". So `x` on the left side of the assignment is implicit 'new_x' and `x` on the right-hand side is implicit 'old_x'. You need some initial value 'x_0', so `x` is initialized before the loop. The "initial guess". – njuffa Mar 28 '15 at 21:18
  • thank you so much this helped me complete it. thank you – alen jacobs Mar 28 '15 at 21:47
  • Since you are a beginner, and working with fortran, I would like you to have a look at Metcalf's fortran optimization. The hardware part is not important(its very old book), but advice on writing code is invaluable. I am saying this because, the `x**2` gives sore to my eyes and many other developer, which is much slower than simple `x*x`. Obviously, you won't see any difference, but, in long run, if you live with fortran, this habit will pay. – BaRud Mar 30 '15 at 16:08
  • A modern compiler will optimize that issue away. Even `x**2.0` doesn't hurt performance by my testing. I prefer `x**2` for readability. – agentp Mar 30 '15 at 17:38

1 Answers1

0

This line is wrong:

x = b-((b**2-a)/2*b)

b is the initial guess. You don't want to keep going back to the initial guess, you want to use the previous value of x computed. Also you should not write /2*x if you want to divide by 2*x. I think you want the following assignment instead:

x = x - ((x**2-a)/(2*x))
Douglas Zare
  • 3,296
  • 1
  • 14
  • 21