Need help setting up function and graphing

Im trying to write a code and graph it but i keep getting these following errors:
Error using odearguments (line 87)
The entries in tspan must strictly increase or decrease.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in SEMIBATCH_6_6graph (line 3)
[v,x]=ode45('SEMIBATCH_6_6',vspan,ic);
Im a bit new to Matlab and am slowly but surely figuring this all out.
% xdot(1)=dV/d(t)
% xdot(2)=d(Nc)/d(t)
% xdot(3)=d(Nb)/d(t)
% xdot(4)=d(Na)/d(t)
function xdot=SEMIBATCH_6_6(v,x)
Cbo = 1.5;
Fbo = 6;
Cao = 0.75;
Nao = 1125;
k = 5.1;
rho =1000;
MWCO2 = 44;
Cc=Nc/V;
X = (Nao-Na)/Nao;
Ca = Na/V;
Cb = Nb/V;
ra = -k*Ca*Cb;
vo = Fbo/Cbo;
FCO2 = -ra*V;
vCO2 = FCO2*MWCO2/rho;
Cd = Cc;
xdot(1,:) = vo-vCO2;
xdot(2,:) = -ra*V;
xdot(3,:) = Fbo+ra*V;
xdot(4,:) = ra*V;
end
Here is the code for the graphing. Any little bit helps
ic=[1125; 0; 0];
vspan= [1500, 1500, 2450.5, 2450.5];
[v,x]=ode45('SEMIBATCH_6_6',vspan,ic);
plot(v,x);
title('Semibatch with Moles as the Variables');
xlabel('V(Seconds)');
ylabel('Na,Nb,Nc');
ic=[.75; 0; 0];
xspan=[0,0,1,1];
[v,x]=ode45('SEMIBATCH_6_6',xspan,ic);
plot(v,x);
title('Semibatch with Moles as the Variables');
xlabel('V(Seconds)');
ylabel('Ca,Cb,Cc');

 Accepted Answer

Hi
First correction in your subfunction : there was missing code (see comments) : V, Nc, Nb, Na are not defined (how they are linked to x)
function xdot=SEMIBATCH_6_6(v,x)
% xdot(1,:)=dV/d(t)
% xdot(2,:)=d(Nc)/d(t)
% xdot(3,:)=d(Nb)/d(t)
% xdot(4,:)=d(Na)/d(t)
Cbo = 1.5;
Fbo = 6;
Cao = 0.75;
Nao = 1125;
k = 5.1;
rho =1000;
MWCO2 = 44;
%%%%%%%%%%%%%%%
% missing code : V, Nc, Nb, Na are not defined (how they are linked to x)
V = x(1,:);
Nc = x(2,:);
Nb = x(3,:);
Na = x(4,:);
%%%%%%%%%%%%%%%
Cc=Nc/V;
X = (Nao-Na)/Nao;
Ca = Na/V;
Cb = Nb/V;
ra = -k*Ca*Cb;
vo = Fbo/Cbo;
FCO2 = -ra*V;
vCO2 = FCO2*MWCO2/rho;
Cd = Cc;
xdot(1,:) = vo-vCO2;
xdot(2,:) = -ra*V;
xdot(3,:) = Fbo+ra*V;
xdot(4,:) = ra*V;
end
then there was some issue with time span and ic vector size . I could make the code to work, but I let you double check if it matches your expectations. I let you correct your second ode computation based on the suggestion below :
also I prefer to use t instead of v as time output of the ode45 function. This could lead to some confusion with your V variable
(that's why I was not sure either if you plot the data vs time t or vs V)
% ic=[1125; 0; 0]; % ic vector size (1x3) inconsistent with x vector in the ODE (4x1)
ic=[1125; 0; 0; 0]; % ic vector must be the same size as your x vector in the ODE (4x1)
% V, Nc, Nb, Na link to x :
% V = x(1,:);
% Nc = x(2,:);
% Nb = x(3,:);
% Na = x(4,:);
%%%%%%%%%%%%%%%
% vspan= [1500, 1500, 2450.5, 2450.5]; % twice too much variables
vspan= [1500, 2450.5]; % this is the time span ( t start , t end) so only 2 variables
[t,x]=ode45('SEMIBATCH_6_6',vspan,ic);
figure(1),
subplot(2,1,1),plot(t,x(:,1)); % only display first column of x (corresponding to V)
title('Plot vs time');
xlabel('time (Seconds)');
ylabel('V');
legend('V');
subplot(2,1,2),plot(t,x(:,2:4)); % only display last 3 columns of x (corresponding to Na,Nb,Nc)
title('Plot vs time');
xlabel('time (Seconds)');
ylabel('Na,Nb,Nc');
legend('Na','Nb','Nc');
figure(2),
plot(x(:,1),x(:,2:4));
title('Plot Na,Nb,Nc vs V');
xlabel('V');
ylabel('Na,Nb,Nc');
legend('Na','Nb','Nc');

More Answers (0)

Categories

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