Save output data in a vector format while loop
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Geoff Hayes
on 17 Feb 2019
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 Comments
Geoff Hayes
on 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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!