# Saving each vector of an ode45 vector solution in a matrix.

2 views (last 30 days)
Gabriel Venter on 26 Apr 2021
Commented: J. Alex Lee on 27 Apr 2021
I have the following code, where I am using a different value of a parameter G in my ODE system (solving it w/ ode45) and want to plot each solution on the same graph. It isnt the same as IC's, as these are the same but the parameter varies. Currently I have:
for i=1:20
G=i/20 %Saving that iterates value of G
[phi,y]=ode45(@(phi,y) odefun(phi,y,G), timerange, IC);
z(i,;)=y %
end
then I want to save all these y's without creating 20 vectors. Is there a way to save each vector y as a column of a matrix? I am very lost and any direction would be very helpful, I cannot find a thread or anythign on the helpcenter.
Gabriel Venter on 27 Apr 2021
I have a 2D autuonomous ODE system here. And I was worried about this timestep problem since ode45 uses a variable timestep and that would be different for different G, so clearly what I was doing was stupid. Thanks!

J. Alex Lee on 27 Apr 2021
Edited: J. Alex Lee on 27 Apr 2021
z = cell(1,20)
for i=1:20
G=i/20 %Saving that iterates value of G
[phi,y]=ode45(@(phi,y) odefun(phi,y,G), timerange, IC);
z{i}=y %
end
Or better yet just use the solution structure version of output
for i=20:-1:1
G=i/20 %Saving that iterates value of G
sol(i) = ode45(@(phi,y) odefun(phi,y,G), timerange, IC);
end
##### 2 CommentsShowHide 1 older comment
J. Alex Lee on 27 Apr 2021