Using a variable in a matrix then plotting one element of the matrix.
Show older comments
Hello,
I'm trying to work with matrices and variables.
Ideally, what I would like to do would be to have a 4 by 4 Matrix (let's call it M for now).
Each of the elements of M depend on a variable x.
Now I would like to take the inverse of M and plot inv(M)(1,1).
When I try doing that in Matlab, I first define my variable
x=0:0.01:5;
Then calculate my matrix
M=A*x^2+B*x+C (where A, B, C are 4 by 4 matrices)
But when I do that, there's an error with matrix sizes.
Should I adapt the size of my M matrix, or am I doing this completely wrong?
Thanks in advance.
Accepted Answer
More Answers (1)
Walter Roberson
on 7 Apr 2012
M=A*x.^2+B*x+C
2 Comments
Julien
on 8 Apr 2012
Walter Roberson
on 8 Apr 2012
Did you realize that the syntax 0:0.01:5 produces a row vector? So size 1 by something? When you have a 4 x 4 matrix matrix-multiplying a second matrix, then because inner dimensions need to match for matrix multiplication, the second matrix would have to be 4 x something. But yours is 1 by something instead.
Your Om is definitely not length 4.
Your Z = -M.*Om.^2 + i.*Om.*C + K; is being careless about whether Om is on the left-hand size of the multiplication or on the right hand side. It doesn't matter for .* of course, but is there any good reason not to be consistent in the placement of the coefficients, so that you would not have to re-arrange terms if you were to switch to * instead of .* ?
Okay, what I guess you want is to replace your current Z= line with
nOm = length(Om);
HOm = zeros(nOm,1);
for K = 1 : nOm
Z = -M .* Om(K).^2 + i .* C .* Om(k) + K;
H = inv(Z);
HOm = H(1,1);
end
plot(Om, HOm);
Categories
Find more on Matrix Indexing 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!