MATLAB question
You are designing a spherical tank (see figure below) of radius R = 3m, to hold water for a small village in a developing country. The volume of liquid it can hold can be computed as V = pi h^2 3R – h/3 Where h is the depth of the water and V is the volume of water. A. Write a function that uses the Newton-Raphson method to calculate the root and number of iterations to obtain the root. Your function should accept the function f, derivative function df, initial guess x_i, and the precision as inputs. function [root, iter] = newraph(f, df, x, precision) B. Write an m-file that uses the function from part A to determine the depth that the tank must be filled to so that it holds 30m^3 of water. Prompt the user for the initial guess of xi. Use a precision of 1e – 4. Your m-file should plot the function f and derivative function df. Also, mark the roots with a blue upward triangle.
Expert Answer
A)
%—————-newraph.m————————–
function [root, iter]= newraph(f,df,xi,precision)
iter=0; % to count the iteration
er=999;% intial error should be very high
x=xi;
while er>precision
x1=xi-(f(x)/df(x));
er=xi-x1
x=xi;
iter=iter+1;
end
root=xi;
To call the above function do following
% define the function f as :
R=value of R here
v=@(h) pi*h^2((3*R-h)/3)
%this is the derivative of the above function
dv=@(h) 2*pi*h*R-pi*h^2
%
vi=initial guess;
precision=put yor value here
Now call the functioin as
>>[root, iter]=newraph(v,dv,vi,precision);
B)