How to remove the following error: Attempted to access beta(2); index out of bounds because numel(beta)=1.
1 view (last 30 days)
Show older comments
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 Comment
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!