ode in a for loop updating initial conditions
이전 댓글 표시
I created the files in attached, main and @greitzer (function). In main I call the function in @greitzer.
Now I would like to put the ode in a for loop to solve the problem for each values of giri.
I'd like the first loop to be from:
t0(1)=0
to the second value of time_rpm(i)
time_rpm(2)
with initial condition y1(1)=0 and y2(0).
Now the second loop, for the second value of giri, so
giri(2)
should be in the interval
t0(2)=time_rpm(2)
tf(2)=time_rpm(3)
with the initial condition to be the last evaluation of the previous loop
y1(i+1) = y(end,1); %initial condition 1
y2(i+1) = y(end,2); %initial condition 2
댓글 수: 12
Cris LaPierre
2021년 1월 7일
편집: Cris LaPierre
2021년 1월 7일
What is the reason you want to solve it this way? Knowing that, there might be a better approach we could suggest.
Paul Rogers
2021년 1월 7일
Paul Rogers
2021년 1월 7일
편집: Paul Rogers
2021년 1월 7일
Paul Rogers
2021년 1월 7일
Paul Rogers
2021년 1월 7일
Cris LaPierre
2021년 1월 7일
편집: Cris LaPierre
2021년 1월 7일
I think the issue is you need to index the t0 and tf values in your call to ode113. In the first loop, they are scalars, in loops 2+, they are vectors. Just pass in the current values each time.
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options);
Paul Rogers
2021년 1월 7일
Cris LaPierre
2021년 1월 7일
편집: Cris LaPierre
2021년 1월 7일
That error is a symptom of the same issue. You really want U1(i),U2(i), and B(i) in your equations, but you use the entire vector.
You might be interested in this example, which shows how to pass extra parameters to your odefun.
Saving and loading a mat file is not a good solution.
I'd call ode113 with the following syntax
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options,U1(i),U2(i),B(i)); %I found ode113 is way more efficient
This means updating your function declaration accordingly.
function [ dy ] = greitzer( t,y,U1,U2,B )
Paul Rogers
2021년 1월 7일
편집: Paul Rogers
2021년 1월 7일
Cris LaPierre
2021년 1월 8일
편집: Cris LaPierre
2021년 1월 8일
You overlooked the other comment I made about saving/loading your other parameters from a mat file. This is replacing the values you are passing in with the ones loaded from the mat file. At the least, do not include U1, U2 or B in your save. That means at least removing the save from your for loop and uncommenting the one earlier in your code.
Just a heads up, you're going to get another error once you fix this one.
Paul Rogers
2021년 1월 8일
편집: Paul Rogers
2021년 1월 8일
Paul Rogers
2021년 1월 8일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!