Loop to solve ODE45 multiple times?

Hello I made this loop to try to solve this system of diferential equations with different initial conditions but im not sure if is ok since im only obtaining a matrix of 45 values where I suppose to get more. This is my code
kba= 1;
kmax= 10000000;
r = 1;
co= 1;
for a = gen10m
for b = gen11m
for c = gen01m
for d = gen00m
k= kba + kmax *(((b + a)/(a + b + c + d))*((b + c)/(a + b + c + d)));
f = @(t,x) [(r - (1 + 1) * co) * x(1) * (1 - ((x(1) + x(2) + x(3) + x(4))/k)); (r - (1 + 0) * co) * x(2) * (1 - ((x(1) + x(2) + x(3) + x(4))/k));
(r - (0 + 1) * co) * x(3) * (1 - ((x(1) + x(2) + x(3) + x(4))/k)); (r - (0 + 0) * co) * x(4) * (1 - ((x(1) + x(2) + x(3) + x(4))/k))];
[t,x] = ode45(f,[0 1],[a b c d]);
gen10m,gen11m, etc...are vectors of the same size so I want to use eah value of these vectors as initial conditions and solved this system for all the values of the vector. Do you think my code is wrong? Or maybe I have to improve it and add a way to save the data each time it is solve for a particular initial conditions?

2 Comments

Jan
Jan on 20 Jun 2018
Edited: Jan on 20 Jun 2018
Is this the complete code? There are at least some missing end commands.
c=1; is useless, if you use c as loop counter also.
Oh yeah I made a mistake in that part thanks for let it know

Sign in to comment.

Answers (1)

Currently you overwrite the results obtained in each iteration. Maybe you want:
nResult = length(gen10m) * length(gen11m) * length(gen01m) * length(gen00m);
Result = cell(1, nResult);
iResult = 0;
for a = gen10m
for b = gen11m
for c = gen01m
for d = gen00m
...
[t,x] = ode45(f,[0 1],[a b c d]);
iResult = iResult + 1;
Result{iResult} = [t, x];
end
end
end
end

8 Comments

Thanks a lot, I test this code and it worked perfectly. Now the thing is I notice that my loop is actually not doing what I thought, because is not evaluating the corresponding values of the vectors is doing it one by the rest of values and that’s why I’m getting an exponential quantity of data. So, do you know how I could modify these indexes to really evaluate the firsts values of the 4 vector and then all the second ones and so on?
I do not understand the comment. What does "not evaluating the corresponding values of the vectors is doing it one by the rest of values" mean? Where do you get which "exponential quantity of data"? Which indices do you want to modify?
pauldjn
pauldjn on 22 Jun 2018
Edited: pauldjn on 22 Jun 2018
I mean suppose I have [1,2,3,4] as values of my vectors, it solve the equations saving the first value (1) of the first vector and using the rest of the values of the other vectors, then it will save the second value (2) and solve the equation with the other values etc., and then it will do the same with the rest of the vectors, so in the end if my vector have 4 values and because I have 4 variables in my ODE system I will have 4^4 results and not 4 x 4 as I should. What I want to do is that the system evaluates all the first values of the 4 vectors without mixing them, then the second ones, and the third ones and so on.
nResult = length(gen10m);
Result = cell(1, nResult);
for iResult = 1:nResult
a = gen10m(iResult);
b = gen11m(iResult);
c = gen01m(iResult);
d = gen00m(iResult);
...
[t,x] = ode45(f,[0 1],[a b c d]);
Result{iResult} = [t, x];
end
@PAUL DAMIAN JIMENEZ NUÑO: Please explain, what gen10m etc are. I cannot follow the description in your comment. What does "saving the first value (1)" mean? And "using the rest of the values of the other vectors"? Please give an example.
@Torsten Thank you so much it worked perfectly
@Jan Thanks for your concer. Suppose that my vectors are: gen10m = [1,2,3,4],gen11m = [5,6,7,8], gen01m = [9,10,11,12], gen00m = [13,14,15,16] so in the first iteration the values for a,b,c and d will be: a=1 b=5 c=9 d=13. Then in the second iteration the values will be: a=2 b=6 c=10 d=14. and so on but instead what my code was doing was evaluating in a combinatory manner let say a=1 b=5 c= 9 d=13 then a=1 (again) b=6 c=10 d=14...etc
Does Torsten's suggestion solve the problem?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 20 Jun 2018

Commented:

Jan
on 23 Jun 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!