Inverse Matrix for 6x6 matrix with variables

조회 수: 65 (최근 30일)
Luis
Luis 2023년 11월 7일
답변: Steven Lord 2023년 11월 7일
I am trying to find the inverse of the matrix above. Here is my code:
x = symvar('x')
y = symvar('y')
z = symvar ('z')
A=[x y y 0 0 0; y x y 0 0 0; y y x 0 0 0; 0 0 0 z 0 0; 0 0 0 0 z 0; 0 0 0 0 0 z]
b=inv(A)
This is giving me the following error:
Error using inv
Invalid data type. Input matrix must be double or single.
Error in untitled (line 5)
b=inv(A)
Could someone explain to me what I'm doing wrong? I'm fairly inexperienced with matlab and I'm not sure what is wrong. Any information is greatly appreciated. Thank you!

답변 (2개)

Stephen23
Stephen23 2023년 11월 7일
syms x y z
A = [x,y,y,0,0,0; y,x,y,0,0,0; y,y,x,0,0,0; 0,0,0,z,0,0; 0,0,0,0,z,0; 0,0,0,0,0,z];
b = inv(A)
b = 
  댓글 수: 3
John D'Errico
John D'Errico 2023년 11월 7일
You may have written a function of your own, named inv. That would be a truly terrible idea.
James Tursa
James Tursa 2023년 11월 7일
@Luis Type this at the command line:
which inv

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2023년 11월 7일
The symvar function does not define symbolic variables. It identifies identifiers in an expression (other than those in a small list of excluded identifiers) that could be variables and returns those identifiers in a cell array of text data.
x = symvar('x')
x = 1×1 cell array
{'x'}
y = symvar('y')
y = 1×1 cell array
{'y'}
z = symvar ('z')
z = 1×1 cell array
{'z'}
So when you construct A, it is not a symbolic variable. It's a cell array.
A=[x y y 0 0 0; y x y 0 0 0; y y x 0 0 0; 0 0 0 z 0 0; 0 0 0 0 z 0; 0 0 0 0 0 z]
A = 6×6 cell array
{'x'} {'y'} {'y'} {[0]} {[0]} {[0]} {'y'} {'x'} {'y'} {[0]} {[0]} {[0]} {'y'} {'y'} {'x'} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {'z'} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {'z'} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {'z'}
whos x y z A
Name Size Bytes Class Attributes A 6x6 3960 cell x 1x1 106 cell y 1x1 106 cell z 1x1 106 cell
Because of this the error message is correct. The inv function is not defined for cell arrays.
Instead, you should use the sym or syms functions to define symbolic variables (as shown in the answer from @Stephen23) and create A using those symbolic variables.
syms x y z
A=[x y y 0 0 0; y x y 0 0 0; y y x 0 0 0; 0 0 0 z 0 0; 0 0 0 0 z 0; 0 0 0 0 0 z]
A = 
whos x y z A
Name Size Bytes Class Attributes A 6x6 8 sym x 1x1 8 sym y 1x1 8 sym z 1x1 8 sym
The inv function is defined for sym arrays.
AI = inv(A)
AI = 
The fact that the symvar function you called didn't display anything makes me suspect that you might be using a symvar.m file other than the ones in MATLAB, Symbolic Math Toolbox, or Curve Fitting Toolbox. Or did you simply not show us the output that you received when you ran the code? What does this command show you when you run it?
which -all symvar
/MATLAB/toolbox/matlab/funfun/symvar.m /MATLAB/toolbox/symbolic/symbolic/@sym/symvar.m % sym method /MATLAB/toolbox/matlab/funfun/@inline/symvar.m % inline method /MATLAB/toolbox/curvefit/curvefit/@fittype/symvar.m % fittype method

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by