next up previous
Next: Use a MATLAB function Up: tutorial3 Previous: The fixed-point iteration method

Coding the fixed-point iteration algorithm


It is clearly laborious and time-wasting to carry out each iteration by hand. Therefore in this section we will add code to iteration.m that will carry out the iteration procedure automatically.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroup The first thing to do is to add a MATLAB input statement to iteration.m that asks you to input the starting value for the iteration i.e. \bgroup\color{black}$x_0$\egroup. In MATLAB you must call this x(1) as MATLAB does not recognise zero indices for vectors. It is also useful to add the command format long at this stage, in order to be able to check the accuracy of your results.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroup Now include a for-loop running from \bgroup\color{black}$n=1$\egroup to \bgroup\color{black}$n=20$\egroup that will calculate the next twenty iterates \bgroup\color{black}$x_1, x_2
\dots, x_{20}$\egroup. Remember that in MATLAB these will be the vector elements x(2), x(3), ..., x(21). You should also include a line of code like disp([ n+1 x(n+1) ]) (see Displaying output) that will print the iterate number and the new iterate each time through the loop.

Save iteration.m and run the program, starting with a value \bgroup\color{black}$x_0=0.75$\egroup.

The process of fixed-point iteration is only useful if the iterates \bgroup\color{black}$x_n$\egroup converge to the true solution \bgroup\color{black}$x_{exact}$\egroup. In the notes we prove that if successive iterates converge, then the iterates will converge to the true solution.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupThus we need a line of MATLAB code to calculate the error at each iteration step \bgroup\color{black}$n$\egroup using code like error(n+1) = x(n+1)-x(n). Insert this just before your disp command and modify the disp command so that it prints out n+1, x(n+1) and the error.

\bgroup\color{blue}\framebox{\em CHECKPOINT: submit solution}\egroup \bgroup\color{black}$\phantom{0}$\egroup \bgroup\color{red}\framebox{ 3.}\egroup Record the value of the variable error(21) .


next up previous
Next: Use a MATLAB function Up: tutorial3 Previous: The fixed-point iteration method
Charlie Macaskill 2004-07-26