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

1 回表示 (過去 30 日間)
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.
  2 件のコメント
dpb
dpb 2020 年 1 月 4 日
Probably one of the globals is a vector...I notice you have the dot operator for element-wise operations in play. That's the problem w/ global, the context in the code elsewhere is difficult to debug.
Use the debugger...
Code stylistic note:
dx=[dx(1);dx(2)];
doesn't do anything useful...you've already created the column vector orientation for dx in the preallocation step.
Jonah Foley
Jonah Foley 2020 年 1 月 4 日
編集済み: Jonah Foley 2020 年 1 月 4 日
noted, about the stylistic note, honestly forgot I had that preallocation in there and aded the bit you highlighted just recently! The only other code I have for this project is, firstly the pwm 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
this one runs fine, I have simulated and seen expected results, and then:
global v_in R C L T
v_in = 1.5;
R = 680;
C = 200e-6;
L = 180e-6;
T = 1e-3;
t = linspace(0,0.2,2e6);
pwm(t,T,0.1);
options=odeset('MaxStep', 1e-5);
[t,x]= ode23(@boost,(0:1e-7:0.2), [0 0], options);
To be honest, im out of my depth here - since the diff eq. depends on the pwm function im unsure about simullating them both simulataneously, so this may be far from correct. The only reason I used globals is that some of the code was provided for me, and those variables had been already defined as globals - so I assume its necessary for whatever reason.
Hope this sheds some more light on the original problem though
thank you

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

採用された回答

Star Strider
Star Strider 2020 年 1 月 4 日
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.
  12 件のコメント
Jonah Foley
Jonah Foley 2020 年 1 月 4 日
Damn, thats looks good thanks!

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

その他の回答 (0 件)

コミュニティ

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by