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 件のコメント

Ahmad Sayah
Ahmad Sayah 2019 年 2 月 17 日
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)?
Ahmad Sayah
Ahmad Sayah 2019 年 2 月 17 日
Thanks for the quick response!
but all are h0
Geoff Hayes
Geoff Hayes 2019 年 2 月 17 日
Please copy and paste the full error message to this question including the line number and code that is throwing the error.
Ahmad Sayah
Ahmad Sayah 2019 年 2 月 17 日
here is the error:
Undefined function or variable 'h0'.
Error in hw4 (line 1)
[Q,h,t,v]= func (h0,D,d,dt)
Geoff Hayes
Geoff Hayes 2019 年 2 月 17 日
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.
Ahmad Sayah
Ahmad Sayah 2019 年 2 月 17 日
Thanks so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by