ODE45 is taking hours and hours to compute
10 views (last 30 days)
Show older comments
Bathala Teja
on 13 Sep 2021
Commented: Bathala Teja
on 14 Sep 2021
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.
0 Comments
Accepted Answer
Walter Roberson
on 14 Sep 2021
Pay attention to the fact that the if statement was removed from the code, and that instead the run was split into two pieces that pass in different T2 values. The mathematics used for ode45() is such that if you use two different branches of an if statement in a single call to ode45(), then there is a good chance that your code is wrong, and that you need to stop the integration at the boundary and then resume integration from where you left off.
% A, B, C matrices formed in terms of theta
commonvars = unique([symvar(A), symvar(B), symvar(C)]); %probably just theta
Afun = matlabFunction(A, 'vars', commonvars);
Bfun = matlabFunction(B, 'vars', commonvars);
Cfun = matlabFunction(C, 'vars', commonvars);
tspan1 = [0, 0.5]; T2_1 = 0;
tspan2 = [0.5, 1]; T2_2 = 7.31;
myfun1 = @(t,y)scriptname(t, y, T2_1, Afun, Bfun, Cfun);
myfun2 = @(t,y)scriptname(t, y, T2_2, Afun, Bfun, Cfun);
y0_1 = zeros(27, 1);
% ode solver
[t_1, y1] = ode45(myfun1, tspan1, y0_1);
y0_2 = y1(end,:);
[t_2, y2] = ode45(myfun2, tspan2, y0_2);
t = [t_1; t_2];
y = [y1; y2];
h = figure;
% plot
plot(t, y);
function dydt = scriptname(t, y, T2, Afun, Bfun, Cfun)
Wr = 2*pi*50;
p =2;
% evaluation of C (numerical) with theta = y(27)
Cn = Cfun(y(27));
for i=1:25
I(i,1)=y(i);
end
T1=1/2*p*I'*Cn*I
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 = Afun(y(27));
Bn = Bfun(y(27));
dydt = Bn\V-(An*y);
end
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.
See Also
Categories
Find more on Linear Algebra 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!