how to create a matrix with variables in it?
Ältere Kommentare anzeigen
for eg., to create following matrix and work on it,
A=[x1 x1*x2 ; x1^2 x2^2]
Antworten (2)
Kye Taylor
am 12 Feb. 2013
Bearbeitet: Kye Taylor
am 12 Feb. 2013
The best answer to your question will likely depend on what you want to do with A. Sometimes, I find the following commands useful, which will create an anonymous function, A, that takes two inputs, x1 and x2 and returns the matrix you describe. Of course, this assumes you have values for the variables x1 and x2.
A = @(x1,x2)[x1,x1*x2;x1^2,x2^2]
And compute, for example, with
det(A(1,2))
If you want A to be symbolic, you can begin using the following command
syms x1 x2
A = [x1,x1*x2;x1^2,x2^2]
Then compute, for example, with
det(A)
1 Kommentar
Sergio E. Obando
am 15 Jun. 2024
As of R2021b, you may also be interested in symmatrix: https://www.mathworks.com/help/symbolic/use-symbolic-matrix-variables.html
Aman Kumar
am 23 Mär. 2021
2 Stimmen
syms x1 x2
A = [x1,x1*x2;x1^2,x2^2]
5 Kommentare
Michael Wolfe
am 25 Apr. 2021
How would you make a matrix with both numbers and variables in it?
Walter Roberson
am 25 Apr. 2021
syms x1 x2
A = [x1,x1*pi;.8,x2^2]
Nic
am 15 Jun. 2024
What if I want to create a matrix with expressions?
syms x
A=[4*x^2+8*x+5 -8*x -5;8x -4*x^2-16*x 4*x;s 4*s -9]
Why doesn't it work?
Steven Lord
am 16 Jun. 2024
What does "doesn't it work" mean in this context?
- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
John D'Errico
am 16 Jun. 2024
Let me guess why it does not work. In what you wrote, what is s?
syms x
A=[4*x^2+8*x+5 -8*x -5;8x -4*x^2-16*x 4*x;s 4*s -9]
Do you see that you defined x as a sym, but how about s?
If you define all of the variables properly, of course it will work.
Kategorien
Mehr zu Assumptions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!