Please need help to detect the error in my code...

1 visualizzazione (ultimi 30 giorni)
% TO WRITE A CODE FOR LAPLACE HEAT FLOW EQUATION FOR ANY ORDER nxn OF GRID,USING ITERATION METHOD ,TO FIND TEMP. AT UNKNOWN POINTS OF GRID
s1=input('Enter the temperature for the top of the 3x3 grid in degress:');
s2=input('Enter the temperature for the bottom of the 3x3 grid in degress:');
s3=input('Enter the temperature for the left of the 3x3 grid in degress:');
s4=input('Enter the temperature for the right of the 3x3 grid in degress:');
n=input('Enter n ');
for b=0:n-1
a=0;
s1=U(a,b);
% assigning respective temps. to boundry points
for a=0:n-1
b=n;
U(a,b)=s4;
for b=1:n
a=n;
U(a,b)=s3;
for a=1:n
b=0;
U(a,b)=s4;
for a=1:n-1
for b=1:n-1 %assigning zero as initial value to the % unknown internal points of grid
U(a,b)=0;
for k=0:200
for a=1:n-1
for b=1:n-1
U_it(a,b)=(1/4)*(U(a+1,b)+U(a-1,b)+U(a,b+1)+U(a,b-1));
disp('------------Iteartion ---------------');
k=k+1;
disp(U_it_(a,b));
if (abs(U(a,b)-U_it(a,b))<0.000001) %condition
U(a,b)=U_it(a,b);
break %stop iterations here
else
U(a,b)=U_it(a,b); %continue doing this process
end
end
end
end
end
end
end
end
end
end
fprintf('temp. at the unknown point is %g ',U(a,b));

Risposta accettata

Matt Fig
Matt Fig il 12 Giu 2011
The error is that you are indexing into the array U with 0. There may be logical errors involved as well. You have nested loops with the same loop index (a and b). Usually this is not desired. Did you really mean to do this? Are 9 layers of nesting really necessary for your problem?
  17 Commenti
Matt Fig
Matt Fig il 12 Giu 2011
I think you will ultimately end up with at most two WHILE loops, assuming you have some convergence criterion.
Ambreen
Ambreen il 12 Giu 2011
hmmmm ok i will try all the ideas you gave ...but still confused what to do ..and i have to submit this code tomorrow in my university ...aaahh much worried !!!!
well thanx a lot !

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by