Finite Difference Matrix Help

조회 수: 33 (최근 30일)
Justin Yeung
Justin Yeung 2020년 2월 16일
편집: Fabio Freschi 2020년 2월 21일
So I have a finite difference problem with beam bending. I am trying define a matrix that follows the 4th order ODE for a Central Difference formula.
n= 10 %number of nodes in the beam
%% Step 2: Define the A Matrix
A = (((2*eye(N) +...
diag(ones(N-1,1),1)))+...
diag(ones(N-1,1),-1));
Now this generates a matrix of size N with '2' along the main diagonal. However, a 4th order ODE is different. So I guess my question is, is there a way to add in values '2' off the main diagonal? I've been trying to mess around with the code above, but it keeps saying the "matrix size dimensions must agree.
  댓글 수: 2
Sindar
Sindar 2020년 2월 16일
check out spdiags. The first example creates the second order second derivative
Srivardhan Gadila
Srivardhan Gadila 2020년 2월 20일
@Justin Yeung are you looking for a matrix which has zeros as diagonal elements, 2's as non-diagonal elements and add this matrix to some other matrix? If not, can you please be more specific or can you give an example?

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

답변 (1개)

Fabio Freschi
Fabio Freschi 2020년 2월 20일
편집: Fabio Freschi 2020년 2월 21일
Edit: I changed my answer including a reference and the second order derivative
The coefficients for central differences of different order of accuracy with uniform spacing can be found on wikipedia here.
I assume you need a second order derivative. If it is the case, you can build the matrix using spdiags:
N = 10;
% coefficients (Derivative 2, Accuracy 4 of the wikipedia table)
C = [ones(N,1)/12 4*ones(N,1)/3 -5*ones(N,1)/2 4*ones(N,1)/3 ones(N,1)/12];
% positions along the diagonal
idiag = -2:2;
% matrix
A = spdiags(C,idiag,N,N);
Remember to divide the matrix by the step size dx^2.
The matrix created in this way is sparse (as it is usually done with these problems).

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by