How to write the linear equations in the form of matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a linear equations: -i*c*sqrt((nx+1)*ny)*u_{nx+1,ny-1}+i*c*sqrt(nx*(ny+1))*u_{nx-1,ny+1}=E*u_{nx,ny}, where nx,ny=0,1,2,3,...,N are integers, c is a constant. As well known, these linear equations can be written in the form of matrix that is A*u=E*u, with u=(u_{0,0},u_{0,1},u_{0,2},...,u_{1,0},u_{1,1},u_{1,2},...u_{nx,ny},...u_{N,N})^T, and A is a matrix with dimension (N+1)*(N+1). I want to know how to construct the matrix A fastly for a very large N in the Matlab. Thanks very much.
0 Kommentare
Antworten (1)
Torsten
am 29 Dez. 2021
Bearbeitet: Torsten
am 29 Dez. 2021
If you want it fast, you will have to define the matrix once by hand. Number the unknowns
U1 = u_{0,0}, U2 = u_{0,1},...,U(N+1)^2 = u_{N,N}
Proceed the same way with the equations EQN1,EQN2,...,EQN(N+1)^2 for U1,U2,...,U(N+1)^2.
Then your matrix will have an entry C in position (i,j) if the coefficient in front of Uj in EQNi equals C.
A slower, but more comfortable way is to write your equations from above as a function:
function res = fun(u)
and define
res(nx,ny) = E*u(nx,ny) - (-i*c*sqrt((nx+1)*ny)*u(nx+1,ny-1)+i*c*sqrt(nx*(ny+1))*u(nx-1,ny+1))
Then use "jacobianest" from the file exchange (or some faster routine) to calculate the Jacobian matrix A of the
function defined in "fun".
0 Kommentare
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!