ode in a for loop updating initial conditions
5 次查看(过去 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 (한국어)