Why does this ode23 simulation not run/plot
Show older comments
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
Accepted Answer
More Answers (0)
Communities
More Answers in the Power Electronics Control
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!