Write Matlab function script that you developed in such that it will take a real number p and a desired tolerance TOL as input and will return the cube-root and number of iterations as output. You need to use a while(1) or infinite loop. Use your function to determine the cube roots of : (a) 81
I am getting stuck on how i can use a while loop for this. Any help would be much appreciated.
PLEASE GIVE THUMBS UP, THANKS
SAMPLE OUTPUT :
code:
%Returns cube root of a no p
function value=cubicRoot(p,TOL)
%Set start and end for binary search
startt = 0; endd = p;
while (true)
mid=(startt + endd)/2;
error = abs(p-(mid*mid*mid));
%If error is less than TOL then mid is
%our answer so return mid
if (error <= TOL)
value= mid;
break;
end
%If mid*mid*mid is greater than p set
% endd = mid
if ((mid*mid*mid) > p)
endd = mid;
% If mid*mid*mid is less than p set
% startt = mid
else
startt = mid;
end
end
end
Get Answers For Free
Most questions answered within 1 hours.