有意思,有人问算法,有人贴算法问用法,说不定被我捡了便宜两边得分。哈哈,我笑。
问题:关于最小二乘法解线性方程组的源程序 ( 积分:30, 回复:1, 阅读:35 )
分类:非技术问题 ( 版主:悲酥清风, robertcool )
来自:tom12345, 时间:01-8-19 21:57:00, ID:601778 [显示:小字体 | 大字体]
以下是来自JohnsonGuo的最小二乘法解线性方程组的源程序, 能否给出调用实例?
type
TMatrix = array of array ofdo
uble;
procedure MinSqrMul(m, n: Integer;
A: TMatrix;
b: array ofdo
uble;
var x: array ofdo
uble);
var
i, j, k: Integer;
ATA: array of array ofdo
uble;
ATb: array ofdo
uble;
Elem:do
uble;
begin
SetLength(ATA, n, n);
SetLength(ATb, n);
for i := 0 to n - 1do
for j := 0 to n - 1do
begin
ATA[i, j] := 0;
for k := 0 to m - 1do
ATA[i, j] := ATA[i, j] + A[k, i] * A[k, j];
end;
for i := 0 to n - 1do
begin
ATb := 0;
for j := 0 to m - 1do
ATb := ATb + A[j, i] * b[j];
end;
for i := 0 to n - 1do
begin
Elem := ATA[i, i];
for j := i to n - 1do
ATA[i, j] := ATA[i, j] / Elem;
ATb := ATb / Elem;
for k := i + 1 to n - 1do
begin
Elem := -ATA[k, i];
for j := k to n - 1do
ATA[k, j] := ATA[k, j] + ATA[i, j] * Elem;
ATb[k] := ATb[k] + ATb * Elem;
end;
end;
x[n - 1] := ATb[n - 1];
for i := n - 2do
wnto 0do
begin
for j := i + 1 to n - 1do
ATb := ATb - ATA[i, j] * x[j];
x := ATb;
end;
end;
来自:sonie, 时间:01-8-26 16:15:00, ID:607026
var
m,n:integer;
a:array[1..2] of array[1..2]do
uble ;
b: array[1..2] ofdo
uble;
x: array[1..2] ofdo
uble
begin
m:=2;
n:=2;
a:={{3,4},{5,6}}
b:={7,8};
MinSqrMul(m, n, A, b,x);
end;
得出的x数组就是
3x+3y=7;
5x+6y=8;
的值