Finite Difference Matrix Help
34 次查看(过去 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 个评论
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 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!