Solving a system of ODEs using ode45 in Matlab
2 views (last 30 days)
Show older comments
Hello guys,
I have a set of four ODEs and need to solve them using ode45 in Matlab. In this ODEs, one of the parameters is a data matrix and other two parameters are vectors. The ODEs model needs to be repeated several time based on the second dimenstion of the data matrix. I tried to code this but getting an error saying that one of the ODEs is undefined once running the code.
The error I got is:
Undefined function or variable 'dmdt'.
Error in testmodel>model (line 120)
dxdt=[dmdt,dfdt,dpdt,dgdt];
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);
Error in testmodel (line 74)
[T,X] = ode45(@model,tspan,InitialCond);
close all; clear all; clc;
Kg = 0.1;
Kd = 1.1;
kg = 0.05;
kd = 0.05;
ka = 0.0455;
Kr = 4.5;
Ks = 1;
alpha = 300;
beta = 0.8;
w = 4;
l = 4;
m16= 7.26;
m15 = m16; m14 = m15; m13 = m14; m12 = m13; m11 = m12;
m10 = m11; m9 = m10; m8 = m9; m7 = m8; m6 = m7; m5 = m6;
m4 = m5; m3 = m4; m2 = m3; m1 = m2;
f16= 0.465;
f15 = f16; f14 = f15; f13 = f14; f12 = f13; f11 = f12;
f10 = f11; f9 = f10; f8 = f9; f7 = f8; f6 = f7; f5 = f6;
f4 = f5; f3 = f4; f2 = f3; f1 = f2;
p16= 0.016;
p15 = p16; p14 = p15; p13 = p14; p12 = p13; p11 = p12;
p10 = p11; p9 = p10; p8 = p9; p7 = p8; p6 = p7; p5 = p6;
p4 = p5; p3 = p4; p2 = p3; p1 = p2;
g16= 0.127;
g15 = g16; g14 = g15; g13 = g14; g12 = g13; g11 = g12;
g10 = g11; g9 = g10; g8 = g9; g7 = g8; g6 = g7; g5 = g6;
g4 = g5; g3 = g4; g2 = g3; g1 = g2;
ds= rand(1473,16);
sp= rand(1,16);
sg= rand(1,16);
d1 = ds(:,1)
d2 = ds(:,2)
d3 = ds(:,3)
d4 = ds(:,4)
d5 = ds(:,5)
d6 = ds(:,6)
d7 = ds(:,7)
d8 = ds(:,8)
d9 = ds(:,9)
d10 = ds(:,10)
d11 = ds(:,11)
d12 = ds(:,12)
d13 = ds(:,13)
d14 = ds(:,14)
d15 = ds(:,15)
d16 = ds(:,16)
rd = [d1,d2 ,d3 ,d4 ,d5 ,d6 ,d7 ,d8 ,d9 ,d10,d11, d12,d13,d14,d15,d16];
m= [m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16];
f= [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16];
p= [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16];
g= [g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16];
InitialCond= [m,f,p,g];
Tstart= 0;
StepSize= 0.5;
Tend= 470000;
tspan=(Tstart:StepSize:Tend);
[rds cds]=size(ds);
for ii=1:rds
for jj=1:cds
[T,X] = ode45(@model,tspan,InitialCond);
figure(1)
plot(T, X(:,1));
hold on
figure(2)
plot(X(:,2),X(:,1))
hold on
end
end
function dxdt = model(t,Y)
format long
global m
global f
global p
global g
global rd
global sp
global sg
n= 16
mn = Y(1:n);
fn = Y(n+1:2*n);
pn = Y(2*n+1:3*n);
gn = Y(3*n+1:4*n);
[rim_r,rim_c]= size(rd)
for i=1:length(rim_r)
for j=1:length(rim_c)
for ii=1:length(sp)
for jj=1:length(sg)
dmdt = kg*(fn*gn)/(Kg+fn)-(mn*pn)/(1+mn)+rd(i,j)
dfdt = -kg*(fn*gn)/(Kg+fn)+(mn*pn)/(1+mn)-(fn*pn)/(1+fn)
dpdt = sp(ii)*alpha*(fn^w)/(Kr^w+fn^w)-ka*pn^2
dgdt = sg(jj)*beta*(fn^l)/(Ks^l+fn^l)-kd*(gn*pn)/(Kd+gn)
end
end
end
end
dxdt=[dmdt,dfdt,dpdt,dgdt];
end
Can you please help me with fixing my code.
3 Comments
Answers (1)
Walter Roberson
on 25 Dec 2020
You do not initialize any of your global variables, so when MATLAB sees the global statements it creates the global variables and initializes them to empty. But length(empty) is 0, so your for loops do not do any work, and so do not assign to any of the variables you need to create dxdt .
Best thing to do is not use global. See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Side note: you need to return a column vector of values, not a row vector.
Also, you are overwriting all of your variables in every iteration through the loops.
Note that you need to produce one output element for each element of the boundary conditions, which is 64 elements. But you have a nested loop that is 4 deep, so you are at risk of producing too many outputs.
[rim_r,rim_c]= size(rd)
rim_r and rim_d are going to be scalars
for i=1:length(rim_r)
for j=1:length(rim_c)
length() of a scalar is 1.
11 Comments
Walter Roberson
on 30 Dec 2020
I need to iterated the ODEs model 16 times
I need to feed in the ODEs model with d1 at iteration 1, and then with d2 at iteration 2 and so on till d16
Each iteration you are working with a vector of 16 elements, so with 16 iterations you would get 16*16 = 256 results at least -- unless, that is, you are initializing some variable before the first round, and the results of the first round get assigned to that variable, and the results are used to calculate values for the second round, which is used to update the variable, and so on, so at the end you get 16 results that incorporate information from all 16 rounds. For example in some cases it might make sense to total the results over 16 round.
Unless you do have some kind of totalling or feedback, I am unable to match up the number of iterations you are doing with the number of output values you are expecting.
Not unless where you have (for example)
dmdt = P.kg .* (fn.*gn) ./ (P.Kg+fn) - (mn.*pn) ./ (1+mn) + rd(i,j);
that the vector variables being used should be indexed so that you are producing a scalar output each time ??
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!