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

1 回表示 (過去 30 日間)
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;
[EDITED, please format your code properly, thanks]

回答 (1 件)

Jan
Jan 2012 年 11 月 12 日
編集済み: Jan 2012 年 11 月 12 日
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?

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by