Gram Schmidt实现部分工作,输出一个坏数字(Gram Schmidt implementation works partially, outputs a bad number)

编程入门 行业动态 更新时间:2024-10-26 07:37:25
Gram Schmidt实现部分工作,输出一个坏数字(Gram Schmidt implementation works partially, outputs a bad number)

我正在尝试在学校项目的代码中实施Gram Schmidt,我有一个问题。它输出一个奇怪的数字,我不知道为什么。其余的都很好,但这个很糟糕。如果帖子是不好,但这是我在这里的第一篇文章,我真的需要帮助。谢谢

#include <iostream> #define dim 100 using namespace std; void inputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i = 0; i < *n; i++){ cout << "V" << i << ":"; for(j = 0; j < *m; j++) cin >> x[i][j]; } } void outputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i=0;i<*n;i++){ for(j=0;j<*m;j++) cout<<x[i][j]<<" "; cout<<endl; } } void initialize(int *m,double v[dim]){ int i; for(i=0;i<*m;i++){ v[i]=0; } } int main(){ double v[dim][dim], f[dim][dim], e[dim][dim],p1,p2,a[dim]; int n,m,i,j,z; cout << "Introduceti numarul de vectori: ";cin >> n; cout << "Introduceti numarul de elemente: ";cin >> m; inputMatrix(&n,&m,v); double div = 0; for(i = 0; i < m; i++){ f[0][i] = v[0][i]; } outputMatrix(&n,&m,v); cout << endl; outputMatrix(&n,&m,f); for(i = 1;i < n; i++){ z = 0; initialize(&m,a); mk1: p1 = 0; p2 = 0; for(j = 0; j < m; j++){ p1 = f[z][j] * v[i][j] + p1; p2 = f[z][j] * f[z][j] + p2; } div = p1 / p2; for(j = 0; j < m; j++){ a[j] = f[z][j] * div + a[j]; } z++; if( z < i){ goto mk1; } else{ for(j = 0; j < m;j++){ f[i][j] = v[i][j] - a[j]; } } cout << endl; outputMatrix(&n,&m,f); return 0; }

输出:

Introduceti numarul de vectori: 3 Introduceti numarul de elemente: 4 V0:1 2 3 0 V1:1 2 0 0 V2:1 0 0 1 1 2 3 0 1 2 0 0 1 0 0 1 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0.642857 1.28571 -1.07143 0 0.8 -0.4 5.55112e-17 1

我不明白为什么输出这个“5.55112e-17”感谢您的帮助!

I'm trying to implement Gram Schmidt in code for a school project and I have a problem.It outputs a weird number and I don't know why.The rest of them are good but this one is bad.Sorry if the post is bad but it's my first post here and I really need help.Thanks

#include <iostream> #define dim 100 using namespace std; void inputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i = 0; i < *n; i++){ cout << "V" << i << ":"; for(j = 0; j < *m; j++) cin >> x[i][j]; } } void outputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i=0;i<*n;i++){ for(j=0;j<*m;j++) cout<<x[i][j]<<" "; cout<<endl; } } void initialize(int *m,double v[dim]){ int i; for(i=0;i<*m;i++){ v[i]=0; } } int main(){ double v[dim][dim], f[dim][dim], e[dim][dim],p1,p2,a[dim]; int n,m,i,j,z; cout << "Introduceti numarul de vectori: ";cin >> n; cout << "Introduceti numarul de elemente: ";cin >> m; inputMatrix(&n,&m,v); double div = 0; for(i = 0; i < m; i++){ f[0][i] = v[0][i]; } outputMatrix(&n,&m,v); cout << endl; outputMatrix(&n,&m,f); for(i = 1;i < n; i++){ z = 0; initialize(&m,a); mk1: p1 = 0; p2 = 0; for(j = 0; j < m; j++){ p1 = f[z][j] * v[i][j] + p1; p2 = f[z][j] * f[z][j] + p2; } div = p1 / p2; for(j = 0; j < m; j++){ a[j] = f[z][j] * div + a[j]; } z++; if( z < i){ goto mk1; } else{ for(j = 0; j < m;j++){ f[i][j] = v[i][j] - a[j]; } } cout << endl; outputMatrix(&n,&m,f); return 0; }

Output:

Introduceti numarul de vectori: 3 Introduceti numarul de elemente: 4 V0:1 2 3 0 V1:1 2 0 0 V2:1 0 0 1 1 2 3 0 1 2 0 0 1 0 0 1 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0.642857 1.28571 -1.07143 0 0.8 -0.4 5.55112e-17 1

I don't understand why it outputs this "5.55112e-17" Thanks for help!

最满意答案

我不明白为什么输出这个“5.55112e-17”感谢您的帮助!

这是舍入误差的结果,这种表示法意味着5.55112乘以10的幂,并且可以被认为是用(实数)数字的计算机表示计算的伪像。 有关详细说明,请考虑这个答案。

I don't understand why it outputs this "5.55112e-17" Thanks for help!

This is the result of rounding errors, this notation means 5.55112 times 10 to the power of -17 and can be considered an artifact of calculating with computer representations of (real) numbers. For some more elaboration, consider this answer.

更多推荐

本文发布于:2023-08-06 23:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   工作   implementation   Schmidt   Gram

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!