이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
ode in a for loop updating initial conditions
조회 수: 4 (최근 30일)
이전 댓글 표시
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일
because giri changes through time,
basically I have to solve the ode for:
giri = 55000 from 0s to 25s with the initial condition y1(0) and y2(0), then giri changes
then giri changes like this:
giri_medium = 55000; %throttle valve aperture
delta_giri = 400; %range between your gamma_T_max e gamma_T_min
giri_max = giri_medium+(delta_giri/2); %maximum aperture
giri_min = giri_medium-(delta_giri/2); %minimum aperture
A_rpm = (giri_max - giri_min)/2; %amplitude
b_rpm = (giri_max + giri_min)/2;
rpm_frequency = 1; %engine's frequency [Hz]
w_rpm = rpm_frequency*2*pi; %engine's frequency [radiants/s]
time_rpm=[25:0.01:30];
giri = A_rpm*sin(w_rpm*time_rpm)+b_rpm;
plot(time_rpm,giri)
and the only way I could think of was that one because every time the value of giri changes I need the previous initial conditions
Paul Rogers
2021년 1월 7일
편집: Paul Rogers
2021년 1월 7일
every time giri changes I need as initial condition the last values of y evalueted at the step before (for the previuos value of giri).
I also did the code for just one value of giri = 55000 in a tspan 0s-50s, but now I need giri to be 55000 for the fits 25s, then to change like in the code previouslly written
Paul Rogers
2021년 1월 7일
the thing I don't get is that the code works for the first loop, then it stops at the second and displays this message:
Error using odearguments (line 84)
The entries in tspan must strictly increase or decrease.
Paul Rogers
2021년 1월 7일
the first cycle is from 0-25.0100 for the first value of giri.
for the second value of giri should be from 25.0100 to 25.0200.
and so on
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일
now I get this:
Error using *
Inner matrix dimensions must agree.
Error in greitzer (line 34)
dy = [ B*(psi_c-y(2));
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode113 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in main (line 106)
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1,y2],options); %I found ode113 is way more efficient
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일
by doing this:
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options,U1(i),U2(i),B(i));
and:
function [ dy ] = greitzer( t,y,U1,U2,B )
I get this the error:
Error using *
Inner matrix dimensions must agree.
Error in greitzer (line 35)
dy = [ B*(psi_c-y(2));
It stops when the second loop should start.
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일
first thing I did in the morning was to follow yoour update.
I brought:
save main_parameters.mat
outside the loop and the simulation finally seems to goo until the end.
I also changed the lenght of the simulation by switching to:
save main_parameters.mat
for i=1:(length(giri)-2)
U1(i) = (D1*pi.*giri(i))/60;
U2(i) = (D2*pi.*giri(i))/60;
B(i) = U1(i)./(2.*wh.*Lc);
[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
t0(i+1) = time_rpm(i+1); %simulation's start [s]
tf(i+1) = time_rpm(i+2); %simulation's finish [s]
y1(i+1) = y(end,1); %initial condition 1
y2(i+1) = y(end,2); %initial condition 2
plot(t,y(:,2))
grid on
grid minor
xlabel('t [s]')
ylabel('\Psi')
hold on
end
by thesee littlle tweeks thanks to you I ccould finally get the result I expeccted.
Just one more thing now:
How can I store all the datas (t,y(:,1) and y(:,2)), since the function only shows the values from the last loop.
Paul Rogers
2021년 1월 8일
thanks to you I solved the loop problem and I got the results I wanted, unfortnatley I can't see the "accepted answer button"
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)