Reputation: 375
I want to write the following matrix:
I define n=100 and I generate the following code:
n=100
D = toeplitz([0 1 zeros(1, n-2) -1]/ (2*h));
but I get a wrong matrix. How can I correct it?
Upvotes: 0
Views: 45
Reputation: 3854
As David said you can use diag as following:
clc clear all close all %------------------ n = 100; h = 5; % Positive Ones A1 = ones(n-1,1); Arr1 = diag(A1,1); % Negative Ones A2 = -ones(n-1,1); Arr2 = diag(A2,-1); % your array Array = (1/(2*h))*(Arr1+Arr2);
Upvotes: 1