Why does this ode23 simulation not run/plot

1 回表示 (過去 30 日間)
Jonah Foley
Jonah Foley 2020 年 1 月 5 日
コメント済み: Star Strider 2020 年 1 月 5 日
I am trying to do something which is slightly above my paygrade as a MATLAB noob, but my code is as follows:
first function:
function v=pwm(t,T,d)
v=zeros(1,numel(t));
for i=1:numel(t)
k = floor(t(i)./T);
if ((k*T<=t(i)) & (t(i)<(k+d)*T))
v(i)=1;
end
if (((k+d)*T<=t(i)) & (t(i)<(k+1)*T)) %#ok<*AND2>
v(i)=0;
end
end
end
second function (differential equation):
function dx=boost(t, x, v_in, R, C, L, z) %#ok<INUSL>
dx=zeros(2,1);
for i=1:numel(z)
if z(i)==1
dx(1) = v_in./L ;
dx(2) = -x(2)./R*C ;
end
if z(i)==0 & x(1)>=0
dx(1) = (v_in - x(2))./L ;
dx(2) = x(1)./C - x(2)./R*C ;
end
if z(i)==0 & x(1)<0 %#ok<*AND2>
dx(1) = 0 ;
dx(2) = -x(2)./R*C ;
end
end
differential equation solving/simulation :
v_in = 1.5;
R = 680;
C = 200e-6;
L = 180e-6;
T = 1e-3;
d = 0.1;
t = linspace(0,0.2,2e6);
z = pwm(t,T,d);
tspan = (0:1e-7:0.2);
x0 = [0 0];
options=odeset('MaxStep', 1e-5);
[tv,xm] = ode23(@(t,x) boost(t, x, v_in, R, C, L, z), tspan, x0, options);
figure(1)
plot(tv,xm)
grid
legend('i_L', 'V_O', 'PWM')
My goal with this code is for the function 'pwm' to have a time vector input, and output which is a vector containing a series of 1's and 0's. This is my pwm regulated control signal, but all it needs to be thought of is as a vector of 1's and 0's. We then have the boost function which defines 3 sets of differential equations as seen by the three if statements. Which if statement, and by extension which set of differential equations is used it based on z(i), (and in some cases x(1)). Moving onto the simulation code, we can see that I have assigned z to the output vector produced by the pwm function which has an input time vector defined by t = linspace(0,0.2,2e6). Finally, I attempt to actually solve the system of differential equations using ode23, and then plot the result.
when it comes to running the simulation it pretty much never concludes, and I never end up seeing a result - any help as to why would be great

採用された回答

Star Strider
Star Strider 2020 年 1 月 5 日
Give it time!
I shortened ‘tspan’ to 150 elements to see if there was a problem with the code (there isn’t):
tspan = linspace(min(tspan), max(tspan), 150);
and it took 585 seconds, about 4 seconds per element. At that rate, with ‘tspan’ of elements, the integration should finish in 90.28 days.
The problem may be that you are passing ‘z’ and evaluating every element of it (it is the same length as ‘t’, and ‘tspan’ however it is evaluated at different time points than ‘tspan’) in a for loop, rather than calling ‘pwm’ as a function as in the earlier code, and with the time points corresponding to ‘tspan’.
So, it might be best to shorten ‘tspan’ withoiut affecting it limits. One way is to re-define it just after the original ‘tspan’ assignment (as I did) using linspace, choosing an appropriate number of elements for the new ‘tspan’.
Also, it is llikely best to use yyaxis for the plots, since ‘xm(:,1)’ and ‘xm(:,2)’ have significantly different magnitudes:
figure(1)
yyaxis left
plot(tv,xm(:,1))
yyaxis right
plot(tv,xm(:,2))
grid
legend('i_L', 'V_O', 'PWM')
You will only be pltting two values, so the third (’PWM’) will not appear. Plotting it will require a separate plot call, then plotting it as a function of whatever time argument you choose to evaluate it with.
  6 件のコメント
Jonah Foley
Jonah Foley 2020 年 1 月 5 日
ok, this was really useful, thank you
Star Strider
Star Strider 2020 年 1 月 5 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

コミュニティ

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by