nxn matrix as an input argument in function

조회 수: 36 (최근 30일)
Sarah Molina Esteves
Sarah Molina Esteves 2020년 11월 16일
댓글: Umar 2024년 8월 9일
function x = matrix(b, L, U)
A= L * U
x= inv(A)*b
end
I am trying to write a function that solves for A an matrix, b a known vector and x an unknown vector. How can I make the above work? In what form am I to input b, L and U into the function arguments?
For
b = $ \begin{pmatrix}a\\ b \\ c \end{pmatrix} $.
L =$ \begin{pmatrix} d & e & f\\ g & h & i \\ j & k & l\end{pmatrix} $
U=$ \begin{pmatrix} m & n & o\\ p & q & r \\ s & t & u\end{pmatrix} $
would my input be like this: matrix([a][b][c], [d,e,f][g,h,i][j,k,l], [m,n,o][p,q,r][s,t,u]) ??
  댓글 수: 1
Umar
Umar 2024년 8월 9일
Hi @Sarah Molina Esteves ,
You have already defined a function named matrix that takes three arguments: b, L, and U. The function calculates the matrix A by multiplying L and U and then solves for x using the equation x = inv(A) * b. When calling the matrix function, you need to provide the inputs b, L, and U in the correct format. Since b is a vector and L, U are matrices, you should input them as follows:
b = [a; b; c];
L = [d, e, f; g, h, i; j, k, l];
U = [m, n, o; p, q, r; s, t, u];
x = matrix(b, L, U);
In MATLAB, vectors are defined using square brackets [ ] and semicolons ; to separate elements vertically, while matrices are defined using square brackets [ ] and commas , to separate elements horizontally within rows.I think this is what @Adriano Filippo Inno is trying to tell you. Hope this helps.

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

답변 (1개)

Adriano Filippo Inno
Adriano Filippo Inno 2020년 11월 17일
편집: Adriano Filippo Inno 2020년 11월 17일
I'm a bit confused by your question. Do you need to solve a linear system with the LU method?
If yes, this function will work:
function x = linSys(A, b)
[L,U,P] = lu(A); % LU decomposition
y = L\(P*b);
x = U\y;
end
To use it:
A = magic(5); % define your A here
b = ones(5, 1); % define your b here
x = linSys(A, b);

카테고리

Help CenterFile Exchange에서 Eigenvalue Problems에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by