making sqrt file without using sqrt function
8 views (last 30 days)
Show older comments

4 Comments
Steven Lord
on 9 Apr 2023
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Answers (2)
Walter Roberson
on 10 Apr 2023
You can code Hero's Method; see https://blogs.sas.com/content/iml/2016/05/16/babylonian-square-roots.html
Each step iteration improves the precision, so with 9 digits as the target precision, you can take 9 iterations.
0 Comments
Sam Chak
on 10 Apr 2023
Edited: Sam Chak
on 10 Apr 2023
Hi @zot
From the definition
it can be rearranged to
to repeat the iteration until the required accuracy is achieved. You can modify Newton–Raphson code for calculating the square root of a real number. Also, address the situations when the initial guess is very far from the real solution (usually happens for very large real number).
The code should generate result like this:
format long g
fun = inline('x^2 - 101', 'x');
x0 = 5; % initial guess
TolX = 1e-9;
[x, err, xx] = modNRsqrt(fun, x0, TolX)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!