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
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!