% This Matlab code solves 1D two-point value problem using SOR(w) method % % u_xx = f(x) % on a square, 0<= x <= 1. % with Dirichlet BC which is given for u(1), u(n+1). % function [k,u] = poisson_sor(n,h,w,f,u0,tol) % u'' - q(x) u = f, a< x < b, % ue = sin (alf x), q(x) = (1+x^2), f(x) = ( - (alf)^2 - (1+x^2) ) sin (alf x) % Output: k: the number of iterations. % u: The computed solution. clear all; close all alf = 2; a=0; b=pi; n = 200; h=(b-a)/n; tol = 10^(-7); w=1.2; x=a:h:b; for i=1:n+1 f(i) =( - alf*alf - (1+x(i)*x(i)) )* sin (alf* x(i)); ue(i) = sin (alf* x(i)); u0(i) = 0; end error = 1000; u = u0; k = 0; while error > tol for i=2:n qt = (1+x(i)^2); u_temp = (u(i-1) + u(i+1) ) - h^2*f(i); u(i) = (1-w)*u0(i) + w*u_temp/( 2 + h^2* qt); end error = max(abs(u-u0)); u0 = u; k = k+1; end soln_err = max(abs(u-ue)), k