# Square Root Iteration Problem

Discussion in 'HTML, Graphics & Programming' started by TomElZ, 12 Apr 2010.

1. TomElZ

# Posts: 305

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.

2. TomElZ

# Posts: 305

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!

3. Inquisitor

# Location: Birmingham

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: 12 Apr 2010
4. TomElZ

Didn't work

5. Dj_Jestar

# Location: Back in East London

Code:
`Math.Sqrt(x);`

6. Inquisitor

# Location: Birmingham

It does for me

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);
}
}
}
```

7. Randomface74

# Location: UK

will get started and write one now, sec

gotta put this on hold for now. will revisit later.

Last edited: 12 Apr 2010
8. Randomface74

# Location: UK

been having double glazing fitted today, the guys have buggered off now so i'm free to continue. watch this space!

dust everywhere

9. Randomface74

# Location: UK

snip

Last edited: 30 Nov 2017
10. TomElZ

Legend

11. Inquisitor

# Location: Birmingham

What was wrong with mine?

12. Randomface74

# Location: UK

doesn't do what he asked at all. did you check mine out?