error: x(2): out of bound 1

3 views (last 30 days)
I have code of the following. I did everything I could do, but couldnt overcome this error.
When I execute the below function I get the error
function [disp_dot]=stage2(x,t)%Stage_2 calculation for displacement and pressure
m=23.5; %Stage_2:From bottom choked till bottom is exhausted; top is exhausted
ODB=0.122;
IDB=0.04625;
g=9.8;
ODT=0.104;
IDT=0.042;
Patm=100000;
dt=0.0001; %time increment value
L=0.11; %This is the length where adiabatic condition starts in top chamber; the length of the leg of the air distributor
AT = pi*(ODT^2-IDT^2)/4;
AB = pi*(ODB^2-IDB^2)/4;
PT=Patm;
ODSBD=0.1285;
IDSBD=0.122;
ASBD=pi*(ODSBD^2-IDSBD^2)/4;
ASB=0.00451183;
AST=0.001267892;
CFM=840; %air inflow
CFMm=CFM*(1/2118.88);
v=CFMm/AB;
v=5;
PST=Patm;
PSB=Patm;
PT=Patm;
LB=0.0259; %This is the length where bottom chamber becomes adiabatic during upward displacement of the piston
PBinitial=((PT*AT)+(m*g)+(PST*AST)-(PSB*ASB))/AB;
KB=PBinitial*(AB*LB)^1.4;
disp_dot(1) = x(2);
disp_dot(2) = (1/m)*((KB*AB/(0.000058402+((ASBD+AB)*x(1))^1.4)-(PT*AT)-(m*g)+(PSB*ASB)-(PST*AST)); %Constant value is from bottom channel 4 in excel
end

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 13 Mar 2019
Edited: KALYAN ACHARJYA on 13 Mar 2019
Issue 1:
When you defined the function as
[disp_dot]=stage2(x,t)
There is not role of t function input data t in the function.
Issue 2:
In the following one parenthesis bracket required at last.
disp_dot(2)=(1/m)*((KB*AB/(0.000058402+((ASBD+AB)*x(1))^1.4)-(PT*AT)-(m*g)+(PSB*ASB)-(PST*AST)));
Now I have defined the function as
function [disp_dot]=stage2(x)%Stage_2 calculation for displacement and pressure
m=23.5; %Stage_2:From bottom choked till bottom is exhausted; top is exhausted
ODB=0.122;
IDB=0.04625;
g=9.8;
ODT=0.104;
IDT=0.042;
Patm=100000;
dt=0.0001; %time increment value
L=0.11; %This is the length where adiabatic condition starts in top chamber; the length of the leg of the air distributor
AT=pi*(ODT^2-IDT^2)/4;
AB=pi*(ODB^2-IDB^2)/4;
PT=Patm;
ODSBD=0.1285;
IDSBD=0.122;
ASBD=pi*(ODSBD^2-IDSBD^2)/4;
ASB=0.00451183;
AST=0.001267892;
CFM=840; %air inflow
CFMm=CFM*(1/2118.88);
v=CFMm/AB;
v=5;
PST=Patm;
PSB=Patm;
PT=Patm;
LB=0.0259; %This is the length where bottom chamber becomes adiabatic during upward displacement of the piston
PBinitial=((PT*AT)+(m*g)+(PST*AST)-(PSB*ASB))/AB;
KB=PBinitial*(AB*LB)^1.4;
disp_dot(1)=x(2);
disp_dot(2)=(1/m)*((KB*AB/(0.000058402+((ASBD+AB)*x(1))^1.4)-(PT*AT)-(m*g)+(PSB*ASB)-(PST*AST))); %Constant value is from bottom channel 4 in excel
end
When I ran the code here is the output
>> y1=[1,2]
y1 =
1 2
>> y=stage2(y1)
y =
2.0000 -26.1197
Please note in the fuction code x define as 2 length vector, which require x(1) and x(2), that why I have passed the y=[valuue1 value 2] to function
Hope it helps!
  2 Comments
Lakshmikruthiga Ponnusamy
Lakshmikruthiga Ponnusamy on 13 Mar 2019
Thank you so much sir. It worked. I also have other question. Can you help on this?
I want the graph to be colored in such a way that stage one is in blue and stage 2 is in red in displ vs time graph, similarly for velocity vs time and pressure vs time graph. I have posted image as example. I want the graph to be like that in image. How do i write the code?
displ=zeros(100,2);
tim=zeros(100,1);
pressure=ones(100,1);
velocity=ones(100,1);
for i=2:100
v=5;
dt=1*10^-4;
function [displ_f,pressure]=stage1(displ_i)
dt=1*10^-4;
v=5;
area=5;
displ_f=displ_i+v*dt;
pressure=area*displ_f;
velocity=10*area*displ_f;
end
function [displ_f]=stage2(displ_i)
dt=1*10^-4;
v=5;
area=5;
displ_f=displ_i+v*dt;
pressure=area*displ_f;
velocity=10*area*displ_f;
end
if (displ(i-1,1)<0.020 && displ(i-1,2)>=0)
disp("IN stage 1");
[displ(i,1)]=stage1(displ(i-1,1));
displ(i,2)=v;
tim(i,1)=tim(i-1)+dt;
elseif (displ(i-1,1)>=0.020 && displ(i-1,1)<=0.0256 && displ(i-1,2)>=0)
disp("IN stage 2");
[displ(i,1)]=stage2(displ(i-1,1));
displ(i,2)=displ(i-1,2);
tim(i,1)=tim(i-1)+dt;
end
end
plot(tim(:,1),displ(:,1));
plot(tim(:,1),pressure);
plot(tim(:,1),velocity);
KALYAN ACHARJYA
KALYAN ACHARJYA on 13 Mar 2019
Edited: KALYAN ACHARJYA on 13 Mar 2019
For the 2nd question, please explain the issue in more generalise and simpler way? Or you can ask the question as new question (new thread), so that others can easily see the question and get the fast answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables 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!