Ode45 iteration issue
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I'm trying to solve differential equation using ode45 function, the formula looks like this:
[t1,X2] = ode45(@(t,x)fun(t,x,C1,C2,C3,C4),t0,X01);
where parameters C1, C2, C3 and C4 are column vector in size of 1:1001. What I want to do is I need to put them inside of function that ode45 is referring (fun.m) to and use them in equation, but I want the values to change after every iteration. So, for example, at the beginning C1 value I want in is C1(1), the next iteration it's C1(2), next iteration it's C1(3) etc. Is there any way to implement that?
댓글 수: 0
채택된 답변
madhan ravi
2019년 5월 17일
[t1,X2]=deal(cell(numel(C1),1));
for k = 1:numel(C1)
[t1{k},X2{k}] = ode45(@(t,x)fun(t,x,C1(k),C2(k),C3(k),C4(k)),t0,X01);
end
댓글 수: 1
madhan ravi
2019년 5월 19일
Update
The code has worked properly, but then after some changes it started to give me 1001x1 cell that has only brackets, like this "[]" and each bracket is empty inside.
The code looks like this
[t1,X2]=deal(cell(numel(C1),1));
[t1,X2]=deal(cell(numel(C2),1));
[t1,X2]=deal(cell(numel(C3),1));
[t1,X2]=deal(cell(numel(C4),1));
for k = 1:numel(C1)
[t1{k},X2{k}] = ode45(@(t,x)fun(t,x,C1(k),C2(k),C3(k),C4(k)),t0,X01);
end
Each C is 1x1001 double and has values in it.
That is called preallocation. You don’t need to use the above lines 4 times "ONCE" is enough. You should be seeing values in t1 & X2.
I'm not quite sure if I understand, but even if I do it once it won't pass the proper values to the function (the X2 which is cell has just brackets, as I said) making the results wrong. Going further, if I do the line only once for example C1, how does it pass parameter C2 values to the function?
I have shown the standard procedure and I can't read your mind without knowing your entire code and the changes you made.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!