where is the wrong in this equations between for loop

1 次查看(过去 30 天)
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 个评论
Mary Jon
Mary Jon 2013-9-16
I think there is error because i have't any true results the initial condition is zeros and the results zero too,that meaning the equation not working!!!!,I am not getting an error message, no tried using the debugger
Image Analyst
Image Analyst 2013-9-17
编辑:Image Analyst 2013-9-17
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/

请先登录,再进行评论。

采纳的回答

Arthur
Arthur 2013-9-16
Your loop doesn't seem to change anything. You only use i and j for indexing, and all your values for E will be 8.85*10.^(-12) or 8.85*10.^(-11). Therefore, your value of v_now won't change.
  2 个评论
Mary Jon
Mary Jon 2013-9-16
what is thing i do to make the values of v_now change from initial condition to the convergence
Arthur
Arthur 2013-9-16
Use i and/or j inside your loop to change one of the values, for instance. But which, what and how entirely depends on what you want to achieve.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-9-16
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 个评论
Walter Roberson
Walter Roberson 2013-9-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by