Recieving error Index exceeds the number of array elements (1) when attempting to useODE45?

I am trying to produce a solution to a pair of ODE's that model a CSTR. When the function is run it gives the error Index exceeds the number of array elements (1). I have attempted to change the array dimensions to allow for the undex but have not been succesful.
function dt=tutorial3(y,t)
F=1;
DH=-5960;
pCp=500;
UA=150;
V=1;
Ti=298;
Tj=298;
k0=9703*3600;
Cai=1;
R=1.987;
E=11843;
dt=zeros(2,1);
dt(1)=((F*Cai)-(F*y(1))-(k0*exp(-E/(R*y(2))))*V)/V;
dt(2)=F*pCp*(Ti-y(2))+(-DH)*(k0*exp(-E/(R*y(2))))*V-UA*(y(2)-Tj);
end
tspan=[0 100];
y0=[5;350];
[t,y]=ode45(@tutorial3,tspan,y0);
plot(t,y)

 Accepted Answer

%SCRIPT FILE:
tspan=[0 100];
y0=[5;350];
[t,y]=ode45(@tutorial3,tspan,y0);
plot(t,y(:,1),'-or')
hold on
plot(t,y(:,2),'-ob')
%FUNCTION FILE:
function dt=tutorial3(t,y)
F=1;
DH=-5960;
pCp=500;
UA=150;
V=1;
Ti=298;
Tj=298;
k0=9703*3600;
Cai=1;
R=1.987;
E=11843;
dt=zeros(2,1000);
dt(1)=((F*Cai)-(F*y(1))-(k0*exp(-E/(R*y(2))))*V)/V;
dt(2)=F*pCp*(Ti-y(2))+(-DH)*(k0*exp(-E/(R*y(2))))*V-UA*(y(2)-Tj);
dt = [dt(1);dt(2)];
end

3 Comments

is the above code working as per your requirement?
Yes it has solved my problem, thank you very much. Can i ask what the modifications you have made to the plot section of the script will do?
plot(t,y(:,1),'-or')
hold on
plot(t,y(:,2),'-ob')
y is column vector with two columns so the first plot represents first column and the second plot the second column

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!