How to iterate an if statement condition and store values in separate columns

조회 수: 1 (최근 30일)
Joy Shen
Joy Shen 2023년 3월 1일
답변: DUY Nguyen 2023년 3월 2일
I'm trying to calculate Esim for different boundary conditions on Fsim. The first being if Fsim is less than n_ins, the second if Fsim is between n_ins and n_tot, and the third if Fsim is above n_tot.
However, I want to have three different values of n_tot. Is there a way to calculate this piecewise function, where one of the boundary conditions has three different values? I essentially would like Esim to have three columns of data for each n_tot(i).
This is what I have so far:
Qsim=[]; Qsim=Cd.*Asim.*sqrt(2*g*(Fsim-n_ins))*Dsim;
Vsim=[]; Vsim=Qsim.*Dsim; %ft^3
n_tot=[12 13 14];
for i=1:1:3
if (Fsim < n_ins)
Esim = 0;
elseif (Fsim >= n_ins & Fsim < n_tot(i))
Esim(:,i)=Vsim./(int_L*int_W);
elseif (Fsim>= n_tot(i))
Esim = int_L*int_W*int_H; % make sure this is here
end
end

답변 (1개)

DUY Nguyen
DUY Nguyen 2023년 3월 2일
Hi,
You can try something like this! Esim will have three columns of data for each n_tot(i)
Qsim = Cd.*Asim.*sqrt(2*g*(Fsim-n_ins))*Dsim;
Vsim = Qsim.*Dsim; %ft^3
n_tot = [12 13 14];
Esim = zeros(length(Vsim), length(n_tot));
for i = 1:length(n_tot)
if (Fsim < n_ins)
Esim(:,i) = 0;
elseif (Fsim >= n_ins && Fsim(j) < n_tot(i))
Esim(:,i) = Vsim(j)/(int_L*int_W);
elseif (Fsim >= n_tot(i))
Esim(:,i) = int_L*int_W*int_H;
end
end

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by