Loop to solve ODE45 multiple times?

조회 수: 1 (최근 30일)
pauldjn
pauldjn 2018년 6월 20일
댓글: Jan 2018년 6월 23일
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
Jan
Jan 2018년 6월 20일
편집: Jan 2018년 6월 20일
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.
pauldjn
pauldjn 2018년 6월 21일
Oh yeah I made a mistake in that part thanks for let it know

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 6월 20일
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
pauldjn
pauldjn 2018년 6월 22일
@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
Jan
Jan 2018년 6월 23일
Does Torsten's suggestion solve the problem?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by