1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| % Illustration of Gauss-Seidel iteration with relaxation
%
% NON-LINEAR SYSTEM
% f(u)= u"(x) + sin(u(x))=0 x in [0 1]
% u(0)=pi/2 and u(1)=0 ;
% discretization with finit difference
% f_i(u1,u2,..,un) = (u(i+1)+u(i-1)-2*u(i))/h^2+ sin(u(i))=0 for i=1..n
% with n number of points
%
% The goal is to determine a value of u(i) for i=1..n
% we will take an u(i) out of f_i(u1,u2,..,un)
%
% u(i)= g(u(i))=(u(i+1)+u(i-1))/2+(2/h^2)*sin(u(i));
%
clear all;
clc;
T=10; % the interval [0 T]
h=.005; % number of points
n=T/h;
w=0.1; %relaxation factor
for i=1:n
u(i)=.1;
%u=rand(n,1);
end
for iter=1:30
temp(1)=(u(2)+pi/2)/2+(h^2/2)*sin(u(1));
u(1) = u(1) + w*(temp(1) - u(1));
for i=2:n-1
temp(i)=(u(i+1)+ u(i-1))/2+(h^2/2)*sin(u(i));
u(i) = u(i) + w*(temp(i) - u(i));
end
temp(n) = ( u(n-1))/2+(h^2/2)*sin(u(n));
u(n) = u(n) + w*(temp(n) - u(n));
end
for i=1:n
t(i)=i*h;
end
plot(t,u);grid on; |
Partager