Why am I getting the following errors

1 Ansicht (letzte 30 Tage)
em246
em246 am 17 Jan. 2019
Kommentiert: Jesús Zambrano am 22 Jan. 2019
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 Kommentare
Stephen23
Stephen23 am 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?
Jan
Jan am 17 Jan. 2019
Bearbeitet: Jan am 17 Jan. 2019
I've formatted your code today to improve the readability. Do you see the bar on top of the field to edit your message? Please use it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 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)];

Weitere Antworten (2)

Jan
Jan am 17 Jan. 2019
Bearbeitet: Jan am 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.

Jesús Zambrano
Jesús Zambrano am 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 Kommentare
em246
em246 am 17 Jan. 2019
I had noticed earlier and corrected that, thank you!
Now the code is working however only 4 plots appear not 5. My adjusted code is below, and the 'volume' subplot is not coming up. Is it something in the code thats wrong? Thanks in advance.
%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 1 m3
ic = [0.5; 0.38; 0; 0; 5];
%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 2.46 kg/kg
%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))*sqrt(1 - (P/Kp)); % 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) Yxo*rg(X,S,P); % Rate of glycerol formation
F = @(t) 5;
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(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) (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)];
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(:,3));
xlabel('Time (hr)');
ylabel('P (kmol/m3 )');
title('Product Concentration');
subplot(2,2,3);
plot(t,x(:,2));
xlabel('Time (hr)');
ylabel('S (kmol/m3)');
title('Substrate Concentration');
subplot(2,2,4);
plot(t,x(:,4));
xlabel('Time (hr)');
ylabel('Glycerol Concentration (kmol/m3)');
title('Glycerol');
subplot(2,2,5);
plot(t,x(:,5));
xlabel('Time (hr)');
ylabel('V (m3)');
title('Volume');
Jesús Zambrano
Jesús Zambrano am 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');

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by