# Help With Starting Code

2 views (last 30 days)
Bailey Smith on 3 Jun 2018
Answered: Rishabh Rathore on 4 Jun 2018
So I'm attempting to write a code that does the following:
Use a function (volume of a sphere cap) and user defined inputs: r (radius), max r, amount to increment by, and the number of heights (of the liquid). It needs to create a table of h values vs volume values.
I know I will need to use two loops: one for r and one for h, and I also have the function created. I'm having some trouble getting it all started though, since I am newer to MATLAB. Any and all help is appreciated. Thank you in advance!
Here is my function:
function out = VOLUME_SPHERE_CAP(h,r)
%VOLUME_SPHERE_CAP Summary of this function goes here
% h is liquid level
% r is radius of sphere
out=pi*(h.^2).*(3*r-h)/3;
end

Rishabh Rathore on 4 Jun 2018
What i get from your question, you want a code that gives you the pairs of h and volume for all r and h.The code below creates a structure result, which has elements h and vol, using your function 'VOLUME_SPHERE_CAP'.
% get input from user for h() vector of values of h, increment, r_min, r_max
result.h=[];
result.vol=[];
for idx=r_min:increment:r_max
for j=1:numel(h)
result.h=[result.h h] %best practice would be to pre-locate the structure 'result'
result.vol=[result.vol VOLUME_SPHERE_CAP(h,idx)]
end
end