somebody help me please! I've gone over the irritation of approx_sqrt and the only way I've got each value of the function while using a calculator it was if I used y=2

format long g
approx_sqrt(2)
y =
1.5
y =
1.41666666666667
y =
1.41421568627451
ans =
1.41421568627451
function y=approx_sqrt(x)
y=x;
while abs(y^2-x)>0.001*x
y=(x/y+y)/2
end
end

7 Comments

What is the problem?
format long g
approx_sqrt(25)
y =
13
y =
7.46153846153846
y =
5.40602696272799
y =
5.0152476019449
y =
5.00002317825395
ans =
5.00002317825395
As you can see, it converges as you would expect Newton's method to converge. And I do see what appears to be quadratic convergence. Surely you don't expect an exact result, since it is an iterative scheme, and your tolerance is relatively huge.
function y=approx_sqrt(x)
y=x;
while abs(y^2-x)>0.001*x
y=(x/y+y)/2
end
end
So what is your issue?
i see what you mean,but, i'm not sure about newtons converge and the quadratic i'm not advanced in math to see it but, i did follow the problem step by step and i got the result of y =
1.5
y =
1.41666666666667
y =
1.41421568627451
ans =
1.41421568627451
only when i used y^2 but the value of y was 2 instead of the square root of x. i want to know am i correct for doing that i used a regular scientific calcluator to do it step by step and compared it to the result of the program.
What are you talking about? The function is an iterative scheme to compute sqrt(x). You passed in x==2. It returned y=1.41421568627451.
What is sqrt(2)?
format long g
sqrt(2)
ans =
1.4142135623731
It returned to you an approximation of sqrt(2), accurate to 6 significant digits. So again. Why do you think there is a problem?
What is a square root? It is a number that, when squared, generates the number you passed in. TRY IT.
1.41421568627451^2
ans =
2.00000600730488
Is that not 2, to within the desired tolerance?
@Walter Roberson i've been learning matlab and tried to understand this problem and so far, although the video says that y=sqaure root of x but, when i use a regular scientific calculator to undersatnd it more. i only get these values:y =
1.5
y =
1.41666666666667
y =
1.41421568627451
ans =
1.41421568627451
when i make y=2 instead what the video said which is y=square root of x.
function y=approx_sqrt(x)
y=x;
while abs(y^2-x)>0.001*x
y=(x/y+y)/2
end
end
i can send the link the youtube link with the time stamp. If my explaining is poor.
8:42
while abs(y^2-x)>0.001*x
so using 2 in the y place is the only way i got those values
y =
1.5
y =
1.41666666666667
y =
1.41421568627451
ans =
1.41421568627451
when i used the scientific calcator to further understand the function. So i'm asking is what i did correct! Because i tried everthing to get those values after hours of rewatching this video Lesson 6.2 while-loops in MATLAB - YouTube
8:42 and that's the time stamp if you wanna know what i struggled with. after i got the result for when x=2 i used to for the after i tried to followed the video's explanation i could the same the numbers the prgramming was giving until i used 2 for the y. So is what i did right? check the video please at the time stamp 8:42 if i failed to explain clearly. thank in advance.
Seriously. Your code is correct. Have more confidence in yourself. You got it right.

Sign in to comment.

Answers (1)

The site seems to want an answer to your question, and we just answered you in the comments. Oh well.
But seriously, your solution is correct. This is indeed Newton's method, as applied to the square root. For example, if I run your code, we see this (actually, I changed the congergence tolerance so it would iterate at least one more time, but the code is the same):
format long g
approx_sqrt(3)
y =
2
y =
1.75
y =
1.73214285714286
y =
1.73205081001473
y =
1.73205080756888
ans =
1.73205080756888
sqrt(3)
ans =
1.73205080756888
What do we know about Newton's method? It has quadratic convergence near a single root. One thing that tells us is as we get near to the solution, you should expect to see double the number of correct digits with each iteration.
So the first iteration started out at y=2. Then we see 1.75, so 2 significant digits are correct. The next iteration gave us 1.732, so with 4 correct digits. The next iteration has the approximation at 1.73205081, so the firsy 8 digits are correct. And then the final iteration gave us essentially full double precision accuracy.
function y=approx_sqrt(x)
y=x;
while abs(y^2-x)>1e-14*x
y=(x/y+y)/2
end
end
Again, this is exactly how that method should work. It did exactly what I would expect. (And the equations you used are correct.)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 20 Feb 2023

Edited:

on 22 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!