Error when using 2 functions as input in lsim
Show older comments
Whenever I run this program, it gives me an error saying:
Error using *. Inner matrix dimensions must agree.
Could someone please explain how else i'm supposed to have the input as exp(-2t)*cos(t)?
Fs = 100;
dt = 1/Fs;
StartTime = -5;
StopTime = 15;
t = StartTime:dt:StopTime-dt;
A = [0 1 0;0 0 1;-1 -4 -2];
B = [0;0;1];
C = [1 0 0];
D = zeros(1,1);
[nom, denom] = ss2tf(A,B,C,D,1);
sys = ss(A,B,C,[]);
t=linspace(0,6);
u = (exp(-2*t) * cos(t));
lsim(sys,u,t)
1 Comment
Birdman
on 5 Nov 2017
In the previous question, have you run my simulink model?
Answers (1)
Walter Roberson
on 5 Nov 2017
The * operator is the algebraic matrix multiplication operator. A*B with A being m x n and B being p x q, (m x n) * (p x q) requires that m and p are the same, and produces an output that is m x q . This is expressed as "the two inner dimensions must be the same"
When you have (exp(-2*t) * cos(t)) you are asking to matrix-multiply something that is (1 x length(t)) * (1 x length(t)) . For this to work, length(t) would have to match 1 -- so scalar * scalar. That is not the case for you.
What you want is
u = (exp(-2*t) .* cos(t));
the .* operator is element-by-element multiplication, equivalent to u(K) = exp(-2*t(K)) * cos(t(K)) for scalar K.
Categories
Find more on Linear Algebra 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!