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

2 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by