Matlab: Divide and Average Method

조회 수: 12 (최근 30일)
Justin Lee
Justin Lee 2021년 2월 17일
편집: John D'Errico 2021년 2월 17일
Can you help me find the bug in Problem #4? Not sure where my code is wrong. For example, my code outputs the sqrt(4) as 1.68...

답변 (1개)

John D'Errico
John D'Errico 2021년 2월 17일
편집: John D'Errico 2021년 2월 17일
Its that new math. The supreme court redefined sqrt(4) as 1.68 just recently. So your code is correct. Now all we need to do is convince your teacher of that. :-)
The divide and average method for sqrt is pretty simple really. In fact, it converges pretty rapidly.
format long g
a = 4;
asqrt = 1;
tol = 1.e-12;
while abs(asqrt*asqrt - a) > tol
asqrt = (a/asqrt + asqrt)/2
end
asqrt =
2.5
asqrt =
2.05
asqrt =
2.00060975609756
asqrt =
2.00000009292229
asqrt =
2
So the idea is you divide the current estimate of the sqrt into a, and then average THAT result with the current estimate. Repeat until you get bored, or until it meet your tolerance. 1 is a good starting point.
What you will see here is this is actlually a quadratically convergent estimator of the sqrt. It does indeed converge rapidly, effectively doubling the number of digits in the estimate after each iteration. This is why I called the convergence behavior quadratic.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by