(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
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.
採用された回答
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 件のコメント
その他の回答 (0 件)
コミュニティ
その他の回答 パワー エレクトロニクス コミュニティ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!