How to solve an ODE with ode45 in a parfor loop?
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have the following code:
function [t,S] = solve_ode(fg_vars,tspan)
inital_conds = [0 0];
t = zeros(65,1);
S = zeros(65,2);
parfor i = 1:100
[t(:,i), S(:,i)] = ode45(@(t,s) fn(t,s,fg_vars),tspan,inital_conds)
end
t = t(:)
S = S(:)
end
Which gives me the following error:
Unable to perform assignment because the size of the left side is 65-by-1 and the size of the right side is 65-by-2.
I dont understand why this error occures, because when I try to calculate the ODE without the pafor loop, the produced solutions are of this size:
t = 65x1 double
S = 65x2 double
Thanks!
0 Commenti
Risposte (1)
KSSV
il 29 Ott 2020
Modificato: KSSV
il 29 Ott 2020
[t(:,i), S(:,i)] = ode45(@(t,s) fn(t,s,fg_vars),tspan,inital_conds)
In the above t will be a column vector and S will be a column matrix with two columns. You cannot save like that. You can do
T = cell(100,1) ;
S = cell(100,1) ;
for i = 1:100
[t, s] = ode45(@(t,s) fn(t,s,fg_vars),tspan,inital_conds) ;
T{i} = t ;
S{i} = s ;
end
The above cannot be used with parfor...you cannot index the parfor loop.
7 Commenti
KSSV
il 29 Ott 2020
Did you notice there is s and S. The one which is saved are initialized as cell S. And s is saved into S. You should have messed up with this.
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!