1190
1190

Reputation: 375

Writing a matrix model in MATLAB

I want to write the following matrix:

matrix

I define n=100 and I generate the following code:

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

Answers (1)

Bilal
Bilal

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

Related Questions