1. Code
%% steepest descent method (symmetric positive definite for solving equations) %% linear equations M * X = b, M is a square matrix, X0 is the initial solution vector, epslion precision control function TSDM = The_steepest_descent_method (M, b , X0 , Epsilon) m = size (m); up = 1000; E = Floor (ABS (log (Epsilon))); X-(:,. 1) = X0; R & lt (:,. 1) = bM * X0; for K = . 1: up Alpha = inner_product (R & lt (:, K), R & lt (:, K)) / inner_product (M * R & lt (:, K), R & lt (:, K)); X-(:, K +. 1) = X-(:, K) + Alpha * R & lt (:, K); R & lt (:, K +. 1) = bM * X-(:, K +. 1); of X_DELTA in (:, K) = X-(:, K +. 1 ) the -X-(:, K); IF sqrt (inner_product (of X_DELTA in (:, K), M * of X_DELTA in (:, K))) <Epsilon BREAK; End End DISP ( 'number of iterations:'); K. 1- TSDM = vpa (X (:, k), e); the product %% function IP = inner_product (M1, M2 ) mAX = max (size (Ml)); SUM = 0; for i = 1:MAX sum = sum+M1(i)*M2(i); end IP = sum; end end
2. Examples
clear all clc for i = 1:4 for j = 1:4 if i == j M(i,j) = 2.1; else M(i,j) = 1.5; end end end b = [1 2 3 4]'; X0 = [1 1 1 1]'; epsilon = 1e-4; S = The_steepest_descent_method(M,b,X0,epsilon) M\b
Results
The number of iterations is: ANS = 21 S = -2.12110743 -0.454511872 1.21208369 2.87867925 ANS = -2.1212 -0.4545 1.2121 2.8788 >>