필터 지우기
필터 지우기

How to create a matrix with while loop?

조회 수: 17 (최근 30일)
Teb Keb
Teb Keb 2022년 2월 15일
댓글: Teb Keb 2022년 2월 15일
I have a problem where I have to calculate the time it will take for a fluid to leave a container using while loop and give the results in matrix array at every 0.10 second increments. This is the code I have so far:
v0=20;
A=3000;
t = 0;
m = [];
while vol<0
t=t+0.10;
vol=A-vol0*t;
m = [m;t,vol];
end
Basically my matrix should be something like:
%t %vol
0 2000
0.10 1000
0.20 500
t 0
% until vol becomes 0 and I want to find the time t when it becomes 0.
But somehow my matrix is 0x0. I can only create a matrix if I set the time to be known.
I know that it is inefficient to use while loop for this case. But I want to learn to create a matrix from while loop using this example.
Thank you,

채택된 답변

KSSV
KSSV 2022년 2월 15일
vol=20;
A=3000;
t = 0;
m = zeros([],2) ;
iter = 0 ;
while vol>0
iter = iter+1;
t=t+0.10;
vol=A-vol*t;
m(iter,:) = [t,vol];
end
m
  댓글 수: 3
Torsten
Torsten 2022년 2월 15일
편집: Torsten 2022년 2월 15일
Because fluid can only leave the container as long as (while) the fluid volume is > 0, not < 0.
Teb Keb
Teb Keb 2022년 2월 15일
oh i see. thank you.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by