# How to remove the following error: Attempted to access beta(2); index out of bounds because numel(beta)=1.

2 views (last 30 days)
Shashank on 12 Nov 2012
Here is my code. Any sort of help will be appreciated.
a = @(x) -(x+3)/(x+1);
b = @(x) (x+3)/((x+1).^2);
f = @(x) 2*(x+1) + 3*((x+3)/((x+1).^2));
n = 1000; % Number of discretization points
T_d(1) = 5; % Temperature condition at x = 0
T_d(n+1) = 4; % Temperature condition at x = L
h = L/n; % Step size
H = linspace(0,L,n+1);
alpha = a(H)';
beta = b(H)';
func = f(H)'; % Term on the RHS
A = zeros(n+1,n+1);
F = zeros(n+1,1);
A(1,1) = 1;
A(2,1:2) = [beta(2)-(2/(h^2)) ((1/(h^2))+(1/(2*h))*alpha(2))];
A(n+1,n+1) = 1;
F(1) = T_d(1);
F(2) = func(2) - (((1/(h^2))-(1/(2*h))*alpha(2))*T_d(1));
F(n) = func(n) - (((1/(h^2))+(1/(2*h))*alpha(n))*T_d(n+1));
F(n+1) = T_d(n+1);
for j = 3:n-1
A(j,j-1:j+1) = [((1/(h^2))-(1/(2*h))*alpha(j)) beta(j)-(2/(h^2)) ((1/(h^2))+(1/(2*h))*alpha(j))];
F(j) = func(j);
end
T_d = A\F;
Jonathan Epperl on 12 Nov 2012
This operation
(x+3)/((x+1).^2);
should probably be
(x+3)./((x+1).^2);

Jan on 12 Nov 2012
Edited: Jan on 12 Nov 2012
n = 1000;
H = linspace(0,L,n+1);
beta = b(H)';
beta(2) % Exists!
The code you have posted does not create a scalar beta, but numel(beta) is 1001.
Perhaps you did not save the program before running it?