Delay differential equation - Error: Function definition not supported in this context. Create functions in code file.

2 views (last 30 days)
I am trying to solve delay differential equations. I am using matlab online. I can not figure out what is wrong in my code. Could someone please help me?! thanks.
function ddex2
p.alpha_m = 1e-2;
p.alpha_M = 7e-2;
p.beta_c = 3e-2;
p.beta_e = 3e-2;
p.s = 5e-4;
p.rho = 5e-3;
p.gamma = 0.01;
p.lamb = 100;
p.kappa = 1.5;
p.nu = 167e-7;
p.epsilon = 0.0001;
tau = 40;
x_c0 = 1000;
x_e0 = 1;
B10 = 300;
B20 = 0;
%history = [x_c0; x_e0; B10; B20];
tau = 40;
function s = ddex2hist(t)
% Constant history function for DDEX1.
s = ones(1000,1,300,0);
sol = dde23(@ddex2de,tau,@ddex1hist,[0, 1000]);
figure
plot(sol.x,sol.y(2,:))
title('Time delay')
xlabel('time t')
ylabel('x_e(t)')
% -----------------------------------------------------------------------
%
function dydt = ddex2de(y,Z)
% Differential equations function for DDEX2.
w_th = p.rho*x_c + p.kappa*p.rho*x_e;
if w_th ~= 0
C_c = min(1, w/w_th)*p.s*x_c;
C_e = min(1, w/w_th)*p.kappa*p.s*x_e;
else
C_c = p.s*x_c;
C_e = p.kappa*p.s*x_e;
if (x_c ~= 0) && (p.s ~= 0)
alpha_c = p.alpha_m + max(array([0, 1 - C_c/(p.s*x_c)]))*(p.alpha_M - p.alpha_m);
else
alpha_c = p.alpha_m;
if (x_e ~= 0) && (p.s ~= 0)
alpha_e = p.alpha_m + max(array([0, 1 - C_e/(p.s*x_e)]))*(p.alpha_M - p.alpha_m);
else
alpha_e = p.alpha_m;
end
ylag = Z(:,2);
x_etau = ylag(2);
x_c = y(1);
x_e = y(2);
B1 = y(3);
B2 = y(4);
dx_cdt = (p.beta_c - alpha_c)*x_c;
dx_edt = (p.beta_e*x_etau) - (alpha_e*x_e);
dB1dt = p.gamma*B1*(p.lamb - B1) - p.nu*x_c*B1;
dB2dt = p.nu*x_c*B1 - C_c - C_e - p.epsilon*B2;
dydt = [ dx_cdt
dx_edt
dB1dt
dB2dt];
end
% -----------------------------------------------------------------------
end % ddex2

Answers (1)

Sahithi Metpalli
Sahithi Metpalli on 13 Mar 2020
Hi,
I assume that you are creating the function from MATLAB command prompt and it is not supported.
You can create the function in MATLAB Online as follows:
Go to Home tab and select New Script and write the function in the script file with the file name same as the function
  1 Comment
Naghmeh Akhavan
Naghmeh Akhavan on 13 Mar 2020
Hello Sahithi, Thank you for response. I have changed the name of function and saved the file with same name. But after runnig, I have no result or even error. Should I do something else? Thanks!
function delayfunc
p.alpha_m = 1e-2;
p.alpha_M = 7e-2;
p.beta_c = 3e-2;
p.beta_e = 3e-2;
p.s = 5e-4;
p.rho = 5e-3;
p.gamma = 0.01;
p.lamb = 100;
p.kappa = 1.5;
p.nu = 167e-7;
p.epsilon = 0.0001;
tau = 40;
%x_c0 = 1000;
%x_e0 = 1;
%B10 = 300;
%B20 = 0;
%history = [x_c0; x_e0; B10; B20];
tau = 40;
function s = history(t)
% Constant history function for DDEX1.
s = ones(1000,1,300,0);
sol = dde23(@ddex2de,tau,@history,[0, 1000]);
figure
plot(sol.x,sol.y(2,:))
title('Time delay')
xlabel('time t')
ylabel('x_e(t)')
% -----------------------------------------------------------------------
%
function dydt = ddex2de(y,Z)
% Differential equations function for DDEX2.
w_th = p.rho*x_c + p.kappa*p.rho*x_e;
if w_th ~= 0
C_c = min(1, w/w_th)*p.s*x_c;
C_e = min(1, w/w_th)*p.kappa*p.s*x_e;
else
C_c = p.s*x_c;
C_e = p.kappa*p.s*x_e;
if (x_c ~= 0) && (p.s ~= 0)
alpha_c = p.alpha_m + max(array([0, 1 - C_c/(p.s*x_c)]))*(p.alpha_M - p.alpha_m);
else
alpha_c = p.alpha_m;
if (x_e ~= 0) && (p.s ~= 0)
alpha_e = p.alpha_m + max(array([0, 1 - C_e/(p.s*x_e)]))*(p.alpha_M - p.alpha_m);
else
alpha_e = p.alpha_m;
end
ylag = Z(:,2);
x_etau = ylag(2);
x_c = y(1);
x_e = y(2);
B1 = y(3);
B2 = y(4);
dx_cdt = (p.beta_c - alpha_c)*x_c;
dx_edt = (p.beta_e*x_etau) - (alpha_e*x_e);
dB1dt = p.gamma*B1*(p.lamb - B1) - p.nu*x_c*B1;
dB2dt = p.nu*x_c*B1 - C_c - C_e - p.epsilon*B2;
dydt = [ dx_cdt
dx_edt
dB1dt
dB2dt];
end
% -----------------------------------------------------------------------
end % ddex2

Sign in to comment.

Categories

Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!