Hello I need help in this question
Show older comments
function [out] = sqrt_bywhile(x, inc)
sqrt = 0;
if ~exist('inc','var')
inc = 0.01;
end
inc1 = 0;
while sqrt <= x
sqrt = x^inc1;
inc1 = inc1+inc;
end
out = inc1-inc;
I am new to the matlab and working on while loops
I need to know how can I write a code for this.
Write a function sqrt_bywhile(x, inc) that estimates the square root of a number x. Start with an estimate of zero and gradually increase your estimate by inc at each step. Return an estimate whose square is equal to or less than x. Your estimate should be equal to the correct square root if the estimate can land on the correct value with the given inc (e.g., sqrt_bywhile(25,1) should return 5). Your estimate should be smaller than then correct square root if the estimate does not land on the correct value (e.g., sqrt_bywhile(24,1) should return 4 and sqrt_bywhile(26,1) should return 5). If inc is not given, use inc=0.01.
- You must use a while loop to solve this problem.
- Do not use for loops.
- Do not use vectorized code.
- Do not use the actual sqrt() function
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!