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

 채택된 답변

Geoff Hayes
Geoff Hayes 2019년 2월 17일

0 개 추천

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!
Geoff Hayes
Geoff Hayes 2019년 2월 17일
편집: Geoff Hayes 2019년 2월 17일
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.
Thanks so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 2월 17일

댓글:

2019년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by