<texit info> author=Roman Putanowicz title=Solution to exercise 5.1.2 </texit>

back

Solution to exercise 5.1.2

<sxh c> A = input(“Give A : ”); x = input(“Give x0 : ”); epsilon = input(“Give epsilon : ”);

n = 0; error = abs(sqrt(A) - x); while ( error >= epsilon)

x = (x + A/x)/2.0;
error = abs(sqrt(A) - x);
n = n+1;

endwhile printf(“Value x=%g calculated with %d steps\n”, x, n); printf(“The error abs(sqrt(A)-x) : %g\n”, error); </sxh>

<texit> \begin{lstlisting} A = input(“Give A : ”); x = input(“Give x0 : ”); epsilon = input(“Give epsilon : ”);

n = 0; error = abs(sqrt(A) - x); while ( error >= epsilon)

x = (x + A/x)/2.0;
error = abs(sqrt(A) - x);
n = n+1;

endwhile printf(“Value x=%g calculated with %d steps\n”, x, n); printf(“The error abs(sqrt(A)-x) : %g\n”, error); \end{lstlisting} </texit>