MATLAB: (ODE23) Unable to perform assignment because the left and right sides have a different number of elements error

electronicspower_electronics_control

Hello guys, I am coming across a problem when trying to formulate a differential equation to be simulated by ode23. My code iis as follows:
function dx=boost(t,x)
global v_in R C L T d
dx=zeros(2,1);
if pwm(t,T,d)==1
dx(1) = v_in./L ;
dx(2) = -x(2)./R*C ;
end
if pwm(t,T,d)==0 & x(1)>=0
dx(1) = (v_in - x(2))./L ;
dx(2) = x(1)./C - x(2)./R*C ;
end
if pwm(t,T,d)==0 & x(1)<0 %#ok<*AND2>
dx(1) = 0 ;
dx(2) = -x(2)./R*C ;
end
dx=[dx(1);dx(2)];
end
so the diff eq. takes three forms, as seen by the three if statements and they depend on the current outputs of another function. This other function is a pwm signal which controls a switch (this is being used to simulate a boost converter circuit but that is irrelevant).
the error 'Unable to perform assignment because the left and right sides have a different number of elements' refers to line 9 [ dx(1) = (v_in – x(2))./L ] . It makes no sense to me why I see this error since as far as I am aware both sides of the equations are scalar. Also I dont see this error for any other assigments in the function which all have similar forms.
Thank you.

Best Answer

First, pass the extra parameters as arguments. It is best never to use global variables:
function dx=boost(t, x, v_in, R, C, L, T, d)
dx=zeros(2,1);
if pwm(t,T,d)==1
dx(1) = v_in./L ;
dx(2) = -x(2)./R*C ;
end
if pwm(t,T,d)==0 & x(1)>=0
dx(1) = (v_in - x(2))./L ;
dx(2) = x(1)./C - x(2)./R*C ;
end
if pwm(t,T,d)==0 & x(1)<0 %#ok<*AND2>
dx(1) = 0 ;
dx(2) = -x(2)./R*C ;
end
dx=[dx(1);dx(2)];
end
Then in your ode23 call, it becomes:
[T,X] = ode23(@(t,x) boost(t, x, v_in, R, C, L, T, d), tspan, x0);
Second, at least one of the values in:
dx(1) = (v_in - x(2))./L;
could be empty, or a vector, either of which would throw that error.