Why am I getting the following errors
    4 views (last 30 days)
  
       Show older comments
    
For the following code, I am getting an error and I am unable to figure out why. Any help would be handy. 
%MATLAB     SYMBOL              DESCRIPTION                             IC                  Units
%x(1)               X                   Cell Concentration              0.5                 kmol/m3
%x(2)               S                   Substrate  Concentration        0.38                kmol/m3
%x(3)               P                   Product Concentration              0                kmol/m3
%x(4)               G                   Glycerol Concentration         0                kmol/m3
%x(5)               V                   Volume                             0                m3
ic = [0.5; 0.38; 0; 0; 0];
%MATLAB     PARAMETER       DESCRIPTION                                 Value           UNITS 
%mumax      mu_{max}            Maximum Growth Rate                     0.2             1/hour
%Ks         K_s                 Monod Constant                          2               kg/m3
%Yxs        Y_{X/S}             Cell Yield                              0.58            kg/kg
%Ypx        Y_{P/X}             Product Yield                           0.2             kg/kg
%Yxo        Y_{X/O}             Glycerol Yield 
%Sf         S_f                 Feed Substrate Concentration            0.37            kmol/m3
%Kp         K_p                 Product Inhibition                      97.9            kg/m3
mumax = 0.20;
Ks = 2;
Yxs = 0.58;
Ypx = 0.2; 
Yxo = 2.46;
Sf = 0.37;
Kp = 97.9;
mu = @(S,P) (mumax*S./(Ks + S))*(1 - (P/Kp))^0.5; % Monod Equation
rg = @(X,S,P) mu(S,P)*X; % Rate of cell growth
rp = @(X,S,P) Ypx*rg(X,S,P); % Rate of ethanol formation
rpg = @(X,S,P,G) Yxo*rg(X,S,P); % Rate of glycerol formation 
F = @(t) 2;
dXV = @(t, x) x(5) *rg(x(1),x(2),x(3));
dPV = @(t, x) x(5) *rp(x(1),x(2),x(3));
dSV = @(t, x) F(t)*Sf - x(5)*rg(x(1),x(2),x(3),x(4))/Yxs;
dGV = @(t, x) x(5) *rpg(x(1),x(2),x(3));
dV = @(t, x) F(t); 
dX = @(t, x) (dXV(t,x) - x(1)*dV(t,x))/x(5);
dS = @(t, x, s) (dSV(t,x) - x(2)*dV(t,x))/x(5);
dP = @(t, x, p) (dPV(t,x) - x(3)*dV(t,x))/x(5);
dG = @(t, x, g) (dGV(t,x) - x(4)*dV(t,x))/x(5);
f = @(t,x) [dX(t,x); dS(t,x,s); dP(t,x,p); dG(t,x,g); dV(t,x)];
tspan = [0 100];
[t,x] = ode45(f,tspan,ic);
subplot(2,2,1);
plot(t,x(:,1));
xlabel('Time (hr)');
ylabel('X (kmol/m3)');
title('Cell Concentration');
subplot(2,2,2);
plot(t,x,s(:,3));
xlabel('Time (hr)');
ylabel('P (kmol/m3 )');
title('Product Concentration');
subplot(2,2,3);
plot(t,x,p(:,2));
xlabel('Time (hr)');
ylabel('S (kmol/m3)');
title('Substrate Concentration');
subplot(2,2,4);
plot(t,x,g(:,4));
xlabel('Time (hr)');
ylabel('V (m3)');
title('Volume');
subplot(2,2,5);
plot(t,x,g,p(:,5));
xlabel('Time (hr)');
ylabel('Glycerol Concentration (kmol/m3)');
title('Glycerol');
The error is: 
Undefined function or variable 's'.
Error in @(t,x)[dX(t,x);dS(t,x,s);dP(t,x,p);dG(t,x,g);dV(t,x)]
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
  odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
  varargin);
2 Comments
  Stephen23
      
      
 on 17 Jan 2019
				"I am getting an error and I am unable to figure out why. Any help would be handy."
First read the error message:
Undefined function or variable 's'.
Error in @(t,x)[dX(t,x);dS(t,x,s);dP(t,x,p);dG(t,x,g);dV(t,x)]
Then look at that line of code:
f = @(t,x) [dX(t,x); dS(t,x,s); dP(t,x,p); dG(t,x,g); dV(t,x)];
Where is s defined?
Accepted Answer
  Torsten
      
      
 on 17 Jan 2019
        dS = @(t, x) (dSV(t,x) - x(2)*dV(t,x))/x(5);
dP = @(t, x) (dPV(t,x) - x(3)*dV(t,x))/x(5);
dG = @(t, x) (dGV(t,x) - x(4)*dV(t,x))/x(5);
f = @(t,x) [dX(t,x); dS(t,x); dP(t,x); dG(t,x); dV(t,x)];
0 Comments
More Answers (2)
  Jan
      
      
 on 17 Jan 2019
        
      Edited: Jan
      
      
 on 17 Jan 2019
  
      This is strange:
dS = @(t, x, s) (dSV(t,x) - x(2)*dV(t,x))/x(5);
dP = @(t, x, p) (dPV(t,x) - x(3)*dV(t,x))/x(5);
dG = @(t, x, g) (dGV(t,x) - x(4)*dV(t,x))/x(5);
You do not use s, p, g inside the functions, so why do you provide them?
f = @(t,x) [dX(t,x); dS(t,x,s); dP(t,x,p); dG(t,x,g); dV(t,x)];
Or, better: You do not provide them. Here the variabels s,p,g are not defined. Although they are not used later, Matlab must assign a value to them, when the anonymous function is evaluated.
The exhaustive use of nested anonymous function is hard to debug. I prefer to use standard functions and to use anonymous functions only to provide the parameters. Then I can simply set a breakpoint in the debugger to see what's going on.
A hint: ^0.5 is more expensive than sqrt.
0 Comments
  Jesús Zambrano
    
 on 17 Jan 2019
        Hi,
One thing I saw is that you define rg with three variables, but then you say:
dSV = @(t, x) F(t)*Sf - x(5)*rg(x(1),x(2),x(3),x(4))/Yxs;
where it uses 4 variables. This still cannot explain your error message but is something that would be good to look at.
2 Comments
  Jesús Zambrano
    
 on 22 Jan 2019
				Hi!,
Since you want to show 5 plots using the subplot command, then try subplot(3,2,1), ..., subplot(3,2,5). In other words:
subplot(3,2,1);
plot(t,x(:,1));
xlabel('Time (hr)');
ylabel('X (kmol/m3)');
title('Cell Concentration');
subplot(3,2,2);
plot(t,x(:,3));
xlabel('Time (hr)');
ylabel('P (kmol/m3 )');
title('Product Concentration');
subplot(3,2,3);
plot(t,x(:,2));
xlabel('Time (hr)');
ylabel('S (kmol/m3)');
title('Substrate Concentration');
subplot(3,2,4);
plot(t,x(:,4));
xlabel('Time (hr)');
ylabel('Glycerol Concentration (kmol/m3)');
title('Glycerol');
subplot(3,2,5);
plot(t,x(:,5));
xlabel('Time (hr)');
ylabel('V (m3)');
title('Volume'); 
See Also
Categories
				Find more on Parallel for-Loops (parfor) 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!



