数值实验共轭梯度法matlab代码,数值线性代数实验-共轭梯度法
一开始用c++的运算符重载程序总是莫名其妙的崩掉,然后以为是运算符重载的问题就写了个class对矩阵重新封装,结果还是崩,然后好久才发现是我把空间开的太大导致程序崩掉,无语,这样就浪费了我一个上午。。。。
课本上的例题:
$$
\begin{equation}
{\left[ \begin{array}{ccc}
10&1&2&3&4\\
1& 9& -1& 2& -3\\
2& -1& 7& 3& -5\\
3& 2& 3& 12& -1\\
4& -3& -5 &-1 &15
\end{array}
\right ]}\times {
\left[ \begin{array}{ccc}
x_1\\
x_2\\
x_3\\
x_4\\
x_5
\end{array}
\right ]}
={
\left[ \begin{array}{ccc}
12\\ -27\\ 14\\ -17\\ 12
\end{array}
\right ]}
\end{equation}
$$
输入数据
5 5
10 1 2 3 4
1 9 -1 2 -3
2 -1 7 3 -5
3 2 3 12 -1
4 -3 -5 -1 15
12 -27 14 -17 12
#include "cstdio"#include"cstring"#include"cstdlib"#include"cmath"#include"iostream"
using namespacestd;const double eps = 1e-6;const int maxr = 10;const int maxc = 10;classMatrix {private:doublem[maxr][maxc];introw, col;public:
Matrixoperator -(Matrix b) {
Matrix c;
c.col= this->col; c.row = this->row;for (int i = 0; i < this->row; i++) {for (int j = 0; j < this->col; j++) {
c.m[i][j]= this->m[i][j]-b.m[i][j];
}
}returnc;
}
Matrixoperator +(Matrix b) {
Matrix c;
c.col= this->col; c.row = this->row;for (int i = 0; i < this->row; i++) {for (int j = 0; j < this->col; j++) {
c.m[i][j]= this->m[i][j] +b.m[i][j];
}
}returnc;
}
Matrixoperator *(Matrix b) {
Matrix c;
memset(c.m,0, sizeof(c.m));
c.row= this->row; c.col =b.col;for (int i = 0; i < this->row; i++) {for (int k = 0; k < this->col; k++) {for (int j = 0; j < b.col; j++) {
c.m[i][j]+= this->m[i][k]*b.m[k][j];
}
}
}returnc;
}
Matrixoperator & (doublea) {
Matrix c= *this;for (int i = 0; i < this->row; i++) {for (int j = 0; j < this->col; j++) {
c.m[i][j]= a*this->m[i][j];
}
}returnc;
}
Matrixoperator !() {
Matrix c= *this;
c.row= this->col, c.col = this->row;for (int i = 0; i < this->row; i++) {for (int j = 0; j < this->col; j++) {
c.m[j][i]= this->m[i][j];
}
}returnc;
}intdcmp() {for (int i = 0; i < this->row; i++) {for (int j = 0; j < this->col; j++) {if (fabs(this->m[i][j]) > eps) return 1;
}
}return 0;
}voidshow_Matrix() {for (int i = 0; i < this->row; i++) {
printf("%.6f", this->m[i][0]);for (int j = 1; j < this->col; j++) {
printf("%.8f", this->m[i][j]);
}
printf("\n");
}
}void set_matrix(int n, intm) {this->row = n, this->col =m;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {
scanf("%lf", &this->m[i][j]);
}
}
}void init(int n, intm) {this->row = n, this->col =m;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {this->m[i][j] = 1.0;
}
}
}int Col(){return this->col;}int Row(){return this->row;}double get(int i, int j) {return this->m[i-1][j-1];}
};
Matrix Conjugate_gradient(Matrix A, Matrix B) {
Matrix x0=B, p0, r1, p1;doublealf, bet;
x0.init(A.Col(),1);
Matrix r0= B-(A*x0);int k = 0;
r1=r0;while(r1.dcmp()) {
k++;if (k == 1) p1 =r0;else{
bet= (!r1*r1).get(1, 1)/(!r0*r0).get(1, 1);
p1= r1 + (p0&bet);
}
alf= (!r1*r1).get(1, 1)/(!p1*A*p1).get(1, 1);
x0= x0 + (p1&alf);
r0=r1;
r1= r1-((A&alf)*p1);
}returnx0;
}int main(int argc, char const *argv[])
{//freopen("in.txt", "r", stdin);
Matrix A, B, C;intn, m;
printf("输入系数矩阵的维数和矩阵A\n");
scanf("%d %d", &n, &m);
A.set_matrix(n, m);
printf("输入矩阵B\n");
B.set_matrix(n,1);
C=Conjugate_gradient(A, B);
printf("利用共轭梯度法得到的解为\n");
C.show_Matrix();return 0;
}
数值实验共轭梯度法matlab代码,数值线性代数实验-共轭梯度法相关推荐
- 共轭梯度法matlab代码博客园,共轭梯度法源代码.doc
共轭梯度法源代码 共轭梯度法程序源代码 #include #include #define N 10 #define eps pow(10,-6) double f(double x[],double ...
- matlab数值分析拟合实例,数值分析函数拟合matlab代码.doc
数值分析函数拟合matlab代码.doc 第一题MATLAB代码用SPLINE作图XI0204060810YI098092081064038X10012Y1NEWTON3XI,YI,X源代码见M文件Y ...
- MATLAB图像基本变换实验报告,MATLAB图像增强与变换处理实验报告
实验一 MATLAB图像增强与变换处理实验 一.实验目的 1.熟悉掌握数字图像处理的基本概念. 2.了解MATLAB的的编程环境,图像处理工具箱的使用方法. 3.掌握数字图像处理图像增强的基本方法. ...
- 【雷达】线性调频脉冲雷达仿真实验附matlab代码
1 内容介绍 在MATLAB上实现线性调频信号(LMCW)的脉冲压缩(PC).动目标显示 (MTI)和动目标检测(MTD).解速度度模糊以及恒虚警率检测(CFAR)等信号 处理算法.(本实验原内容为D ...
- matlab创新开放性实验,基于MATLAB 的自动控制原理实验仿真系统的设计
基于MATLAB 的自动控制原理实验仿真系统的设计 基于MATLAB 的自动控制原理实验仿真系统的设计 田晴,张茁 (河北联合大学电气工程学院,河北唐山063000) 摘要:MATLAB的图形用户界面 ...
- 如何使用matlab仿真物理实验,基于Matlab的大学物理实验模拟.pdf
维普资讯 浙江科技学院学报,第 2O卷第 2期,2008年 6月 JournalofZhejiangUniversityofScienceandTechnology Vo1.20No.2,June20 ...
- MATLAB设计控制系统仿真实验,基于MATLAB的自动控制原理实验仿真系统的设计
一.引言<自动控制原理>是自动化专业的基础课程,是控制科学与工程学科的一门方法论课程,主要培养学生掌握控制系统的分析和设计方法,其内容之多,理论性之强,决定了课程学习的难度.而实验课作为课 ...
- matlab电位图仿真实验,基于MATLAB的静电场描绘实验仿真
百家论点 基于MATLAB的静电场描绘实验仿真* 陈晓旭 唐翠明 (四川理工学院物电学院,四川 自贡 643000) 摘 要:静电场强度和电势空间分布检测较困难,常采用稳恒电流产生的稳恒电场来模拟静电 ...
- 用MATLAB拟合实验报告,MATLAB插值与拟合实验报告材料
<MATLAB插值与拟合实验报告材料>由会员分享,可在线阅读,更多相关<MATLAB插值与拟合实验报告材料(9页珍藏版)>请在人人文库网上搜索. 1.实用标准文档文案大全CEN ...
最新文章
- android点滴 之 进度条
- pythonfor循环100次_在for循环中只打印一次
- spingboot实现redis的发布订阅
- python简单体育竞技模拟_python初体验 —— 模拟体育竞技
- 数据库MySQL安装
- in作为介词的用法_(完整版)介词in重要用法归纳
- php如何查看openssl扩展安装成功,php如何安装openssl扩展
- [日期]字符串转Data对象
- 就我不坑2 nyoj(简单模拟)
- 在网页输出10的阶乘.php,VB 用for循环语句编写程序,计算和输出10的阶乘
- rtlab matlab版本,电力电子技术教学中电力仿真软件选择与应用.doc
- 高等数学复习之空间解析几何
- 耿建超英语语法---陈述句(2)
- 2020年开发人员的机器学习基础
- python统计英语单词出现次数
- 目 录 -- 项目管理知识体系指南(PMBOK指南)(第五版)
- 微信小程序:多功能图片处理器
- 齐次弦振动方程的matlab解法,ode45求解振动微分方程
- Sculpin Extended:自定义静态站点博客
- 对比学习中的4种经典训练模式