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

% 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));

 Respuesta aceptada

Matt Fig
Matt Fig el 12 de Jun. de 2011

1 voto

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 comentarios

Ambreen
Ambreen el 12 de Jun. de 2011
i have to start the indexing from 0,because the first point is U(0,0) ...so now how i can i write it???
also i use same a and b because i ineed U(a,b) to be called in the formula many times ..is it wrong way ???
Ambreen
Ambreen el 12 de Jun. de 2011
well i am a beginner to MATLAB ,i don't really know if i am writting unnecessary loops....i just tried to write what the code is like..so...
Matt Fig
Matt Fig el 12 de Jun. de 2011
You cannot start the indexing at 0 in MATLAB, as you have been told before. That doesn't mean that you cannot solve your problem in MATLAB. If the first point is 0, then this maps to 1. If the second point is 1, then this maps to 2, etc...
U(1,1) % This represents point (0,0)
U(1,2) % This represents point (0,1)
...
U(I,J) % This represents point (I-1,J-1)
Ambreen
Ambreen el 12 de Jun. de 2011
oh now i got it ...thank you so much :) and the last point will be written as U(I+1,J+1) %that represents (I,J) ????
Matt Fig
Matt Fig el 12 de Jun. de 2011
Give a very small example of what your array should look like, say for these:
s1 = 1;
s2 = 2;
s3 = 3;
s4 = 4
n = 5;
Show what you think the final array should look like and perhaps we can find the way to calculate it efficiently.
Ambreen
Ambreen el 12 de Jun. de 2011
ok for n=5
there are 36 total points on grid (as its from 0 to 5 ,in length and width of grid )
and the known points are the outer boundary points of grid ,that is there are 20 known points,to which 5 points have value "1" as you suppose,other 5 have value "s",and next 5 points have value "3",next have "4"
now comes the internal points of the grid that are unknown ....that is 4x4 =16 unknown points...
for these we assign value 0 initially...and then using the formula
U(i,j)iteration=(1/4)*(U(i+1,j)+U(i-1,j)+U(i,j+1)+U(i,j-1))
it evaluates using the previous defined points of grid...and this iteration next time uses the new obtained result in the formula and it continues untill the results start to repeat ...and then the actual value of result at unkown points come...
thats my idea to write the code...do you think its correct???
Matt Fig
Matt Fig el 12 de Jun. de 2011
O.k, so show the initial grid, don't just describe it.
U = [1 2 3 4 5;1 0 0 0 5;1 0 0 0 5;1 0 0 0 5;1 0 0 0 5;1 2 3 4 5]
is that correct? What is s? Once we have the initial grid and the above iteration formula, I think we can get it...
Ambreen
Ambreen el 12 de Jun. de 2011
that s was written by mistake ..its actually"2"
Matt Fig
Matt Fig el 12 de Jun. de 2011
So s is 2... great! Now please show the initial grid.... Like what I did...
Ambreen
Ambreen el 12 de Jun. de 2011
ok i have draw the grid with initial values,but i dont know how to show you that picture over here ???is there a way???i do not know how i can write it in matrix form...like you do ..but by viewing the grid you would surely get what i am trying to tell
Ambreen
Ambreen el 12 de Jun. de 2011
ok please watch that link ...i put that grid here
http://www.mypicx.com/06122011/grid/
Matt Fig
Matt Fig el 12 de Jun. de 2011
You will have to put into matrix form to solve it. To put it into matrix form, just look at what I did. Replace the values, add rows and columns if you need to do so. Did you execute the U I gave above and look at it in the command window?
Matt Fig
Matt Fig el 12 de Jun. de 2011
I see the image, but you have what looks like coordinates at each node of the grid. What are these coordinates to? Ultimately you must translate the idea to matrix form to solve it. MATLAB won't solve it by descriptions or pictures...
Ambreen
Ambreen el 12 de Jun. de 2011
i know it must be solved in matrix form,like you wrote it...but i can't build the correct matrix ,i am trying to make it by the idea you gave me ..
Ambreen
Ambreen el 12 de Jun. de 2011
the code i wrote above is not correct???
Matt Fig
Matt Fig el 12 de Jun. de 2011
I think you will ultimately end up with at most two WHILE loops, assuming you have some convergence criterion.
Ambreen
Ambreen el 12 de Jun. de 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 !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by