solving many linear differential equations through ODE solver.

I am not sure whether my approach is good or not, because my simulation takes ENORMOUS amount of time in execution.
I have a general form of differential equations , which contain summation signs, and it is suumed over i,j. When i,j varies, the number of variables in the differential equations also changed. Index i and j are related with a relation.That means the number of variable are also dependent on i & j .So, here is what i am planning
syms H(i,j) % generalized variable
% Storing the varaibles
n=10 %number of variables (This would change)
for i=1:n
for j=1:n
if i+j==... && i/2==...
T(i,j)=H(i,j) % Store the variable
end
end
%% Now i am using T as variable, and using 4 "for loops" to form the differential equations.
% Then solve it through ODE .
The code is working for small value of "n". But it takes 3 days when n=50. And runs out of memory when n=60. How can i optimize my code and avoid syms ? I can observe, that the major problem is in creating the equations.Am i doing wrong in creating the variables like this ? Is there any way to overcome this ?

 Accepted Answer

Don't use symbolic variables together with "many" differential equations.
Use a numerical ODE integrator instead (e.g. ODE15S).

11 Comments

Thanks Torsten for the reply !!
"Don't use symbolic variables together with "many" differential equations.". But I have to generate/form equations to simulate my model. I mean, what is the other way to overcome symbolic 'constrain' .
You don't need symbolic variables to form your equations.
Take a look at the examples provided for the ODE solvers.
I'm hinting to this:
https://de.mathworks.com/help/matlab/ref/ode45.html
Torsten I am trying to solving this
If you observe it I have to first 'GENERATE' differential equations and then solve it. Can you suggest something.
Something like this together with a call to ODE15S:
function dy = fun(t,y)
N = numel(y)-1;
Pn = y(1:N);
P = y(N+1);
dPn = zeros(N,1);
for n = 2:N
summe = 0.0;
for r = 1:n-1
summe = summe+Pn(r)*Pn(n-r)
end
dPn(n) = -2*kp*Pn(n)*P + 0.5*2.0*kp*summe;
end
dP = ?
dPn(1) = ?
dy = [dPn;dP]
end
@Torsten N also have to be defined inside the function right?
@madhan ravi:Thank you for your comment.
If you know how to generate the Pn and kp variables from just the values of y then you should be fine getting N from the size of y. Otherwise, i.e. if you need a more arbitrary ODE you can allways write your function fun with additional input parameters:
function dy = fun(t,y,H)
dy = H*y;
end
Then you'll have to call the ode-integrating functions something like this:
H_5 = randn(5); % or whatever you need
y0 = (1:5)';
y_s = ode15s(@(t,y) fun(t,y,H_5),[t_0,t_end],y0);
HTH
You didn't write the differential equations for P and P1.
These have to be inserted in the lines
dP = ?
dPn(1) = ?
Further, "kp" has to be specified at the beginning of the function.
Thanks Torsten,Madhan ravi and Bjorn Gustavsson. I really appreciate your help.It saved a lot of computation time.
Since I am not from programming background, It took so long for me in accepting your answer.

Sign in to comment.

More Answers (0)

Asked:

P K
on 6 Jan 2019

Commented:

P K
on 17 Jan 2019

Community Treasure Hunt

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

Start Hunting!