Matlab code for Gauss-Seidel and Successive over relaxation iterative methods
来源:互联网 发布:六轴机械手臂编程 编辑:程序博客网 时间:2024/06/06 02:33
代码matlab 维基原理
原始代码里面每迭代一次都求解线性方程组,不太合理。可以通过改写,只须求一次即可。
不过考虑到矩阵是上三角的,主要只涉及回代,也就未必不行。
function [x, error, iter, flag] = sor(A, x, b, w, max_it, tol)% -- Iterative template routine --% Univ. of Tennessee and Oak Ridge National Laboratory% October 1, 1993% Details of this algorithm are described in "Templates for the% Solution of Linear Systems: Building Blocks for Iterative% Methods", Barrett, Berry, Chan, Demmel, Donato, Dongarra,% Eijkhout, Pozo, Romine, and van der Vorst, SIAM Publications,% 1993. (ftp netlib2.cs.utk.edu; cd linalg; get templates.ps).%% [x, error, iter, flag] = sor(A, x, b, w, max_it, tol)%% sor.m solves the linear system Ax=b using the % Successive Over-Relaxation Method (Gauss-Seidel method when omega = 1 ).%% input A REAL matrix% x REAL initial guess vector% b REAL right hand side vector% w REAL relaxation scalar% max_it INTEGER maximum number of iterations% tol REAL error tolerance%% output x REAL solution vector% error REAL error norm% iter INTEGER number of iterations performed% flag INTEGER: 0 = solution found to tolerance% 1 = no convergence given max_it flag = 0; % initialization iter = 0; bnrm2 = norm( b ); if ( bnrm2 == 0.0 ), bnrm2 = 1.0; end r = b - A*x; error = norm( r ) / bnrm2; if ( error < tol ) return, end [ M, N, b ] = MatriceSplit( A, b, w, 2 ); % matrix splittingM01=M\N;%改写M02=M\b; for iter = 1:max_it % begin iteration x_1 = x; x = M01*x+M02; %M \ ( N*x + b ); % update approximation error = norm( x - x_1 ) / norm( x ); % compute error if ( error <= tol ), break, end % check convergence end b = b / w; % restore rhs if ( error > tol ) flag = 1; end; % no convergencefunction [ M, N, b ] = MatriceSplit( A, b, w, flag )%% function [ M, N, b ] = MatriceSplit( A, b, w, flag )%% MatriceSplit.m sets up the matrix splitting for the stationary% iterative methods: jacobi and sor (gauss-seidel when w = 1.0 )%% input A DOUBLE PRECISION matrix% b DOUBLE PRECISION right hand side vector (for SOR)% w DOUBLE PRECISION relaxation scalar% flag INTEGER flag for method: 1 = jacobi% 2 = sor%% output M DOUBLE PRECISION matrix% N DOUBLE PRECISION matrix such that A = M - N% b DOUBLE PRECISION rhs vector ( altered for SOR ) [m,n] = size( A ); if ( flag == 1 ), % jacobi splitting M = diag(diag(A)); N = diag(diag(A)) - A; elseif ( flag == 2 ), % sor/gauss-seidel splitting b = w * b; M = w * tril( A, -1 ) + diag(diag( A )); N = -w * triu( A, 1 ) + ( 1.0 - w ) * diag(diag( A )); end;% END MatriceSplit.m% END sor.m
matrixSplit.m
sor.m
阅读全文
0 0
- Matlab code for Gauss-Seidel and Successive over relaxation iterative methods
- Matlab实现——Jacobi Iteration and Gauss-Seidel Iteration
- jacobi, gauss-seidel, and SOR
- 数值分析 Gauss-Seidel迭代法求解线性方程组 MATLAB程序实现
- Gauss-Seidel解线性方程组
- Jacobi Gauss-Seidel迭代法
- 求解线性方程组--Gauss-Seidel迭代法
- Jacobi迭代法 Gauss-Seidel迭代法
- Jacobi迭代法 Gauss-Seidel迭代法
- 高斯—赛德尔(Gauss - Seidel)迭代法解线性方程组(Matlab程序)
- 4.4.7 Iterative Methods
- Matlab实现——严格对角占优三对角方程组求解(高斯赛尔德Gauss-Seidel迭代、超松弛)
- Gauss–Seidel方法和Jacobi 方法
- Jacobi迭代法与Gauss-Seidel迭代法
- Gauss-Seidel迭代法求方程组的解
- Jacobi迭代法与Gauss-Seidel迭代法
- MATLAB Code For Inverse and Forward Kinematics (Newton-Raphson Method)
- 数值计算 && 实验8 && Gauss-Seidel迭代法求解线性方程组
- 输入输出 端口类型
- C++最全string与char *的转换
- Android中View绘制过程(一) decorView绘制
- 方法和数组
- Java的代码块
- Matlab code for Gauss-Seidel and Successive over relaxation iterative methods
- OpenGL蓝宝书源码学习(二十)第六章——Dissolve
- unlink之64位下有保护措施的利用
- csdn如何转载别人的博客
- 模仿百度API接口搜索框
- C++ Primer 练习 12.19
- Android Loader的机制以及源码分析
- ubuntu 16.04 安装ROS时的依赖问题的解决
- 题解——Leetcode 16. 3Sum Closest 难度:Medium