Question
Please use the following code to answer the questions: function [sqr, er] = Babylonian_part(p,maxitr) %% Babylonain Method for Finding the square root of p
Please use the following code to answer the questions:
function [sqr, er] = Babylonian_part(p,maxitr)
%% Babylonain Method for Finding the square root of p
% This function calculates the square root of a real number using the Babylonian
% method (an iterative algorithm) with an user-defined maximum number of
% iterations, and plots the relative error versus the number of iterations
% Inputs/Outputs:
% p = The number whose square root is to be evaluated
% maxitr = Maximum number of iterations allowed
% er = Vector of approximate relative errors
% sqr = Estimated value of the square root of p
%% Initializations
er = ; % Initializate the approximate error vector er
sqr = ; % Initializate sqr by p
%% Execute the Alogrithm
for k = 1:maxitr
% Store old estimate
% Calculate new estimate
% Calculate percent error
end
%% Plot the error versus iteration number
figure;
plot((1:k),er(1:k), 'ko','markersize',6,'markerfacecolor','r'); grid on;
xlabel('Iteration Number');
ylabel('Relative Error (%)');
ylim([-5 100]);
set(gca,'Fontsize',12,'LineWidth',1);
end
One numerical method for calculating the square root of a number is the Babylonian method. In this method p is calculated in iterations. The solution process starts by choosing a value x as a first estimate of the solution. Using this value, a second, more accurate solution x2 can be calculated with x2 = (x + p/x)/2, which is then used for calculating a third, still more accurate solution x3, and so on. The general equation for the algorithm to estimate the solution for the square root of a number is thus x+1 = (xi + p/xi)/2. a) Write a MATLAB user-defined function that calculates the square root of a number using the Babylonian method. Name the function function [sqr, er] = Babylonian (p, maxitr) where maxitr is the maximum number of iterations to be performed, er is a vector of percent errors, and sqr is the estimated value of the square root of p. In the program, use x = p for the first estimate of the solution. Then, by using the general equation in a loop, calculate new, more accurate solutions. Your function should define a vector of the approximate percent errors, er, defined by erk = 100%. |xnew-Xold | for the k-th iteration Xnew and plot the error versus the number of iterations. A partially completed code has been provided (remove the "part from the function handle before running). b) Use Babylonian to find the square root of 12.34. Use the value of 20 for maxitr. Interpret the output plot. Compare your result with the MATLAB result using sqrt. c) Modify your Babylonian function so that a while-loop checks both the error tolerance and the maximum number of iterations. Name the function function [sqr, er] = Babylonian Err (p, maxtol, maxitr) where the input arguments p is the number whose square root is to be determined, maxtol is the maximum tolerance, and maxitr is the maximum number of iterations allowed. The output arguments sqr is the estimated value of the square root of p, and er is the percent error. In the program, use x = p for the first estimate of the solution. Then, by using the general equation in a loop, calculate new, more accurate solutions. Terminate the looping if either of the following conditions is met: The relative approximate error is less than or equal to the maximum error tolerance, maxtol The number of iterations exceeds the maximum number of iterations allowed, maxitr. d) Use Babylonian Err to find the square root of 12.34. Use the value maxtol=1e-5 and maxitr=10.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started