Trying to get a function to apply to values in an array

조회 수: 1 (최근 30일)
Noah D'Ascanio
Noah D'Ascanio 2021년 1월 14일
편집: Matt J 2021년 1월 14일
I'm trying to make a plot of depths from 0 up to 3*R for a R of 10 for the following function:
function V = tank(R,d)
% This function determines the volume in a tank comprised of a conical
% base of height R and a cylandrical tank of height 3R. Values of the depth of water, d, and R
% are inputted to determine the volume of the tank and if it exceeds the
% overfill limit.
OV = "Overflow";
VC =0;
Vcyl=0;
if d>(3*R)
disp(OV)
elseif d < R
VC = pi*R^2*(d/3);
Vcyl =0;
else
VC = pi*R^2*(R/3);
Vcyl = pi*R^2*(d-R);
end
V = Vcyl + VC;
By using the following script:
R = 10;
dmax = 3*R
inc = 0.5
d = 0:inc:dmax
V = tank(R,d);
My V array of V values is way off and I cant figure out where I went wrong. I'm assuming it has something to do with not applying the finction to each value of the d array I created?

채택된 답변

Matt J
Matt J 2021년 1월 14일
편집: Matt J 2021년 1월 14일
One way to fix it,
V=arrayfun(@(z)tank(10,z), d);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by