Using a variable in a matrix then plotting one element of the matrix.
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
Kye Taylor
am 8 Apr. 2012
When matlab sees A.*B or A.^B the two arrays A and B need to be the same size. In the expression Z = -M.*Om.^2 + i.*Om.*C + K, the matrix M is 4-by-4 but Om is 1-by-501. The regular * and ^ also won't work in your situation because MATLAB interprets those operations as matrix multiplication and matrix exponentiation so the common rule from linear algebra of inner-dimensions is required. That is M*x only works for x that have dimensions 4-by-(someNumber).
Let me make an example that is similar to your situation. Let M be a 2-by-2 matrix whose entries depend on a parameter p. For example, M could have entries
M(1,1) = 1; M(1,2) = p; M(2,1) = p^2; M(2,2) = p; M(3,3) = 1; M(4,4) = 1; and zeros everywhere else.
I will define M in MATLAB using the command
M = @(q) [1,2*q,0,0;q^2,1,0,0;0,0,1,0;0,0,0,1];
In the MATLAB Workspace Window, notice that M is a function handle. But, if you do
M1 = M(1),
then M1 is a 4-by-4 double. (Search the documentation for function handle for more info.)
Now, I will plot the (1,1) element of M inverse as a function of the parameter p.
ps = 0:0.1:5;
oneOneElement = zeros(size(ps)); % allocate memory for the (1,1) element
for p = 1:length(ps)
tempMatrix = inv(M(p));
oneOneElement(p) = tempMatrix(1,1);
end
figure,plot(ps,oneOneElement,'k.-')
2 Kommentare
Kye Taylor
am 8 Apr. 2012
My pleasure. Function handles, of which M is known as an anonymous function, are super handy. Especially when you start getting fancy with arrayfun, bsxfun, etc. Check those out too.
Weitere Antworten (1)
Walter Roberson
am 7 Apr. 2012
M=A*x.^2+B*x+C
2 Kommentare
Walter Roberson
am 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);
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!