Finite Difference Matrix Help
15 ビュー (過去 30 日間)
古いコメントを表示
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
2020 年 2 月 16 日
check out spdiags. The first example creates the second order second derivative
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
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).
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!