Handout 1, Math 104C, Instructor: Hector D. Ceniceros
Fixed Point Iteration for a 2X2 Nonlinear System
The system in this example is given by 16 4 2 1 16 4 2 1 2 2 2 1 2 2 2 2 2 1 1 1 x x x x x x x x + + = + + + =
in [0,1]x[0,1]. By the theorem seen in class we guarantee the existence of a unique fixed point in [0,1]x[0,1], and fixed point iteration should converge for any initial guess in this domain.
Matlab M-script:
%Fixed point iteration for a 2X2 nonlinear system of equations. % x1 = g1(x1,x2) % x2 = g2(x1,x2) %INPUT: x0=[x10; x20] (initial guess, provide before calling) % TOL (tolerance) % Max_iter (max number of iterations) % Define function g in gfun.m %OUTPUT: % Approximated root or failure message. format long; %x0=[0 ; 0]; TOL=1e-3; Max_iter=100; err=zeros(Max_iter,1); k=0; dif=10000; while k<= Max_iter & dif >= TOL k=k+1; x = gfun(x0); dif= norm(x-x0,inf); err(k)=dif; x0=x; %update iterates end if k > Max_iter warning('Maximum number of iterations exceeded') %failure message end disp('Difference between consecutive iterates') err(1:k) disp('The final approximated root is:') x disp('Number of iterations') k
2
The function gfun is defined in another file (gfun.m)
function g=gfun(x) % Defines a vector-valued function of x=(x1, x2) x1=x(1);x2=x(2); g1= 0.5 + x1/4 + (x1^2 + x2^2)/16; g2= 0.5 + x2/4 - (x1^2 + x2^2)/16; g=[g1 ; g2];
Matlab output: x0=[0;0];
Difference between consecutive iterates ans = 0.50000000000000 0.15625000000000 0.05676269531250 0.01947623677552 0.00634094849831 0.00198743063035 0.00060575898803 The final approximated root is: x = 0.74142307020470 0.59182888292030 Number of iterations k = 7
Another initial guess
x0=[.5;.5];
Difference between consecutive iterates ans = 0.15625000000000 0.05676269531250 0.01947623677552 0.00634094849831 0.00198743063035 0.00060575898803 The final approximated root is: x = 0.74142307020470 0.59182888292030 Number of iterations k = 6
Faster convergence as initial guess is closer to fixed point. 3
We now try the Seidel fixed point iteration method for the same 2x2 system. Here is a Matlab M-script that implements it:
%Seidel Fixed point iteration for a 2X2 nonlinear system of equations. % x1 = g1(x1,x2) % x2 = g2(x1,x2) %INPUT: x0=[x10; x20] (initial guess) % TOL (tolerance) % Max_iter (max number of iterations) % Define function g1 and g2 in this file %OUTPUT: % Approximated root or failure message. x0=[0 ; 0]; TOL=1e-3; Max_iter=100; err=zeros(Max_iter,1); k=0; dif=10000; while k<= Max_iter & dif >= TOL k=k+1; x(1) = 0.5 + x0(1)/4 + (x0(1)^2 + x0(2)^2)/16; %g1(x0(1),x0(2)) x(2) = 0.5 + x0(2)/4 - (x(1)^2 + x0(2)^2)/16; %g2(x(1),x0(2)) dif= norm(x-x0,inf); err(k)=dif; x0=x; %update iterates end if k > Max_iter warning('Maximum number of iterations exceeded') end disp('Difference between consecutive iterates') err(1:k) disp('The final approximated root is:') x disp('Number of iterations') k
Matlab output of Seidel-fixed point iteration
Difference between consecutive iterates ans = 0.50000000000000 0.15528869628906 0.05636663818918 0.01983428337206 0.00678075251804 0.00227778213632 0.00075731011025 The final approximated root is: x = 0.74130546261491 0.59172326140607 Number of iterations k = 7
Convergence is not faster than plain fixed point for this particular example. However, Seidel may often be faster and does not have any extra computational cost.
4
Plotting in Matlab
Often in this course we will need to plot numerical results and error estimates to better visualize them. To illustrate how to plot in Matlab, we now plot the norm of the difference between consecutive iterates:
plot(err(1:k),'o'); xlabel 'iteration';ylabel 'Difference between iterates'
1 2 3 4 5 6 7 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 iteration D i f f e r e n c e
b e t w e e n
i t e r a t e s
Fig 1. Plot of the norm of the difference between consecutives iterates against iterations.
5
Because err gets too small is better to plot it using a logarithmic scale in the y axis. We can do this with the matlab command
semilogy(err(1:k),'o'); xlabel 'iteration';ylabel 'Difference between iterates'
1 2 3 4 5 6 7 10 -4 10 -3 10 -2 10 -1 10 0 iteration D i f f e r e n c e
b e t w e e n
i t e r a t e s
Fig 2. Semi-log plot of the norm of the difference between consecutives iterates against iterations.
Matlab M-Books
This handout was created as a Matlab M-book with the Matlab command notebook under windows. Notebook allows you to access Matlab inside MS-Word so you can create a document (M-Book) wich contains text, Matlab commands, and Matlab output. Check the online documentation at: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/noteboo3.shtml