Write a user-defined MATLAB function macabre root (p, toll) that returns the cub
ID: 3798981 • Letter: W
Question
Write a user-defined MATLAB function macabre root (p, toll) that returns the cube root of a real number p using the modified Babylonian method with an accuracy of to 1. In addition, the function returns the number of iterations required to achieve the desired accuracy. Call this function, either from the command widow or using a script m-file, to determine the cube roots of: (i) 27 (ii) 785.4567, and (iii) -98764.78654. Store these values in two vectors, x and n, where x and n stores the cube roots and the number of iterations required to reach the desired accuracy. The relative iterative error needs to be less than.1.0e - 15. Make sure that your function has: (i) a meaningful H1 line, and (ii) meaningful help texts. The modified Babylonian method for finding the cube root reads as: x = (2 x_n - 1 + p/x^2_n - 1) 3Explanation / Answer
% following function calculate cuberoot of given number p with the accuracy of tol
% And return cuberoot and number of iterations required to calculate cuberoot using babylonial method.
function [y1,count1]= mycuberoot(p,tol)
x=p
e=tol
y=1
count=0
while count<x
if (x-y)<e
break
end
new_y=((2*y)+(x/y))/3 % Given formula
y=new_y
count+=1
end
y1=y
count1=count
end
[ans1,ans2]=mycuberoot(27,1.0e-15)
[ans1,ans2]=mycuberoot(785.4567,1.0e-15)
[ans1,ans2]=mycuberoot(-98764.78654,1.0e-15) %Here tol is given 1.0e-15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.