'Undefined Function or Variable' Error when Function is Defined

My model contains two functions, cp(t) and ct(t). In my original (working) code (see below), cp(t) and ct(t) are each defined by one functional form. Now, I would like to study the behavior of the dependent variables for different functional forms of cp(t) and ct(t). How can I do this?
My Attempt: I created two matrices in the ODE solver; in the first matrix, each column represents a function for cp(t), and in the second matrix, each column represents a function for ct(t). When I run the ODE solver, I get the error "Undefined Function or Variable" for cp(t).
Thank you!
-----------------------------------------------
ORIGINAL, WORKING CODE
ODE File:
function dy = short_ODE(t,y)
b = 21.5;
d = 0.167;
qe = 0.0542;
q1 = 0.000257;
q2 = 0.0008;
dy(1,1) = 10*b*y(9)*(cp(t)/(cp(t)+ct(t)))-ne(t)*y(1)-qe*y(1);
dy(2,1) = ne(t)*y(1)-n1(t)*y(2)-f1(t)*y(2)-q1*(y(2)^2)/cp(t);
dy(3,1) = n1(t)*y(2)-n2(t)*y(3)-f2(t)*y(3)-q2*(y(3)^2)/cp(t);
dy(4,1) = n2(t)*y(3)-np(t)*y(4)-fp(t)*y(4);
dy(5,1) = np(t)*y(4)/2-(1+d)*y(5)+y(9);
dy(6,1) = -(1+d)*y(6)+y(5);
dy(7,1) = -(1+d)*y(7)+y(6);
dy(8,1) = -(1+d)*y(8)+y(7);
dy(9,1) = -(1+d)*y(9)+y(8);
function y = T(t)
T0 = 21.5;
y = T0 + 0.6346*cos(t*2*pi/365) + 0.7731*sin(t*2*pi/365);
end
% Nested cp(t)
function y = cp(t)
y = 135000-35000*(1.446*cos(t*2*pi/365)+0.7109*sin(t*2*pi/365)...
+1.347*cos(2*t*2*pi/365)+1.408*sin(2*t*2*pi/365)...
-0.9942*cos(3*t*2*pi/365)-0.2396*sin(3*t*2*pi/365));
end
% Nested ct(t)
function y = ct(t)
epsilon = 1e-10;
y = 0*t + epsilon;
end
% Nested ne
function y = ne(t)
y = 0.693/(1.10316852+12.55262944/(1+(T(t)/15.21894679)^6.468691289));
end
% Nested n1
function y = n1(t)
y = 0.693/(2.711278535+18.35202441/(1+(T(t)/15.61912844)^5.845513817));
end
% Nested f1
function y = f1(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
% Nested n2
function y = n2(t)
y = 0.693/(4.244561306+136.3457233/(1+(T(t)/10.3599642)^4.783523801));
end
% Nested f2
function y = f2(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
% Nested np
function y = np(t)
y = 0.693/(8.583819474+18.15593517/(1+(T(t)/21.5533767)^7.852462638));
end
% Nested fp
function y = fp(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
end
ODE Solver:
clear all;
tf = 1000;
dt = 1;
t = 0:dt:tf;
y0 = [3954200.8835438923;2310300.5386356956;2007000.7121853685;
103780.6956163843;1289400.7821495022/100/5;
1289400.7821495022/100/5;1289400.7821495022/100/5;
1289400.7821495022/100/5;1289400.7821495022/100/5];
[t,y] = ode15s(@short_ODE,t,y0);
A = y(:,5)+y(:,6)+y(:,7)+y(:,8)+y(:,9);
plot(t,A,'b');
xlabel('Time (in days)');
ylabel('Population');
h_xlabel = get(gca,'XLabel');
set(h_xlabel,'FontSize',14);
h_ylabel = get(gca,'YLabel');
set(h_ylabel,'FontSize',14);
set(gca,'fontName','Helvetica');
MODIFIED CODE WITH ERROR
ODE Solver:
clear;
tf = 1000;
dt = 1;
t = 1:dt:tf;
y0 = [3954200.8835438923;2310300.5386356956;2007000.7121853685;
103780.6956163843;1289400.7821495022/100/5;
1289400.7821495022/100/5;1289400.7821495022/100/5;
1289400.7821495022/100/5;1289400.7821495022/100/5];
m = min(c(t));
d = m/10;
i = 0;
for p = 0:d:m % This is where I created matrices cp(:,i) and ct(:,i)
i = i + 1;
a(1:length(c(t))) = p;
cp(:,i) = transpose(a); %#ok<*SAGROW>
ct(:,i) = transpose(c(t)) - transpose(a);
end
for j = 1:11
calle1 = @(t,y) e1_ODE(t,y,j);
[t,y] = ode15s(calle1,t,y0);
A(:,j) = y(:,5)+y(:,6)+y(:,7)+y(:,8)+y(:,9);
plot(t,A,'b');
xlabel('Time (in days)');
ylabel('Population');
h_xlabel = get(gca,'XLabel');
set(h_xlabel,'FontSize',14);
h_ylabel = get(gca,'YLabel');
set(h_ylabel,'FontSize',14);
set(gca,'fontName','Helvetica');
hold on
end
% Nested c(t)
function y = c(t)
y = 135000-35000*(1.446*cos(t*2*pi/365)+0.7109*sin(t*2*pi/365)...
+1.347*cos(2*t*2*pi/365)+1.408*sin(2*t*2*pi/365)...
-0.9942*cos(3*t*2*pi/365)-0.2396*sin(3*t*2*pi/365));
end
ODE File:
function dy = e1_ODE(t,y,j) % Here, I have index as an input, which wasn't needed
% for the working code
b = 21.5;
d = 0.167;
qe = 0.0542;
q1 = 0.000257;
q2 = 0.0008;
epsilon = 1e-10;
dy(1,1) = 10*b*y(9)*(cp(t,j)/(cp(t,j)+ct(t,j)))-ne(t)*y(1)-qe*y(1);
dy(2,1) = ne(t)*y(1)-n1(t)*y(2)-f1(t)*y(2)-q1*(y(2)^2)/cp(t,j)+epsilon;
dy(3,1) = n1(t)*y(2)-n2(t)*y(3)-f2(t)*y(3)-q2*(y(3)^2)/cp(t,j)+epsilon;
dy(4,1) = n2(t)*y(3)-np(t)*y(4)-fp(t)*y(4);
dy(5,1) = np(t)*y(4)/2-(1+d)*y(5)+y(9);
dy(6,1) = -(1+d)*y(6)+y(5);
dy(7,1) = -(1+d)*y(7)+y(6);
dy(8,1) = -(1+d)*y(8)+y(7);
dy(9,1) = -(1+d)*y(9)+y(8);
function y = T(t)
T0 = 21.5;
y = T0 + 0.6346*cos(t*2*pi/365) + 0.7731*sin(t*2*pi/365);
end
% Nested ne
function y = ne(t)
y = 0.693/(1.10316852+12.55262944/(1+(T(t)/15.21894679)^6.468691289));
end
% Nested n1
function y = n1(t)
y = 0.693/(2.711278535+18.35202441/(1+(T(t)/15.61912844)^5.845513817));
end
% Nested f1
function y = f1(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
% Nested n2
function y = n2(t)
y = 0.693/(4.244561306+136.3457233/(1+(T(t)/10.3599642)^4.783523801));
end
% Nested f2
function y = f2(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
% Nested np
function y = np(t)
y = 0.693/(8.583819474+18.15593517/(1+(T(t)/21.5533767)^7.852462638));
end
% Nested fp
function y = fp(t)
y = 0.666733-0.056977*T(t)+0.00125224*T(t)^2;
end
end

 Accepted Answer

Looks like the issue is you define cp in the workspace and the function can't access that variable when it gets called. So, try passing cp to the ode function:
calle1 = @(t,y) e1_ODE(t,y,j,cp);

6 Comments

Thank you for the suggestion. I passed cp(t) and ct(t) to the ode function:
calle1 = @(t,y) e1_ODE(t,y,j,cp,ct);
The "Undefined Function or Variable" error is gone. However, now the error "Subscript indices must either be real positive integers or logicals" comes up. The error is in the following line:
dy(1,1) = 10*b*y(9)*(cp(t,j)/(cp(t,j)+ct(t,j)))-ne(t)*y(1)-qe*y(1);
The variables t and j are both greater than 0. What else could be causing this error?
Thank you!
You use the same names for both functions and for variables, which you even noted in your code comments:
This is where I created matrices cp(:,i) and ct(:,i)
You need to rename the variables to use different names to the functions.
a_k's "Answer" moved here:
Thank you for the suggestion, Stephen. I am unsure about which variables to rename. Should I rename cp(t,j) and ct(t,j) in the ODE file?
@a_k: The basic problem is that in your original DOE file both cp and ct were functions, whereas inside the modified version they are variables (arrays). Once you pass them as Josh Meyer suggested then they are arrays and you need to consider that these need indexing and not input values as the functions did.
This is an unfortunate weakness of MATLAB that the function-calling and array-indexing syntax is identical.
You need to consider where those variables are defined!
EDIT: Actually your approach is basically flawed, because the ODE solver requires cp and ct to be functions of t so that they return correct values for the solver's t values. But when you supply them as arrays then the ODE solver cannot provide arbitrary values of t and y that it would use to numerically solve this ODE, because the arrays are simply fixed sets of values: these can be accessed using indexing, but will not provide the correct values for any arbitrary t values.
One solution would be to pass function handles rather than arrays. You could then define the function handle in whatever way required, and then the ODE solver would use this. Simply pass the handles exactly as Josh Meyer showed.
@Stephen Cobeldick: I reviewed my code (Modified Code with Error, not the Original Working Code). I define functions cp and ct in the ODE Solver. In the ODE File, I use the functions in the differential equations dy(1,1), dy(2,1), and dy(3,1) --- is this where I am defining variables cp and ct?
@Stephen Cobeldick: Thank you for recommending function handles! My code is working as intended.

Sign in to comment.

More Answers (0)

Asked:

on 2 Nov 2017

Edited:

on 22 Dec 2017

Community Treasure Hunt

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

Start Hunting!