Undefined variable t error

3 views (last 30 days)
Will Seccombe
Will Seccombe on 17 Mar 2019
Commented: Star Strider on 17 Mar 2019
I am trying to to model enzyme kinetics but MATLAB is stating I have an undefined variable t even though it is defined later in the code. Please could someones pont me in the right direction please
function [ f ] = dXdT(t,x)
e = x(1);
cc = x(2);
ac = x(3);
cb = x(4);
g = x(5);
t = [0 100];
kc1 = 36; %all kc and kd values in h-1
kc2 = 40000;
kc3 = 79200;
kc4 = 8.28e-8;
kd = 0.0227;
E0 = 1; %all these values need to be in g/L
km1 = 1;
km2 = 1;
km3 = 1;
KI1 = 1;
KI2 = 1;
KI3 = 1;
KI4 = 1;
KI5 = 1;
initialcon = [1 0.76 0.24 0.09 0];
dEdt = E0*exp(-kd*t);
dCCdt = -(kc1*e*cc)/(km1*(1+(g/KI1)+(cb/KI2))+cc);
dACdt = (kc1*e*cc)/(km1*(1+(g/KI1)+(cb/KI2))+cc)-(kc2*e*ac)/(km2*(1+(g/KI3)+(cb/KI4))+ac);
dCBdt = (kc2*e*ac)/(km2*(1+(g/KI3)+(cb/KI4))+ac)-(kc3*e*cb)/(km3*(1+(g/KI5))+cb);
dGdt = (kc3*e*cb)/(km3*(1+(g/KI5))+cb)-(kc4*g);
dFdt = kc4*g;
f = [dEdt; dCCdt; dACdt; dCBdt; dGdt; dFdt;];
dXdT([1 0.76 0.24 0.09 0]); %initial concentrations of the 3 compounds
[t,x] = ode45(@dXdT, t, initialcon, @dXdTplot); %function, time frame, initial condition
end
  2 Comments
Walter Roberson
Walter Roberson on 17 Mar 2019
In order to run that code you would need to pass in exactly two parameters.
The dXdT([1 0.76 0.24 0.09 0]) line at the bottom of the code attempts to call the exact function you are defining, which would be a recursive call, but it is making the call with only one parameter (that look like x values) rather than 2 parameters. The recursive call would fail as soon as it tried to use x(1) since x is not passed in to the call.
Will Seccombe
Will Seccombe on 17 Mar 2019
Is this what you were suggesting? I have made these changes but t i still undefined
function [ f ] = dXdT(t,v,w,x,y,z)
e = v;
cc = w;
ac = x;
cb = y;
g = z;
0:100 = t;
kc1 = 36; %all kc and kd values in h-1
kc2 = 40000;
kc3 = 79200;
kc4 = 8.28e-8;
kd = 0.0227;
E0 = 1; %all these values need to be in g/L
km1 = 1;
km2 = 1;
km3 = 1;
KI1 = 1;
KI2 = 1;
KI3 = 1;
KI4 = 1;
KI5 = 1;
initialcon = [1 0.76 0.24 0.09 0]; %initial concentrations of components
dEdt = E0*exp(-kd*t);
dCCdt = -(kc1*e*cc)/(km1*(1+(g/KI1)+(cb/KI2))+cc);
dACdt = (kc1*e*cc)/(km1*(1+(g/KI1)+(cb/KI2))+cc)-(kc2*e*ac)/(km2*(1+(g/KI3)+(cb/KI4))+ac);
dCBdt = (kc2*e*ac)/(km2*(1+(g/KI3)+(cb/KI4))+ac)-(kc3*e*cb)/(km3*(1+(g/KI5))+cb);
dGdt = (kc3*e*cb)/(km3*(1+(g/KI5))+cb)-(kc4*g);
dFdt = kc4*g;
f = [dEdt; dCCdt; dACdt; dCBdt; dGdt; dFdt;];
[t,x] = ode45(@dXdT, t, initialcon, @dXdTplot); %function, time frame, initial condition
end

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 17 Mar 2019
Try this:
function [ f ] = dXdT(t,x)
e = x(1);
cc = x(2);
ac = x(3);
cb = x(4);
g = x(5);
t = [0 100];
kc1 = 36; %all kc and kd values in h-1
kc2 = 40000;
kc3 = 79200;
kc4 = 8.28e-8;
kd = 0.0227;
E0 = 1; %all these values need to be in g/L
km1 = 1;
km2 = 1;
km3 = 1;
KI1 = 1;
KI2 = 1;
KI3 = 1;
KI4 = 1;
KI5 = 1;
dEdt = E0*exp(-kd*t);
dCCdt = -(kc1*e*cc)./(km1*(1+(g/KI1)+(cb/KI2))+cc);
dACdt = (kc1*e*cc)./(km1*(1+(g/KI1)+(cb/KI2))+cc)-(kc2*e*ac)./(km2*(1+(g/KI3)+(cb/KI4))+ac);
dCBdt = (kc2*e*ac)./(km2*(1+(g/KI3)+(cb/KI4))+ac)-(kc3*e*cb)./(km3*(1+(g/KI5))+cb);
dGdt = (kc3*e*cb)./(km3*(1+(g/KI5))+cb)-(kc4*g);
dFdt = kc4*g;
% SZ = [size(dEdt); size(dCCdt); size(dACdt); size(dCBdt); size(dGdt); size(dFdt)]
f = [dEdt(2); dCCdt; dACdt; dCBdt; dGdt; dFdt;];
end
t = linspace(0, 0.3);
% dXdT([1 0.76 0.24 0.09 0]); %initial concentrations of the 3 compounds
initialcon = [1 0.76 0.24 0.09 0 0];
[t,x] = ode15s(@dXdT, t, initialcon, @dXdTplot); %function, time frame, initial condition
figure
plot(t, x)
grid
Actually, you didn’t define ‘t’. I did here.
There were other problems that I corrected so it would run, including changing the solver from ode45 (that got stuck) to ode15s (since yours is a ‘stiff’ problem). (I ran it inside a test function I use for Answers questions, and in ran successfully in that context.)
Experiment to get the result you want.
  4 Comments
Will Seccombe
Will Seccombe on 17 Mar 2019
Thanks Walter and Star Strider you've sorted out my problem, thank you very much
Star Strider
Star Strider on 17 Mar 2019
@Will Seccombe —
Could you please explain how your definition of t differs from my original t definition because from what I can see they are identical.
Although not identically placed. Your definition is inside your ODE function. I closed the ODE function (witha separate end call), and put everything else after it. The order and location of assignments are important.
Do what Walter suggests, or save your ODE function as ‘dXdT.m’ somewhere on your MATLAB search path, and run everything from my linspace call to the grid call in a separate script.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!