Save output data in a vector format while loop

Hi all,
I would like to export the output of the while loop in a vector format but it is not working for me... below is the code please advise.
Thanks
function [Q,h,t,v]= func (h0,D,d,dt)
g=9.81;
rho=1000;
h=h0;
t=0;
j=0;
% v=sqrt(2*g*h)
% h=(sqrt(h0)-1/2*d^2/D^2*sqrt(2*g)*t)^2
% Q=sqrt(2*g*h)*pi*d^2/2
while (t<228)
j=j+1;
t=t+dt;
h=((sqrt(h0))-((1/2)*(d^2)/(D^2)*(sqrt(2*g))*t))^2;
v=sqrt(2*g*h);
Q=sqrt(2*g*h)*pi*(d/2)^2;
a(j)=Q;
b(j)=h;
c(j)=t;
d(j)=v;
end

 采纳的回答

Ahmad - since your function signature defines the output parameters, then you can update these parameters rather than using them as "intermediaries" for the a, b, c, and d arrays. For example
function [Q,h,t,v]= func (h0,D,d,dt)
g = 9.81;
rho = 1000;
h = h0;
t = dt:dt:228; % create your t array with a step size of dt
Q = zeros(length(t), 1); % pre-allocate memory for your Q, h, and v arrays
h = zeros(length(t), 1);
v = zeros(length(t), 1);
for j = 1:length(t)
h(j) = ((sqrt(h0))-((1/2)*(d^2)/(D^2)*(sqrt(2*g))*t(j)))^2;
v(j) = sqrt(2*g*h(j));
Q(j) = sqrt(2*g*h(j))*pi*(d/2)^2;
end
Try the above and see what happens!

7 个评论

Thanks Geoff,
still having an issue when calling out my function, i got "undefined function or variable 'ho' even though i have it defined with a value when i run my function, and it recognizes and error in my function as well...
can you please make the edits in the while loops?
Thanks!
Ahmad - you may have a typo. Is the variable named ho or h0 (the letter 'o' vs the number 0)?
Thanks for the quick response!
but all are h0
Please copy and paste the full error message to this question including the line number and code that is throwing the error.
here is the error:
Undefined function or variable 'h0'.
Error in hw4 (line 1)
[Q,h,t,v]= func (h0,D,d,dt)
Ahmad - well if this is error is at line 1 of your hw4.m file then the error message makes sense since you haven't yet defined h0 (or D or d or dt). You need to define what these values should be or pass in numeric values into func.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by