現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
ode in a for loop updating initial conditions
3 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)