ODE45 is taking hours and hours to compute
Show older comments
I want to solve 27 odes, for that i formed equations with matrices.
First i formed A(27*27 matrix), B(27*27 matrix) and C(25*25 matrix). All three are interms of 'theta'(i used 'sym' for forming A, B, C matrices). Now iam going to use these matrices to form ode eq's and solving using ode45.
Here i gave script after forming A, B, C matrices for some confidentiality.
% A, B, C matrices formed interms of theta
myfun = @(t,y)scriptname(t,y,A,B,C);
% dummy values for tspan and y0
tspan = [0 1];
y0 = zeros(27, 1);
% ode solver
sol = ode45(myfun,tspan,y0);
h = figure;
% plot
plot(sol.x,sol.y(i,:));
function dydt = scriptname(t,y,A,B,C)
Wr = 2*pi*50;
p =2;
% evaluation of C (numerical) with theta = y(27)
Cn = double(subs(C,y(27)));
for i=1:25
I(i,1)=y(i);
end
T1=1/2*p*I'*Cn*I
if t<0.5
T2=0;
else
T2=7.31;
end
V=[cos(Wr*t);
cos(Wr*t+2.*pi/3.);
cos(Wr*t-2.*pi/3.);
zeros(21, 1);
0;
(T1-T2);
y(26)]
% evaluation of A and B (numerical) with theta = y(27)
An = double(subs(A,y(27)));
Bn = double(subs(B,y(27)));
dydt = Bn\V-(An*y);
end
While running the script, it is taking hours and hours(i waited 5-6 hours and stopped compiling) but not giving any result.
I dont know what is wrong with the script.
Can anyone suggest me how to get result quickly.
Accepted Answer
More Answers (1)
Jan
on 13 Sep 2021
Symbolic omputations need a lot of time. Can you implement the code numerically?
If the equation to be integrated is stiff, ODE45 tries to reduce the stepsize to mikroskopic values. Use a stiff solver in this case. e.g. ODE23S.
if t<0.5
Remember that Matlab's ODE integrators are designed to handle smooth functions only. Maybe this is a hard jump and the stepsize controller fails to pass this point. The correct way is to stop the integration at such jumps and restart it with the changed parameter.
12 Comments
Bathala Teja
on 13 Sep 2021
Walter Roberson
on 13 Sep 2021
i cant implement numerically since its difficult to form my A, B, C matrices numerically.
Outside of the ode call, use matlabFunction to generate function handles from A, B, C, and pass in the function handles and use those instead of subs()
Bathala Teja
on 13 Sep 2021
Stephen23
on 13 Sep 2021
@Bathala Teja: define those three (or however many) function handles before calling ODE45.
Then pass the function handles using the method shown here:
Bathala Teja
on 14 Sep 2021
Bathala Teja
on 14 Sep 2021
syms t
A = [1, t; t.^2, t.^3]
Afun = matlabFunction(A)
B = subs(A, t, 2)
Bfun = Afun(2)
Generate Afun from A ahead of time outside your ODE function and pass it into your ODE function. When you would have substituted values into A instead evaluate Afun.
Bathala Teja
on 14 Sep 2021
Walter Roberson
on 14 Sep 2021
Yes, Steven and I have understood your script.
Bathala Teja
on 14 Sep 2021
Jan
on 14 Sep 2021
@Bathala Teja: The discussion would be much easier, if you post your complete code. Then the conversion to a direct numerical version is most likely easy. As long as the readers are guessing, they can give abstract hints only. If you do not understand these hints, a helpful answer is unlikely.
Bathala Teja
on 14 Sep 2021
Categories
Find more on Mathematics 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!
