IF statement to fill a cell array base on conditions

조회 수: 1 (최근 30일)
Jestoni Orcejola
Jestoni Orcejola 2020년 8월 26일
답변: Jestoni Orcejola 2020년 9월 2일
I have an ODE function with time dependent variables, one of the time dependent variable I am trying to pass throug the equation is volume. I want to set a an if statement that will fill a cell array that contains volume change over time. What I am have trouble with is writing the proper if statement.
My goal is to change volume for a certain amount of time, in my case I am changing volume until I meet a threshold, then stay constant at that threshold for another prescribed time, then when I crossed a certain depth I want to change volume again until I meet the seond threshold. Once I create this cell array, I wan to pass it to my function.
Here is my code:
DEPTH=y(1);
%% Calculating Displaced Volume as a function of flowrate and time
%cubic meter
VBD_State=cell(length(time),1);
pumprate=23.3/1000000;
total_oil=2600/1000000;
new_total_oil=1300;
for b = 1:length(time)
if displaced_volume(time(b),pumprate)<=total_oil
VBD_State{b,1}=displaced_volume(time(b),pumprate);
end
if DEPTH ==900 && displaced_volume(time(c),pumprate)<=new_total_oil
VBD_State{b,1}=displaced_volume(time(c),pumprate);
else
VBD_State{(b),1}=total_oil;
end
end
end

답변 (2개)

Ayush Gupta
Ayush Gupta 2020년 9월 2일
Let’s say the two-volume threshold are volume_threshold1 and volume_threshold2, it can be done like this. Refer to the following code:
if(depth < depth_threshold)
if(current_volume < volume_threshold1)
change volume or do whtatever
end
else
if(current volume < colume_threshold2)
change the volume accordingly
end
end
And change the symbols accordingly to fit it.

Jestoni Orcejola
Jestoni Orcejola 2020년 9월 2일
DEPTH=y(1);
initial_float_volume=0.0939.*ones(length(time),1);
%% Calculating Displaced Volume as a function of flowrate and time
%cubic meter
VBD_State=cell(length(time),1);
pumprate=23.3/1000000;
total_oil=2600/1000000;
new_total_oil=1300;
for b = 1:length(time)
if(DEPTH < 900)
if displaced_volume(time(b),pumprate)<=total_oil
VBD_State{b,1}=displaced_volume(time(b),pumprate);
end
current_v= VBD_State{b,1};
else
if current_v>(.0939+new_total_oil)
VBD_State{b,1}=displaced_volume(time(c),pumprate);
end
end
end
The code above is terminating prematurely.
I need the code to do:
if y(1) < 900
change volume by adding the amount: first threshold
--there is a rate at which the volume changes until it meets the threshold--
then when y(1)=900
change volume by subtracting the amount: second threshold
I need the volume change to be saved in an array to pass through the equation below
%% Function for ODE45: Calculates Velocity
%V=dv;%0.0943912;
Cd=.16;
A=.0613;
m=95.976;
g=9.81;
%density=v(1);
format long g
%Volume as a function of BE
V=spline(time,Float_Volume,DEPTH);
rho=spline(xx,Seawater_Density_Profile,DEPTH) ;
%rho = spline(x1,descending_rho_profile,DEPTH);
speed=[y(2); (rho*V*g)/-(m+V*rho)+(m*g/(m+V*rho))-((1/(2*m+V*rho)*Cd*A.*rho.*y(2).^2))];

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by