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

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 17 Feb. 2019

0 Stimmen

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 Kommentare

Ahmad Sayah
Ahmad Sayah am 17 Feb. 2019
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 am 17 Feb. 2019
Bearbeitet: Geoff Hayes am 17 Feb. 2019
Ahmad - you may have a typo. Is the variable named ho or h0 (the letter 'o' vs the number 0)?
Ahmad Sayah
Ahmad Sayah am 17 Feb. 2019
Thanks for the quick response!
but all are h0
Geoff Hayes
Geoff Hayes am 17 Feb. 2019
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 am 17 Feb. 2019
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 am 17 Feb. 2019
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 am 17 Feb. 2019
Thanks so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by