# Laplace Eq. Numerical Sol. with SOR Method-MATLAB Code

 Register Blogs Members List Search Today's Posts Mark Forums Read

 April 3, 2020, 15:27 Laplace Eq. Numerical Sol. with SOR Method-MATLAB Code #1 New Member   jist Join Date: Feb 2020 Posts: 3 Rep Power: 6 Hi all, I've been given analytical solution of U=x^2-y^2 which satisfies the Laplace Eq. and its numerical solution as S for a square domain. Boundary conditions setted from analytical solution 'U' and unchanged through the code. Internal points of S taken as 0 at initial. Then i am asked that to perform 20 iterations of SOR method with relaxation factor (omega) changing 1 to 2 with an interval of 0.002. I should've been able to calculate error between analytical and numerical solution for each relaxation factor at the end of 20th iteration with given MATLAB code below but errors do not match with the solution at all. Can anybody tell me what is it that i am doing wrong? Thanks in advence. Here is my code, Code: clc close all clear all N=21; iteration=20; deltax=1/(N-1); deltay=deltax; delomega=0.002; omegai=1; Nomega=1/(0.002); x=linspace(0,1,21); y=linspace(0,1,21); omega=linspace(1,2,Nomega); %analytical solution u=zeros(N,N); for j=1:N for i=1:N u(i,j)=x(i)^2-y(j)^2; end end s=zeros(N,N); s(1,:)=u(1,:); s(N,:)=u(N,:); s(:,1)=u(:,1); s(:,N)=u(:,N); error=0; for k=1:Nomega for l=1:iteration for j=2:N-1 for i=2:N-1 R=s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1)-4*(s(i,j))-(deltax^2)*s(i,j); s(i,j)=s(i,j)+0.25*(1+delomega*k)*R; end end if l==iteration error=error+abs(u(i,j)-s(i,j)); %error_(k,1)=omega(k); error_(k)=error/((N-2)*(N-2)); end end end plot(omega(:),error_(:)); xlabel('\omega'); ylabel('Error'); grid on grid minor

 April 3, 2020, 15:30 #2 Senior Member   Filippo Maria Denaro Join Date: Jul 2010 Posts: 6,817 Rep Power: 73 First, correct "U=x^2+y^2" in "U=x^2-y^2", otherwise the solution does not satisfy the Laplace euqation. Then, why do you think to get an accurate solution after only 20 iterations??

April 3, 2020, 15:52
#3
New Member

jist
Join Date: Feb 2020
Posts: 3
Rep Power: 6
Sorry for the typo, it was x^2-y^2 as stated in the code.
Well this part is not about how to get an accurate solution via iteration. I will simply run the code for smaller mesh sizes to see how mesh sizes impact the solution.

Quote:
 Originally Posted by FMDenaro First, correct "U=x^2+y^2" in "U=x^2-y^2", otherwise the solution does not satisfy the Laplace euqation. Then, why do you think to get an accurate solution after only 20 iterations??

April 3, 2020, 16:19
#4
Senior Member

Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,817
Rep Power: 73
Quote:
 Originally Posted by jist Sorry for the typo, it was x^2-y^2 as stated in the code. Well this part is not about how to get an accurate solution via iteration. I will simply run the code for smaller mesh sizes to see how mesh sizes impact the solution.

You wrote "but errors do not match with the solution at all."

April 3, 2020, 17:44
#5
New Member

jist
Join Date: Feb 2020
Posts: 3
Rep Power: 6
I have the solution plot (error as a function of omega and grid sizes) for the scenario i explained but i am not getting the same results. What i tried to ask was that am i doing anything wrong with SOR or the error between analytical and numerical solution?
Quote:
 Originally Posted by FMDenaro You wrote "but errors do not match with the solution at all."

 April 3, 2020, 18:02 #6 Senior Member   Filippo Maria Denaro Join Date: Jul 2010 Posts: 6,817 Rep Power: 73 You have a 2D map of the error as function of two variables. Plot the contour and the surface and check for the values producing the minimum error. Be aware that you cannot see a decreasing of the error for smaller mesh sizes.

 Tags laplace equation, matlab code, relaxation factor, sor