#### Real Examples

Loops can be used in many ways. They can perform the same calculations on many different starting values, as shown in the sine() examples above.

Loops are also commonly used to perform a series of calculations on a single starting value, to produce a single result. A common use for this sort of iteration is found in numerical analysis, which uses iterative calculations to solve many kinds of equations.

##### Integer Powers

Integer powers can be computed using a simple loop.

```/***************************************************************************
*  Description:
*
*  Arguments:
*
*  Returns:
*
*  History:
*  Date        Name        Modification
*  2013-08-11  Jason Bacon Begin
***************************************************************************/

#include <stdio.h>
#include <sysexits.h>

int     main(int argc,char *argv[])

{
// Variable definitions
double          base, power;
unsigned int    exponent, i;

// Statements
printf("Enter base and exponent: ");
scanf("%lf %u", &base, &exponent);
power = 1.0;
for (i = 1; i <= exponent; ++i)
power *= base;
printf("%f ** %u = %f\n", base, exponent, power);
return EX_OK;
}

```
```!-----------------------------------------------------------------------
!   Program description:
!       Compute integer powers of real numbers
!-----------------------------------------------------------------------

!-----------------------------------------------------------------------
!   Modification history:
!   Date        Name        Modification
!   2011-03-10  Jason Bacon Begin
!-----------------------------------------------------------------------

! Main program body
program int_power
use constants           ! Constants defined above
use ISO_FORTRAN_ENV     ! INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT, etc.

! Disable implicit declarations (i-n rule)
implicit none

! Variable definitions
real(8) :: base, power
integer :: exponent, i

! Statements
print *, 'Enter base and exponent on one line:'
power = 1.0d0
do i = 1, exponent
power = power * base
enddo
print *, base, ' ** ', exponent, ' = ', power
end program

```
##### Newton's Method

Newton's method is a simple iterative approach for finding the roots of an equation of the form f(x) = 0.

1. Specification: Find a root for a function f(x). ( Where does f(x) equal 0. )
2. Design: Use Newton's method:
1. Make an initial guess for x.
2. Follow a tangent line from the function f(x) to where it crosses the x axis. This point will usually be closer to the root of f(x) than the initial x was. The value of x where the tangent intersects the axis is: n = x - f(x) / f'(x)
3. Use n as the next guess. By repeating the process, we get closer to the root of f(x) each time.

How do we know when to stop iterating?

• Method 1: Check the value of f(x). The problem with this method is that a function could come very close to zero at some point, and then veer away, so we could get a false positive root detection. Problems can also occur for functions with a very high or very low slope as they approach the x axis. The former can lead to excessive iterations that are not necessary for an accurate result, while the latter can lead to an inaccurate result.
• Method 2: Compare guesses. As we get closer to a true root, the difference between consecutive guesses gets smaller. As we approach a non-zero minimum or maximum, the difference will begin to get bigger again. This method isn't fail safe either, as some functions may have a slope near vertical, causing the next guess to be very close to the current one, while f(x) is still far from 0. It is generally cheaper to compute than f(x), however.

Note that Newton's method does not guarantee finding a root, even if one exists. Whether, and how fast it converges on a root depends on your initial guess.

```#include <stdio.h>
#include <sysexits.h>

#define ABS(n)  ((n) < 0 ? -(n) : (n))

int     main(int argc,char *argv[])

{
double  x, y, slope, tolerance;

printf("Enter initial guess for x: ");
scanf("%lf", &x);

printf("Enter the tolerance: ");
scanf("%lf", &tolerance);

// Compute first guess to prime the loop
y = x * x - 1.0;

while ( ABS(y) > tolerance )
{
// Recompute x, y and slope
slope = 2.0 * x;
x = x - y / slope;
y = x * x - 1.0;

// Debug output
printf("%f\n", y);
}

printf("%f\n", x);  // This is the solution within tolerance
return EX_OK;
}

```
```program newton

! Disable implicit declarations (i-n rule)
implicit none
real(8) :: x, y, slope, tolerance

write (*,*) 'Enter initial guess for x:'

write (*,*) 'Enter the tolerance'

! Compute first guess to prime the loop
y = x ** 2 - 1.0d0

do while ( abs(y) > tolerance )
! Recompute x, y and slope
slope = 2.0d0 * x
x = x - y / slope
y = x ** 2 - 1.0d0

! Debug output
write (*,*) y
enddo

write (*,*) x  ! This is the solution within tolerance
end program

```

### Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
1. Modify the Newton's method program so that it terminates when the difference between successive estimates is below the tolerance. Under what conditions would this produce a better estimate than the program that compares the y value to tolerance?