Вы находитесь на странице: 1из 2

Lab 4, Iterative methods for solving linear systems

Topics
Mathematics: solving 3 3 linear systems by Jacobi and GaussSeidel methods. MATLAB: writing code for iterative methods for solving linear systems; using ags to indicate convergence.

Jacobi method
Consider the linear system Ax = b where 4 A= 1 2

1 1 2 3 , 1 3

5 b = 5 . 3

Start by writing down the system of linear equations on paper. Rearrange the equations into a suitable form for the Jacobi method. Write a function M-le myjacobi.m to carry out one step of the Jacobi method for a given initial estimate x(0) . Think about what the input/output variables for your function should be. Test your function using an initial estimate of x(0) = (0, 0, 0), checking the output with a hand calculation if necessary. Modify your function M-le so that: It carries out a series of iterations of the Jacobi method instead of just one iteration. It stops once it has either carried out the maximum number of iterations niter, or the iterations have converged within a specied tolerance tol, i.e. the relative change between successive iterations x(k) and x(k+1) is less than tol (just like in lab 4): x(k+1) x(k) x(k+1)

< tol.

The maximum number of iterations and the acceptable tolerance are passed as inputs to the function. It returns a table of the results, the k th row of which contains x(k) , the k th approximation to the solution. Hint: Refer to your multidimensional Newtons method function le (lab 4) as a model, particularly for the covergence test. Try your function le out using dierent values of niter and tol to make sure it is doing what it should do. Checkpoint: Print out the table returned by your function M-le for niter=100 and tol=1e-6. Check that the solution from your program is correct. Now modify your function le so that it tells you whether or not convergence has occurred. True/false information like this is usually conveyed using ag variables. For example, you could set up a new output variable convergeflag, where: convergeflag=1 if the method converged in the specied number of iterations; convergeflag=0 if the method failed to converge in the specied number of iterations. Write a short M-le mysolve.m that calls your function le, tests the value of the output ag and displays an appropriate message: 13

either Converged to x=... Number of iterations carried out =...; or Failed to converge. Number of iterations carried out = .... Hint: The command [m, n] = size(D) returns the size of the matrix D into the variables m and n. For example, if D is a 3 4 matrix, it would set m=3 and n=4. You may nd this command useful for nding out how many iterations your function actually carried out. The appropriate command for displaying a message like this is fprintf(Converged to x = (%7.4f, %7.4f, %7.4f). ...) Number of iterations carried out = %3d\n,

Checkpoint: Run your M-le mysolve.m twice, using dierent values of niter and tol, once with convergence happening and once with convergence not happening.

GaussSeidel method
Write a function M-le myGS.m that does the same thing as myjacobi.m using the GaussSeidel method instead of the Jacobi method (you can use myjacobi.m as a model so you dont have to start from scratch). Modify mysolve.m so that it calls your GaussSeidel function instead of your Jacobi function. Checkpoint: Run your M-le mysolve.m twice, using the values of niter and tol that you used for the previous checkpoint. Compare the Jacobi and GaussSeidel methods for this linear system.

Practice test question


Modify your GaussSeidel function so that it works on any 3 3 system. The new function should therefore take two extra input variables: the matrix A and right-hand side vector b. Modify mysolve.m appropriately so that it calls your function to try to solve Ax = b where b is as above and 4 2 1 1 2 1 1 1 . (i) A = 3 6 1 , (ii) A = 3 4 1 6 4 1 1 Checkpoint: Run mysolve.m on the above two systems. What happened and why? Check your solutions are correct.

14

Вам также может понравиться