Reputation: 437
I was wondering if it's possible to combine for-loops/while-loops and ODE45
?, please look at the example below:
I have a function (ode) which I want to solve at different ic
(initial conditions):
ser = @(x) x.^(-0.3) - x.^(1.8); % the function
tspan = 1:0.02:2;
x0 = 0.5;
% x0 = 0.8;
% x0 = 1.2;
% x0 = 1.8;
% x0 = 2;
% x0 = 2.5;
[~, x_t] = ode45(@(t,x) ser(x), tspan, x0);
plot(tspan,x_t,'r-')
And plot the solution curves
at the end.
Is there a way to pass the other ic
without doing it manually, like a loop? or any other way to optimize this step? If I could receive some help with this, it will be very appreciated because I have to compute many ODEs
(more complex ones) at 15 to 25 different ic
.
Thanks in advance!
PS. If the code has to change (e.g. different names for the x0
s or solutions x_t
) it will be OK!
Upvotes: 0
Views: 1863
Reputation: 1164
If you have a predefined no. of ic
you can use this for loop:
for i=1:NoOfic
test_mat(i,:)=test_ode_45(x(i),tspan);
end
You should predefine your matrix (test_mat) in a sufficient dimension e.g.
test_mat = zeros(NoOfic, SizeOftspan)
Obviously I have defined the variables NoOfic
and SizeOftspan
to the specific numbers
My whole test-code is:
tspan = 1:0.02:2;
x= [0.4 0.8 1.2 1.5 2.6];
sizeOftspan = size(tspan);
sizeOfFamily= size(x);
test_mat = zeros(sizeOfFamily(2),sizeOftspan(2));
test_tes= test_ode_45(x(1),tspan);
for i=1:sizeOfFamily(2)
test_mat(i,:)=test_ode_45(x(i),tspan);
end
plot(tspan, test_mat)
Where my test_ode_45
function is a simple x*sin(t) function
Upvotes: 1