<zz>conjugate gradient共轭梯度方法及matlab推导

Matlab求解线性方程组(一)共轭梯度法 一,算法原理 共轭梯度法可以看作是特殊的迭代法,有迭代法的格式,即首先给出x(0),再由迭代格式 二,程序框图 三,源代码 四,实例分析 阅读详情
此处有两个写的比较好的介绍最速下降,牛顿法,共轭梯度下降法的网页,mark一下。

共轭梯度法(Conjugate gradient method)

(cited from:wiki)

      In mathematics,the conjugate gradient method is an algorithm for the numerical solution of particular systems of linear equations, namely those whose matrix is symmetric and positive-definite. (看到了吧,系数矩阵A必须是对称的和正定的)The conjugate gradient method is an iterative method, so it can be applied to sparse systems that are too large to be handled by direct methods such as the Cholesky decomposition. Such systems often arise when numerically solving partial differential equations.
----------------------

正定矩阵:

在线性代数里,正定矩阵(即“正数-确定-矩阵”)是埃尔米特矩阵的一种,有时会简称为正定阵。

一个n × n的实对称矩阵 M 是正定的当且仅当对于所有的非零实系数向量z,都有 zTMz > 0。其中zT 表示z的转置。

对于复数的情况,定义则为:一个n × n的埃尔米特矩阵 M 是正定的当且仅当对于每个非零的复向量z,都有z*Mz > 0。其中z* 表示z的共轭转置。由于 M 是埃尔米特矩阵,经计算可知,对于任意的复向量z,z*Mz必然是实数,从而可以与0比较大小。因此这个定义是自洽的。

---------------------
The conjugate gradient method can also be used to solve unconstrained optimization problems such as energy minimization.

The biconjugate gradient method provides a generalization to non-symmetric matrices. Various nonlinear conjugate gradient methods seek minima of nonlinear equations.

A comparison of the convergence of gradient descent with optimal step size (in green) and conjugate vector (in red) for minimizing a quadratic function associated with a given linear system. Conjugate gradient, assuming exact arithmetic, converges in at most n steps where n is the size of the matrix of the system (here n=2).

Description of the method
Suppose we want to solve the following system of linear equations
    Ax = b
where the n-by-n matrix A is symmetric (i.e., AT = A), positive definite (i.e., xTAx > 0 for all non-zero vectors x in Rn), and real.
We denote the unique solution of this system by x*.


The conjugate gradient method as a direct method
We say that two non-zero vectors u and v are conjugate (with respect to A) if


Since A is symmetric and positive definite, the left-hand side defines an inner product

<Au,v>=(Au)T*v=(uT)*(AT)*v=<u,ATv>

So, two vectors are conjugate if they are orthogonal with respect to this inner product.Being conjugate is a symmetric relation: if u is conjugate to v, then v is conjugate to u. (Note: This notion of conjugate is not related to the notion of complex conjugate.)
Suppose that {pk} is a sequence of n mutually conjugate directions. Then the pk form a basis of Rn, so we can expand the solution x* of Ax = b in this basis:

The coefficients are given by

 (because   are mutually conjugate)


This gives the following method for solving the equation Ax = b. We first find a sequence of n conjugate directions and then we compute the coefficients αk.

 The conjugate gradient method as an iterative method

If we choose the conjugate vectors pk carefully, then we may not need all of them to obtain a good approximation to the solution x*. So, we want to regard the conjugate gradient method as an iterative method. This also allows us to solve systems where n is so large that the direct method would take too much time.

We denote the initial guess for x* by x0. We can assume without loss of generality that x0 = 0 (otherwise, consider the system Az = b − Ax0 instead). Starting with x0 we search for the solution and in each iteration we need a metric to tell us whether we are closer to the solution x*(that is unknown to us). This metric comes from the fact that the solution x* is also the unique minimizer of the following quadratic function; so if f(x) becomes smaller in an iteration it means that we are closer to x*.

This suggests taking the first basis vector p1 to be the negative of the gradient of f at x = x0. This gradient equals Ax0−b. Since x0 = 0, this means we take p1 = b. The other vectors in the basis will be conjugate to the gradient, hence the name conjugate gradient method.

Let rk be the residual at the kth step:

Note that rk is the negative gradient of f at x = xk, so the gradient descent method would be to move in the direction rk.Here, we insist that the directions pk be conjugate to each other. We also require the next search direction is built out of the current residue and all previous search directions, which is reasonable enough in practice.

The conjugation constraint is an orthonormal-type constraint and hence the algorithm bears resemblance to Gram-Schmidt orthonormalization.
This gives the following expression:


(see the picture at the top of the article for the effect of the conjugacy constraint on convergence). Following this direction, the next optimal location is given by:

with

--------------------

 在线性代数中,如果内积空间上的一组矢量能够张成一个子空间,那么这一组矢量就称为这个子空间的一个基。

Gram-Schmidt正交化提供了一种方法,能够通过这一子空间上的一个基得出子空间的一个正交基,并可进一步求出对应的标准正交基。 

--------------------

 

 

 The resulting algorithm

The above algorithm gives the most straightforward explanation towards the conjugate gradient method. However, it requires storage of all the previous searching directions and residue vectors, and many matrix vector multiplications, thus could be computationally expensive. In practice, one modifies slightly the condition obtaining the last residue vector, not to minimize the metric following the search direction, but instead to make it orthogonal to the previous residue. Minimization of the metric along the search direction will be obtained automatically in this case. One can then result in an algorithm which only requires storage of the last two residue vectors and the last search direction, and only one matrix vector multiplication.Note that the algorithm described below is equivalent to the previously discussed straightforward procedure.

The algorithm is detailed below for solving Ax = b where A is a real, symmetric, positive-definite matrix. The input vector x0 can be an approximate initial solution or 0.

repeat
ifr k+1 is sufficiently small  then exit loop  end if
end repeat
The result is  x k+1

This is the most commonly used algorithm. 


Numerical example
To illustrate the conjugate gradient method, we will complete a simple example.
Considering the linear system Ax = b given by

we will perform two steps of the conjugate gradient method beginning with the initial guess

in order to find an approximate solution to the system.

Solution
Our first step is to calculate the residual vector r0 associated with x0. This residual is computed from the formula r0 = b - Ax0, and in our case is equal to


Since this is the first iteration, we will use the residual vector r0 as our initial search direction p0;the method of selecting pk will change in further iterations.

We now compute the scalar α0 using the relationship


We can now compute x1 using the formula

This result completes the first iteration, the result being an "improved" approximate solution to the system, x1. We may now move on and compute the next residual vector r1 using the formula

Our next step in the process is to compute the scalar β0 that will eventually be used to determine the next search direction p1.


Now, using this scalar β0, we can compute the next search direction p1 using the relationship

We now compute the scalar α1 using our newly-acquired p1 using the same method as that used for α0.

Finally, we find x2 using the same method as that used to find x1.

The result, x2, is a "better" approximation to the system's solution than x1 and x0. If exact arithmetic were to be used in this example instead of limited-precision, then the exact solution would theoretically have been reached after n = 2 iterations (n being the order of the system).


 Convergence properties of the conjugate gradient method

The conjugate gradient method can theoretically be viewed as a direct method, as it produces the exact solution after a finite number of iterations, which is not larger than the size of the matrix, in the absence of round-off error. However, the conjugate gradient method is unstable with respect to even small perturbations, e.g., most directions are not in practice conjugate, and the exact solution is never obtained.Fortunately, the conjugate gradient method can be used as an iterative method as it provides monotonically improving approximations  to the exact solution, which may reach the required tolerance after a relatively small (compared to the problem size) number of iterations. The improvement is typically linear and its speed is determined by the condition number κ(A) of the system matrix A: the larger is κ(A), the slower is the improvement.
If κ(A) is large, preconditioning is used to replace the original system  with  so that  gets smaller than 

 

按照上面的算法,其MATLAB程序,如下:(需要的人可自行拷取,如有错误,敬请告知!)

% % % %-------------------------------------------------------------------------------------

function x = myconjgrad(A,b,tol) 
% MYCONJGRAD     my Conjugate Gradient Method. 
%   x = MYCONJGRAD(A,b,tol) attemps to solve the system of linear equations A*x=b 
%   for x. The N-by-N coefficient matrix A must be symmetric,positive definite and the right 
%   hand side column vector B must have the same length as A. 
%   input parameter tol specifies the tolerance of the method.  
% 
% Example: 
%{ 
n = 600; 
m = 800; 
A = randn(n,m); 
A = A * A'; 
b = randn(n,1); 
tic, x = myconjgrad(A,b,1e-4); toc 
norm(A*x-b) 
%}
% 
    x = b;        % assuming x0 is b
    r = b - A*x;    % the residual vector r0  
    p = r;        % the first search direction  

    for k = 1:numel(b); 
    z = A*p;        
           alpha = (r'*r)/(p'*z);    % the scalar 
        x = x + alpha*p;    % x1 = x0 + alpha0*p0  
    s = r'*r;           
       r = r - alpha*z;    % the residual vector r 
       if( norm(r) < tol ) 
            return; 
       end
    B = (r'*r)/s; 
       p = r + B*p;    % y is the search direction p 
    end 
% % % %-------------------------------------------------------------------------------------

从程序中可看到,关键的变量有系数αk,由之可得新的计算点x位置,新的残差向量rk,以及β,由它可以求得新的搜索方向pk。

程序编写时,参考了该地址的程序。两者结果不一样,在所实验的条件下,本文结果较优。但是不明白参考版本的计算思路。或许有其他优点?有知道的请告知。


MATLAB 共轭梯度法求解线性方程组(附代码) 共轭梯度法(Conjugate Gradient Method)是一种用于求解大型稀疏对称正定线性方程组的迭代算法。该方法结合了梯度下降法和共轭方向的概念,以达到更快速的收敛。共轭梯度法 是介于最速下降法与牛顿法之间的一个方法,它仅需利用一阶导数信息,但克服了最速下降法收敛慢的缺点,又避免了牛顿法需要存储和计算Hesse矩阵并求逆的缺点,在有限元求解中经常用到此方法求解椭圆问题。 阅读详情

相关推荐

MATLAB - 共轭梯度法

一、问题描述 求解系数矩阵A是对称正定矩阵的线性方程组或求解二次函数的极小值点。 二、算法 共轭梯度法,步骤如下: 1 任意给定初始点及精度 2 3 对于,作 1) 2) 3),或 4) 若或,则输出,,取作为的解,否则 ...

qq_41628935的博客 2万+

Numerical Optimization---Nonlinear conjugate Gradient Methods

在线性共轭梯度法中αk\alpha_kαk​ 精确线搜索可以精确得到,当在非线性共轭梯度法中的αk\alpha_kαk​ 就不能采用精确线搜索,因为对于非线性复杂函数采用的精确线搜索关于αk\alpha_kαk​ 的函数和原函数一样的复杂。 因此 可以使用非精确线搜索来得到对应的αk\alpha_kαk​ 定理7 保证了PkP_kPk​是下降方向的,if 参生一个坏的下下降方向(与负梯度方...

王尚权 qq:2515162716 463

预处理_预处理(Preconditioning)

预处理的基本思想是通过修改病态(ill-conditioned)系统 得到一个等价系统 使得迭代算法的收敛速度更快。常见的方法是使用一个非奇异矩阵 使得 其中预处理矩阵 需要使得 的条件数变小,或者说避免病态问题。预处理共轭梯度共轭梯度法的收敛速度正比于 ,因此对于条件数比较差的矩阵收敛速度会很慢,我们需要选择矩阵 使得 ,这样就具有更快的收敛速度了。为了保证预处理后的矩阵 是对...

weixin_34871733的博客 1237

matlab共轭梯度法_优化算法之共轭梯度法

共轭梯度法是Hestenes和Stiefel(1952年)在求解大规模线性方程组时创立的一种迭代算法。1964年,Fletcher和Reeves将其引入非线性最优化问题,创立了非线性共轭梯度法。该方法基于前一迭代点的搜索方向对当前迭代点的负梯度方向进行修正来产生新的搜索方向,即搜索方向为当前迭代点的负梯度方向与前一搜索方向的线性组合。本期大纲1.共轭方向2.共轭方向法3. 正定二次函...

weixin_40009099的博客 2422

共轭梯度法详细推导分析

共轭梯度法是一种经典的优化算法。算法求解速度较快,虽然比梯度下降法复杂,但是比二阶方法简单。 一、引入 1. 优化模型建立 假定待优化的问题如下所示: min⁡xf(x)=12xTAx−bTx \min_{x} f(x)=\frac{1}{2}x^TAx - b^Tx xmin​f(x)=21​xTAx−bTx 其中xxx为待优化变量,AAA为半正定矩阵(在线性代数中,正定矩阵为对称矩阵),bbb...

开贰锤 4万+

【最优化算法】基于【MATLAB】的共轭梯度法【Conjugate Gradient】分析与推导

引入阻尼牛顿法进行修正,但是在牛顿法和阻尼牛顿法中都存在计算Hesse矩阵的问题,使得在多次迭代时可能会出现计算量过大的问题。

ALi.S的博客 4189

共轭梯度法Matlab的实现

文章目录写在前面共轭梯度法代码实现标题函数定义部分函数测试画图显示效果显示方法运行方法 写在前面 写这篇博客是为了增加对共轭梯度的理解。最近最优化课一直在讲共轭梯度,雅克比,梯度下降啊,课上没听太懂,就课下花了点时间好好学一下,在此记录一下。 这篇博客主要介绍了共轭梯度的matlab实现,代码可以运行看到效果。代码分为2个部分,第一部分代码是函数定义,第二部分代码是测试代码。 共轭梯度法 我是参考了下面这篇博客,以及B站的一个视频 共轭梯度法的简单直观理解 https://www.bilibili.com/

weixin_43755104的博客 1万+

使用MATLAB实现共轭梯度(CG)算法——共轭梯度算法

注意,在实际应用中,矩阵 A 通常是大型的且稀疏的,因此使用MATLAB的稀疏矩阵格式(通过 spdiags、speye 等函数创建)可以显著提高计算效率。此外,CG算法的收敛速度受到系数矩阵谱分布的影响,当矩阵条件数较大时,收敛速度可能会变慢。共轭梯度算法基于一个关键概念:共轭方向。, p_{n-1}},使得对于所有i ≠ j,都有p_i^T A p_j = 0,则称这组向量为A共轭的。5.否则,计算下一个搜索方向的系数β_k = (r_{k+1}^T r_{k+1}) / (r_k^T r_k)。

m0_53407570的博客 852

CG(conjugate gradient method)+Python+Matlab

% ** 文件名:Conjugate Gradient.m % % ** 日 期:2016.12.14 % % ** 描 述:共轭梯度法解线性方程组(Conjugate Gradient method) % % ** 函数:113页计算实习3.2 % % ** 参考教材:《数值分析》李乃成,梅立泉,科学出版社 % clear; clc; %% %获得需要的N阶矩阵A N=100; %解向量的维数 %N=200; %N=400; a0=eye(N);%a0为n阶的单位阵 a1=eye(N-1)..

Angelaboy的博客 1013

共轭梯度法在matlab中的实现

最优化共轭梯度法的matlab代码实现,可以用于求解非线性方程的解非约束性最优化问题

MATLAB实现共轭梯度解决最优化问题

MATLAB结合实例实现共轭梯度,使用了经典共轭下降公式,DY公式,FR公式,PRP+公式和PRP公式,解决最优化问题

matlab共轭梯度法_Ax = b 的迭代解法-从梯度下降到共轭梯度

线性方程组 Ax =b 除了高斯消元法以外,还有一些很有趣的迭代解法, 比如雅可比法(Jacobi Method),高斯-赛德尔迭代(Gauss–Seidel method)。这里只针对 A 满足 对称 ( ), 正定(即 ),并且是实系数的,那么我们可以用 梯度下降 和 共轭梯度 来解线性方程组 : 梯度下降 Gradient descent梯度下降(Gradient descent)完全...

weixin_40008884的博客 1307

MATLAB实现共轭梯度法

MATLAB实现共轭梯度法MATLAB实现共轭梯度法MATLAB实现共轭梯度法

数值优化算子

优化问题非常常见。在Python里,有Scipy这个package里的optimizer, minimizer等module,提供了不少优化算子。常见的Conjugate gradient, Simplex methods, L-BFGS都有实现。可是,在实际中,经常不能显示的提供jacobian或者hessian矩阵,因而用conjudatae gradient一类的现成算子不可行。但如果不想依...

eglxiang的博客 378

共轭梯度法的MATLAB实现程序

使用MATLAB编程实现共轭梯度法

Qt 5.9.1 32位下,手把手搞定周立功CAN二次开发库的加载与配置(含常见错误排查)

本文详细介绍了在Qt 5.9.1 32位环境下配置周立功CAN二次开发库的完整流程,包括环境准备、项目配置、常见错误排查及性能优化建议。特别针对MinGW 32bit环境,提供了.pro文件的精准配置和DLL部署策略,帮助开发者快速解决库文件匹配、编译报错和运行时问题,实现高效的CAN总线通信开发。

weixin_28717431的博客 356

共轭梯度法推导

1.共轭向量  在介绍共轭梯度法前,首先介绍一下共轭向量的概念。对于向量e1\boldsymbol{e_1}e1​,e2\boldsymbol{e_2}e2​,如果两个向量正交,则有e1Te2=0\boldsymbol{e_1}^\mathrm{T}\boldsymbol{e_2}=0e1​Te2​=0,那么对于共轭向量来说,假设有一正定对称矩阵A\boldsymbol{A}A,如果e1TAe2=...

youtuodai1436的博客 3057

永磁同步电机控制笔记:foc控制原理通俗讲义

引言 到此为止,永磁同步电机控制相关文章已经写了7篇,其中simulink仿真系列文章5篇,控制笔记系列文章2篇,恍惚间感觉少了一篇概论,在此插入一篇,也算是对入坑电机控制一年半来的知识梳理。 文章目录引言一、同步电机是怎么转起来的二、什么样的旋转磁场效率最高三、怎么精准控制旋转磁场的方向四、怎样调节dq轴电流 一、同步电机是怎么转起来的 永磁同步电机结构如下: 外围是定子绕组,中间为永磁体,设...

搞电机的博客 2万+

matlab的共轭梯度法

通过matlab编程,实现共轭梯度法算法

weixin_42469573的博客 5437

MATLAB AppDesigner 中TextArea保留原有信息并换行显示提示信息

App Designer 中的文本区域TextArea组件,一般可以用来作为matlab app设计中的信息显示。 app.TextArea.Value = 'Hello Matlab'; Value可以是字符向量、字符向量元胞数组、字符串数组或一维分类数组。这样直接给TextArea赋值,每次赋值都会消除先前的信息,然后显示新的信息。如果想要在app实现保留原有信息、多行信息的输出,在MathWorks官方帮助中心给出了一种方法: cellArrayText{1} = sprintf('%s\n

我是麻瓜喔的博客 1万+

为什么你的XGBoost多分类效果不如预期?可能是这5个参数没调对

本文深度剖析了XGBoost模型在处理多分类问题时效果不佳的五大关键参数陷阱。针对多分类问题的独特性,文章提供了max_depth、learning_rate、采样参数及正则化参数等核心参数的量化调整策略与诊断方法,帮助开发者精准调优,有效提升模型在各类别上的均衡性能与泛化能力。

weixin_29182481的博客 300

matlab的转置和共轭,对Matlab中共轭、转置和共轭装置的区别说明

共轭转置共轭转置The symbols (·)T , (·)∗, and (·)H are,respectively,the transpose, complex conjugateand Hermitian transpose operators.这里使用随机产生的矩阵来说明问题,例如:A=randn(2,2)+randn(2,2)*iA=randn(2,2)+randn(2,2)*jA=ran...

weixin_34043312的博客 3832

C# 上位机之海康工业相机开发(SDK)实战教程

海康工业相机的C#开发核心是基于MVS SDK的“初始化→枚举→连接→采集→释放”流程,其中图像回调处理和格式转换是关键。严格按照SDK文档处理设备类型(网口/USB)和像素格式;及时释放图像资源,避免内存泄漏;错误码是排查问题的重要依据(可参考SDK中的“错误码说明”文档)。掌握这些基础后,可结合机器视觉算法(如OpenCV)实现缺陷检测、尺寸测量等高级功能,构建完整的工业视觉系统。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 3860
上一篇: 多分类问题中的unbalance数据集的分类
下一篇: <zz>matlab向量化技巧经验总结
且歌且行
博客等级 码龄19年 58粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值