求四元一次方程的算法

aokesha 2008-09-10 10:38:18
本人要通过Excel分析数据,通过4组4个已知数据求4个未知数 ,可是如果出现同列数据相等的情况 就解不出来了 所以求教各位大虾帮忙给算法 要不然这个月工资就没有了 就要喝粥了:(
公式是这样 :
未知数1 = 常数1 + 未知数2 * 常数2的开方 + 未知数3 * 常数3 + 未知数4 * 常数4;
...全文
613 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
phz1985 2008-09-12
  • 打赏
  • 举报
回复
http://www.tyut.edu.cn/kecheng/xianxds/TeachMat/1-5.htm
用克莱姆法则计算.
bayuan0414 2008-09-12
  • 打赏
  • 举报
回复
建议楼主花点时间看看 线性代数
fallening 2008-09-12
  • 打赏
  • 举报
回复
楼上的,一个matlab多少钱能搞定?
这个价格打5折,把钱给我,我帮你搞定这个程序。
train0714 2008-09-12
  • 打赏
  • 举报
回复
matlab真的很简单!
aokesha 2008-09-12
  • 打赏
  • 举报
回复
谢谢 我研究下
aozhi 2008-09-12
  • 打赏
  • 举报
回复
高斯消元?
fallening 2008-09-12
  • 打赏
  • 举报
回复
//FileName:         lq.h
//Author: Feng Wang
//Date: Tue Mar 25 13:51:02 CST 2008
//Comment: Resolve the linear equation
//Last Modify:
//

#ifndef _LQ_H
# define _LQ_H

#include "det.h"

#include <vector>
#include <cassert>
#include <cstdlib>

#ifdef DEBUG
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
#endif

//
//for solution of the linear equation
// A X = B
//
template<class T>
void lq ( const std :: vector<T>& a, //store A[size][size]
const std :: vector<T>& b, //store B[size]
const unsigned int& order, //store size
std :: vector<T>& result //store X[size]
)
{
#ifdef DEBUG
cout << "\n>>Currently processing function lq()."
<< "\nThe order is " << order;

cout << "\nThe incoming vector a is \n";
copy( a.begin(), a.end(), ostream_iterator<T>( cout, " ") );

cout << "\nThe incoming vector b is \n";
copy( b.begin(), b.end(), ostream_iterator<T>( cout, " ") );
#endif

unsigned int Order = order;
//if order is set to default, calculate order
if ( 0 == Order )
{
Order = b.size();
}

assert ( Order * Order == a.size() );
assert ( Order == b.size() );

result.clear();

const T _D = det ( a, Order ); //store |D|

#ifdef DEBUG
cout << "\nThe base vector det is " << _D;
#endif

if ( 0 == _D )
{
if ( "Failed to solve the linear equation" )
{
#ifdef DEBUG
cout << "\n the det of vector is 0, failed to solve the equation.";
#endif

exit ( EXIT_FAILURE );
}
}

//change colum to get D[i]
//then solve it
for ( unsigned int i = 0; i < Order; ++i )
{
std :: vector<T> A = a;
for ( unsigned int j = 0; j < Order; ++j )
{
A[i+j*Order] = b[j];
}
const T D = det ( A, Order ); //store D[i]

result.push_back ( D / _D ); //get X[i]

#ifdef DEBUG
cout << "\nThe base vector det is " << _D;
cout << "\nThe current vector det is " << D;
cout << "\nresult[" << i << "] calculated, whose value is " << D / _D;
#endif

}

#ifdef DEBUG
cout << "\n<<Stride out of fucntion lq()";
#endif
}



#endif //_LQ_H

这个是用克莱姆法则求解的函数,同样,超过6阶后果自负
fallening 2008-09-12
  • 打赏
  • 举报
回复
给你以前我写的一段代码看看
//FileName:     det.h
//Author: Feng Wang
//Date: Mon Mar 24 21:25:15 CST 2008
//Comment: return the determinant of a square matrix
//Last Modify: Tue Mar 25 13:55:44 CST 2008

#ifndef _DET_H
# define _DET_H

#include <cassert>
#include <vector>
#include <cmath>
//
//USAGE:
// #include "../algorithm/ran.h"
// ......
// vector<long double> s;
// const unsigned int N = 3;
//
// for ( unsigned int i = 0; i < N; ++i )
// for ( unsigned int k = 0; k < N ;++k )
// s.push_back( _ran() );
// long double determinant = det( s, N );
// ......
//

//return the determinant of a square matrix arr whose size is order by order
template< class T >
T det ( const std :: vector<T>& arr, const unsigned int& order = 0 )
{
unsigned int Order = order;

//if order is set to default, calculate it
if ( 0 == Order )
{
Order = sqrt ( arr.size() );
}

assert ( Order * Order == arr.size() );


if ( 1 == Order )
return ( arr[0] ) ;

T sum = 0;
std ::vector<T> arr2 ;
int sign = 1;

for ( unsigned int i = 0 ; i < Order ; ++i, sign *= -1 )
{
/* copy n-1 by n-1 array into another array */
const unsigned int newsize = ( Order - 1 ) * ( Order - 1 ) ;

arr2.resize ( newsize );
for ( unsigned int j = 1 ; j < Order ; ++j )
{
for ( unsigned int k = 0, count = 0 ; k < Order ; ++k )
{
if ( k == i )
continue ;//jump out of k loop

const unsigned int pos = j * Order + k ;
const unsigned int newpos = ( j - 1 ) *
( Order - 1 ) + count ;
arr2[newpos] = arr[pos] ;
count++ ;
}//end of k loop
}//end of j loop

/* find determinant value of n-1 by n-1 array and add it to sum */
sum += arr[i] * sign * det ( arr2, Order - 1 ) ;
}
return sum;
}
#endif //_DET_H


这个是计算矩阵的行列式值的,当然,如果超过6阶,后果自负
aokesha 2008-09-12
  • 打赏
  • 举报
回复
谢谢楼上的 不过看完了 我头也大了 呵呵
fallening 2008-09-11
  • 打赏
  • 举报
回复
不一定非叠代不可的

可以用克来姆法则直接给出
看这里http://hi.baidu.com/limingdai/blog/item/0d7ae0098cfefa206a60fb6c.html
aokesha 2008-09-11
  • 打赏
  • 举报
回复
我再看看 找找
XiaoG602 2008-09-10
  • 打赏
  • 举报
回复
用matlab或者mathmatica吧,很快
用户 昵称 2008-09-10
  • 打赏
  • 举报
回复
富农不许喝粥
aokesha 2008-09-10
  • 打赏
  • 举报
回复
常熟1不一定相等的
aokesha 2008-09-10
  • 打赏
  • 举报
回复
第一式-第二式 不应该是 0=a-e +(b-f)*y + (c-G)*z + (d-h)*w么
0=a+b*y+c*z+d*w;
同除b:
0=a+y+c*z+d*w;
第三式-第四式
0=e+f*y+g*z+h*w;
同除b:
0=e+y+g*z+h*w;

太乙 2008-09-10
  • 打赏
  • 举报
回复
这样,



第一式-第二式
0=a+b*y+c*z+d*w;
同除b:
0=a+y+c*z+d*w;
第三式-第四式
0=e+f*y+g*z+h*w;
同除b:
0=e+y+g*z+h*w;

一直做下去!



太乙 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 aokesha 的帖子:]
本人要通过Excel分析数据,通过4组4个已知数据求4个未知数 ,可是如果出现同列数据相等的情况 就解不出来了 所以求教各位大虾帮忙给算法 要不然这个月工资就没有了 就要喝粥了:(
公式是这样 :
未知数1 = 常数1 + 未知数2 * 常数2的开方 + 未知数3 * 常数3 + 未知数4 * 常数4;
[/Quote]

什么意思?是不是就是:

x = a+y*b+z*c+w*d;
x = e+y*f+z*g+w*h;
x = i+y*j+z*k+w*l;
x = m+y*n+z*o+w*p;

suncx001 2008-09-10
  • 打赏
  • 举报
回复
如果说有两列相等的话,行列式的值为零,方程无解

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧