Some technical issues of Laplace Numerical Equations
1 次查看(过去 30 天)
显示 更早的评论
Hi there, I got some error message from the coding.
The codes are shown below.
When I run with it, it says I have the message as
Index in position 1 exceeds array bounds (must not exceed 1).
Error in section_2_b (line 42)
unkp1 = 0.25*(un(j+1,i)+un(j-1,i)+un(j,i+1)+un(j,i-1));
I am not sure how to figure it out : (
The method I am using is based on ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/305147/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/305147/image.png)
clear;clc;close all;
T1 = -5; % degree celcus
T2 = -10; % degree celcus
L = 3; % metres [m] % length of the x dimensions
H = 1; % metres [m] % length of the y dimensions
nfs = 100; % Number of Fourier terms
% Initialise error and set tolerance for convergence
err = 1;
tol = 1e-8;
k = 0; % iteration counter
%% Method B
% Set the dx and dy
n = 1;
h = 0.1/(2^n); % dx and dy has the same value
dx = h;
dy = h;
nx = L/h + 1; % Number of grid points in x
ny = H/h + 1; % Number of grid points in y
x = linspace(0,L,nx); % Vector of grid points in x
y = linspace(0,H,ny); % Vector of grid points in y
% Initialise the exact, numerical method
un = zeros(ny,nx);
unkp1 = zeros(ny,nx);
% Set boundary conditions
un(:,1) = 0; % Left boundary
un(:,nx) = 0; % Right boundary
un(1,:) = -4*T2*x.*(x-L)/L/L; % Bottom boundary
un(ny,:) = -4*T1*x.*(x-L)/L/L; % Top boundary
unkp1 = un;
% interate Jacobi Method(known as method B) till convergence
while err > tol
% update itteration counter
k = k + 1;
% loop with computaional nodes
for i = 2:nx - 1
for j = 2:ny - 1
unkp1 = 0.25*(un(j+1,i)+un(j-1,i)+un(j,i+1)+un(j,i-1));
end
end
% calculate the error
err = sqrt(sum(sum((un-unkp1).^2)));
% update un
un = unkp1;
end
disp(un);
0 个评论
采纳的回答
KSSV
2020-5-29
Replace this line:
unkp1 = 0.25*(un(i+1,j)+un(i-1,j)+un(i,j+1)+un(i,j-1));
with
unkp1(i,j) = 0.25*(un(i+1,j)+un(i-1,j)+un(i,j+1)+un(i,j-1));
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!