Square Root Iteration Problem

Associate
Joined
7 Aug 2008
Posts
302
Hey lads.

Basically am going a bit insanse so am kinda hoping someone can help me out. Basically I need to create a program in C# that allows the user to enter a number that they want to find the square root off and then enter there approximation.

From this the following formula is used. new = (old + x/old) / 2. So for example if I wanted to find the square root of 25 and I put in an approximation off 7 then new would be new = (7 + 25/7) / 2. Therefore new would then = 5.2857. This would then be used for the next iteration - new = (5.2857 + 25/5.2857) /2.

This process then needs to be repeated until the number is within ±0.00000005 of the actual answer which I know how to do. Each iteration needs to be wrote to a text box. I no its going to be a Do/While loop with the While condidtion being to stop when the number is within ± 0.00000005 of the actual Square Root. So it would be 5 in this case. I just don't know how to do this.

Chears to anyone who can help.
 
Associate
OP
Joined
7 Aug 2008
Posts
302
namespace AssignmentTwo_QuestionTwo
{
public partial class Form1 : Form
{
double sqrtnumb;
double old;
double new1;
double answer;

public Form1()
{
InitializeComponent();
}

private void btnapprox_Click(object sender, EventArgs e)
{
sqrtnumb = Convert.ToDouble(txtsquareroot.Text);
answer = Math.Sqrt(sqrtnumb);
old = Convert.ToDouble(txtapprox.Text);
do
{
new1 = (old + sqrtnumb / old) / 2;
txtiterations.Text = String.Format("The answer is {0} ", answer);
txtiterations.Text += Environment.NewLine;
txtiterations.Text += String.Format("The next iteration is {0} ", new1);
txtiterations.Text += Environment.NewLine;
}
while ((new1 - old / new1) > 0.0000005);
{
old = new1;
new1 = (old + sqrtnumb / old) / 2;
}
}
}
}

What I've got so far which just crashes ahha!
 
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
You want something like this:

Code:
double y = 25;
double x = 7;
double xOld;
double epsilon = 0.0000005;
do
{
	xOld = x;
	x = (x + y / x) / 2;
} while (Math.Abs(x - xOld) > epsilon);

So with each iteration, you're copying the current value of x to keep track of the change from the previous iterate and then calculating the new iterate. The loop condition is that the absolute difference between iterates is greater than the desired precision (usually called epsilon), and this is checked after each new iterate is calculated.

This is untested, though, so it might not work! Also, remember to use code tags in the future :)
 
Last edited:
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Didn't work :(

It does for me :confused:

This produces the output "sqrt(25) = 5":

Code:
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double y = 25;
            double x = 7;
            double xOld;
            double epsilon = 0.0000005;
            do
            {
                xOld = x;
                x = (x + y / x) / 2;
            } while (Math.Abs(x - xOld) > epsilon);

            Console.WriteLine("sqrt({0}) = {1}", y, x);
            Console.ReadKey(true);
        }
    }
}
 
Back
Top Bottom