Help with Error using ==> plus message

조회 수: 8 (최근 30일)
Brian
Brian 2014년 1월 26일
답변: Walter Roberson 2014년 1월 26일
I am getting an Error using ==> plus message when I run the following code:
>> clear
% Continuous-time Model of Mass-Spring-Damper
A=[0 0 0 1 0 0; 0 0 0 0 1 0; 0 0 0 0 0 1; -.1 .1 0 -.05 .05 0; .001 -.201 .2 .0005 -.0305 .03;
0 2 -2 0 .3 -.3];
B=[ 0 0; 0 0; 0 0; 1 0; -.01 .01; 0 -.1];
G=[ 0; 0; 0; 0; 0; .5]; dt=0.5;
%Conversion to Discrete-time
ddt = linspace(0,dt,200);
summ = zeros(6); %nx = number of state varicables
for i = 1:200
summ = summ + expm(A*ddt(i));
Ad = expm(A*dt);
Bd = summ*dt/200*B;
Gd = summ*dt/200*G;
end
%Simulate Forced Process
NNN=15/dt; ttt=zeros(1,NNN); xxx=zeros(6,NNN);
x0=[1; .1; .5; 0; 0; 0]; xxx(:,1)=x0;
for kk=1:NNN-1
ttt(kk+1)=dt*kk; uk=0; wk=0;
xxx(:,kk+1)=Ad*xxx(:,kk) + Bd*uk + Gd*wk;
end
plot(ttt,xxx(1,:),'-k*',ttt,xxx(2,:),'-ko')
legend(' Mass Position',' Mass Velocity')
xlabel('Time (seconds)')
ylabel('State Variables (m or m/s)')
??? Error using ==> plus
Matrix dimensions must agree.
I think it is in my xxx(:,kk+1)=Ad*xxx(:,kk) + Bd*uk + Gd*wk; line of code but I can't figure out why... Any help will be appreciated

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 26일
편집: Azzi Abdelmalek 2014년 1월 26일
Just change this part of the code
uk=0;
wk=0;
for kk=1:NNN-1
ttt(kk+1)=dt*kk;
xxx(:,kk+1)=Ad*xxx(:,kk) + uk + Gd*wk;
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2014년 1월 26일
Examine the expression
Ad*xxx(:,kk) + Bd*uk + Gd*wk
Ad is expm(A*dt). A is 6 x 6, dt is a scalar, so A*dt is 6 x 6 and expm() of a 6 x 6 is in turn 6 x 6.
xxx is 6 x NNN, k is a scalar, so xxx(:,k) is 6 x 1.
6 x 6 matrix-multiply by 6 x 1 gives 6 x 1, so Ad*xxx(:,kk) results in 6 x 1.
Bd is summ*dt/200*B . summ is zeros(6) so it is 6 x 6. dt is a scalar, and 6 x 6 * a scalar is 6 x 6. B is 6 x 2, and 6 x 6 matrix-multiply by 6 x 2 gives 6 x 2.
uk is a scalar. 6 x 2 * a scalar gives 6 x 2, so Bd * uk is 6 x 2. Now uk is 0, so the 6 x 2 matrix will be all 0.
Gd is summ*dt/200*G. As per above, summ*dt/200 is 6 x 6. G is 6 x 1 and 6 x 6 matrix-multiply by 6 x 1 gives 6 x 1.
wk is a scalar. 6 x 1 * a scalar gives 6 x 1, so Gd * wk is 6 x 1. Now wk is 0, so the 6 x 1 matrix will be all 0.
You are thus attempting to add a 6 x 1 matrix together with zeros(6,2) together with zeros(6,1). The result is clearly going to be an error message.
If you are going to set uk and wk to 0, then unless you want the error, you should probably reduce the expression to just xxx(:,kk+1)=Ad*xxx(:,kk) without the Bd and Gd terms.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by