How to invert a matrix that has variables?

Hello everybody, I'm trying to invert a matrix in which every entry depends on the variable h. If I experiment in a general case and do:
syms h
A=[1*h (2-h)*3;(2-h)*3 0]
B=inv(A)
I am able to recieve the inverse matrix of A because it is a square symmetric matrix. However, in my specific case I have:
w=ones(2,1);
p=5;
x=w.^(1/p);
S=rand(1,2);
syms h
alpha=x+h*S';
w_alpha=alpha.^p;
K_total=sym(zeros(8));
C_1=rand(3);
C_1=(C_1+C_1')/2;
C_2=rand(3);
C_2=(C_2+C_2')/2;
C=w_alpha(1)*C_1+w_alpha(2)*C_2;
B=rand(3,8);
g_L_ele=[1 2 5 6 7 8 3 4];
K_total(g_L_ele,g_L_ele)=...
K_total(g_L_ele,g_L_ele)+...
B'*C*B;
K=vpa(K_total,2);
K=(K+K')/2;
inv(K)
Even though it's still a square symmetric matrix, that only depends on h, I can't get the inverse. Can anybody help me please? I have a final project due to tomorrow and this is the only part I'm missing in order to complete it.

2 Comments

K_total is undefined, although you have defined a variable named K_total_h. So of course your code must fail.
Note that reducing the matrix to 2 digits is pure insanity when trying to compute an inverse. Could that also be part of your problem? How can I know?
Oh my bad. K_total_h was supposed to be K_total in the code I'm sorry, already edited. However, it was just a typo passing it from my code to the website, it still doesn't work... I used only 2 digits precisely because it wasn't working with more and I thought it would help but no luck...

Sign in to comment.

 Accepted Answer

rank(C) is 3, so rank(B'*C*B) will be 3, so inv() can only work the vpa(K_total,2) accidentally introduces creates rank 8 out of the rank 3 matrix.

2 Comments

I see... So is there any way to compute the inverse of the Matrix considering that I have a variable there or none at all? Because the same code but with a scalar instead of "h" gives a valid answer
You only get a result by accident due to floating point round off. Look at:
w = sym(ones(2,1));
p = sym(5);
x = w.^(1/p);
S = sym(rand(1,2));
%syms h real
h = sym(rand);
alpha = x+h*S';
w_alpha = alpha.^p;
K_total = sym(zeros(8));
C_1 = sym(rand(3));
C_1 = (C_1+C_1')/2;
C_2 = sym(rand(3));
C_2 = (C_2+C_2')/2;
C = w_alpha(1)*C_1 + w_alpha(2)*C_2;
B = sym(rand(3,8));
g_L_ele = [1 2 5 6 7 8 3 4];
K_total(g_L_ele,g_L_ele) = ...
K_total(g_L_ele,g_L_ele) + ...
B'*C*B;
K = double(K_total);
K=(K+K')/2;
rank_K = rank(K)
invK = inv(K)
rank_invK = rank(invK)
in this could, which runs with higher precision until just before doing the inv, you can see that rank(K) is 3 even though a scalar value was used for h. From this we can conclude that if K is rank 8 in your code, it is only due to loss of precision at some earlier step.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!