where is the wrong in this equations between for loop
Show older comments
clear
clc
w=1.8;
n=111;
m=33;
hx= 4/32 ; %space betweem nodes in x direction
hy=20/110+1;
h=hy/hx;
E0=8.85*10^(-12);
v_now= zeros(n,34);
d=2:33;
v_now(110,d)=100;
v_now(105,14:21)=100;
v_prev= zeros(n,34);
v_now(:,34)=v_now(:,2);
iter=0;
[x y]=ndgrid(1:111,1:32);
error=(sum(sum((abs(v_now-v_prev))/m*n)));
while(error>0.01)%Run this until convergence
[x,y]= meshgrid(1:34,1:111)
iter=iter+1; % Iteration counter increment
*for i=105
for j=2:12
E (i, j)= 8.85*10.^(-12);
E (i-1,j)=8.85*10.^(-12);
E (i-1, j-1)=8.85*10.^(-11); %%permittivity of interface region
E (i, j-1)=8.85*10.^(-11);
a=(E (i, j)+E (i, j-1));
b=(E (i-1,j)+E (i-1, j-1));
c= E (i, j)+E (i-1, j);
d=E (i, j-1)+E (i-1, j-1);
W1 (i, j)=(h.^2).*a;
W2 (i, j)=(h.^2).*b;
W3 (i, j)=c;
W4 (i, j)=d;
W (i, j)=a+b+c+d;
v_now (i, j)= (1-w).*(v_prev (i, j))-w*(W1 (i, j).*(v_prev (i+1, j))+W2 (i, j).*(v_now (i-1, j))+W3 (i, j).*(v_prev (i, j+1))+W4 (i, j).*(v_now (i, j-1)))/(W (i, j));
end
end*__******
*
[x y]=ndgrid(1:111,1:34);
error=(sum(sum((abs(v_now-v_prev))/m*n)));
v_prev=v_now; % Updating previous iteration matrix to the last iteration performed
surf(x,y,v_now)
set(gca)
title('potential ')
xlabel('i')
ylabel('j')
zlabel('v_now')
end
3 Comments
Walter Roberson
on 14 Sep 2013
What leads you to think that there is an error in those equations? Are you getting an error message, or is the result just not what you expect? Have you tried using the debugger ?
Mary Jon
on 16 Sep 2013
Image Analyst
on 17 Sep 2013
Edited: Image Analyst
on 17 Sep 2013
It's customary, recommended, and more time efficient to use the debugger before asking us. I debug for hours everyday and if every time something looked weird I had to post something and wait for an answer, instead of solving it myself with the debugger, it would take forever to write programs. Don't you want to learn this crucial skill? If so, view Doug Hull's excellent video: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Accepted Answer
More Answers (1)
Walter Roberson
on 16 Sep 2013
Is your loop executing at all?
error=(sum(sum((abs(v_now-v_prev))/m*n)));
Shouldn't you be dividing by (m*n) instead of dividing by m and multiplying the result by n ? Have you checked that the initial error is in fact greater than 0.01 ?
Are you looking for the average absolute difference? Or the RMS (root mean squared) ?
error = mean( abs(v_now(:) - v_prev(:)) ); %average absolute difference
error = sqrt( sum( (v_now(:) - v_prev(:)).^2 ) ); %RMS
Caution: do not use "error" as a variable: it is the name of an important routine.
3 Comments
Mary Jon
on 16 Sep 2013
Edited: Walter Roberson
on 17 Sep 2013
Walter Roberson
on 17 Sep 2013
I have having difficulty following those calculations mentally, especially since there is no comment indicating the purpose of the calculation.
When I visually compare the code gone through for j=2:12 and j-22:32, it looks to me to be the same. Is it? If it is, then combine the code into a single block with
for j = [2:12, 22:32]
I note that your calculations for v_now(i,j) include a term which is v_now(i, j-1), so the calculations are order dependent -- the calculation for (105,2) depends in part upon the value at (105,1), and then in the next iteration, (105,3) depends in part upon the value just calculated at (105,2), and so on. And the calculation for (106,2) done first thing in the while loop, depends in part upon the value at (105,2) which has not been updated yet in this iteration of the while, but the calculation at the end of the while loop for (104,2) depends upon the fact that (105,2) has already been updated. "Captain, in view of the alternatives, are you sure this is wise??". Would it perhaps make more sense to write into new_vnow() at each step and then at the bottom of the while loop, update v_now = new_vnow? In this way, each calculation would depend upon the node values as of the previous while iteration, instead of depending on the fine details of the order you update inside the while loop.
Categories
Find more on Symbolic Math Toolbox 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!