Undefined variable t error

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

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.
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

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

Could you please explain how your definition of t differs from my original t definition because from what I can see they are identical. Thank yuo for the help with ode15s but the working outside of the function displays the error:
Error: File: dXdTT.m Line: 31 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "dXdTT".)
end
Is it possible that using the version R2018a (research lisense) is affecting my results?
Move the lines from t = linspace(0, 0.3); to the grid call, and put them all at the top of your dXdTT.m file.
Thanks Walter and Star Strider you've sorted out my problem, thank you very much
@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

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!