Need help with jacobi iterative method computation - matlab code

I'm having trouble getting my code to work for a jacobi iteration problem. The problem is for a class project titled Numerical solution of heat equation on a 2D rectangular plate, here's a pic of the guidelines:
heres my code:
%%Part 1- Define N+1 by 2N+1 matrix
n=10 ;
old_Temp=zeros(n+1,2.*n+1) ;
old_Temp(:,(2.*n+1))=1 ;
old_Temp(n+1,:)=1 ;
old_Temp;
new_Temp=old_Temp;
% Part 2- Define 3 loops
% outermost loop
No=10;
tol=(1*10^-6);
iterations=0;
old_Temp=new_Temp;
while abs(new_Temp-old_Temp) <= tol % convergence check
for i=2:2*N % inbetween loop
for j=2:N % innermost loop
new_Temp(i,j) = (old_Temp(i+1,j) + old_Temp(i-1,j) + old_Temp(i,j+1) + old_Temp(i,j-1))/4; %jacobi computation
end
end
old_Temp(i,j)=new_Temp(i,j);
iterations=iterations+1; % iteration counter
end
| I can't seem to figure out what's wrong with it...it keeps giving me the error
Attempted to access old_Temp(12,2); index out of bounds because size(old_Temp)=[11,21].|

Réponses (1)

Walter Roberson
Walter Roberson le 13 Mai 2015
You create a (10+1) * (2*10+1) matrix, but your first index into it, "i", is set to go up to 2*N and your second index "j" is set to go up to N. It appears that you have the limits of the two "for" loops reversed.

5 commentaires

Hey walt, I actually made a little bit of headway since I posted this, however I have a new problem now. Here's my new code:
% Numerical Solution of Heat Equation
% on a 2-D Rectangular Plate
%%Part 1- Define N+1 by 2N+1 matrix
n=10 ;
old_Temp=zeros(n+1,2.*n+1) ;
old_Temp(:,(2.*n+1))=1 ;
old_Temp(n+1,:)=1 ;
old_Temp;
new_Temp=old_Temp;
% Part 2- Define 3 loops
% outermost loop
N=10;
tol=(1*10^-6);
iterations=0;
old_Temp=new_Temp;
delta=0;
while delta <= tol % convergence check
for j=2:2*N % inbetween loop
for i=2:N % innermost loop
new_Temp(i,j) = (old_Temp(i+1,j) + old_Temp(i-1,j) + old_Temp(i,j+1) + old_Temp(i,j-1))/4; %jacobi computation
end
end
x=mean(new_Temp(:,N));
y=mean(old_Temp(:,N));
delta=abs(x-y);
old_Temp(i,j)=new_Temp(i,j);
iterations=iterations+1; % iteration counter
iterations
% [C,h]=contour(new_Temp,10)
% clabel(C,h)
% title('number of iterations, convergence criterion')
% xlabel('abcd')
% ylabel('efgh')
% pause(1/24)
end
new_Temp
My problem is that it goes through one iteration but then stops and I believe it's because my convergence check is wrong. If you could look at it and maybe tell me what's wrong I'd appreciate it. The convergence criteria
" must be the absolute difference of the following value between two successive iterations:
the average value of computed temperature on horizontal line j=N "
Have you considered using
conv2(old_Temp, [0 1 0; 1 0 1; 0 1 0]/4, 'same')
instead of your double-for loop?
You know that the
old_Temp(i,j)=new_Temp(i,j);
is taking place after the double "for" loop has ended, so the values of "i" and "j" are whatever they happened to last be as a result of the loop? In particular i will be 2*N and j will be N ? So you are modifying only the bottom right hand corner ? But your delta is based upon the middle column, which isn't going to change?
The only reason I am using the double for loop is because the project guidelines want the algorithm written out a certain way.
You could be doing the entire matrix in one step, without even using conv2(). Ah well. In any case look at the other issues I raised.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by