Write a user-defined MATLAB function mycuberoot (p, tol) that returns the cube r
ID: 1843761 • Letter: W
Question
Write a user-defined MATLAB function mycuberoot (p, tol) that returns the cube root of a real number p using the modified Babylonian method with an accuracy of tol. In addition the function returns the number of iterations required to achieve the desired accuracy. (b) 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, times 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. (c) 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: chi_n = (2 chi_n-1 + p/chi_n-1^2) 3Explanation / Answer
function [ x,n ] = mycuberoot( p,tol )
%To find the cube root using babylonian method for a desired tolerance.
% Detailed explanation goes here.
x=1; % let x0 =1 (initial assumpution)
for n=1:inf % iteration for 1 to infinite iterations
c=x; % c is previous approximation
x=(2*x+p/(x^2))/3; % finding x by modified babylonian method
error =c-x; % finding approximate error
error=abs(error); %making absolute value
relativeerror=error/abs(x); %find relative itterative error
if relativeerror<=tol %check for tolerance
return
end
end
end
%% now call function in command window as
[x,n]=mycuberoot(27,1.0e-15) we get x= 3, n=10
[x,n]=mycuberoot(789.4567,1.0e-15) we get x= 9.2422, n=15
[x,y]=mycuberoot(-98764.78654,1.0e-15) we get x= -46.2240 , n= 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.