谁有最速下降法的程序?
谢谢! (* STEEPEST DESCENT ALGORITHM 10.3*
*To approximate a solution P to the minimization problem
* G(P) = MIN( G(X) : X in R(n) )
* given an initial approximation x
*
* INPUT: Number n of variables; initial approximation x;
* tolerance TOL; Maximun number of iterations NN.
*
*OUTPUT: Approximate solution X or a message of failure
*
*)
F := Module[{d,i,f},
d = 0;
For[i =1, i <= n, i++,
d = d+(CF)^2;
];
f = d;
f
];
OK = 0;
n = 3;
For[i = 1,i <= n,i++,
TEMP = Input["This is the Steepest Descent Method.\n
If n is not 3 the program must be altered.\n
Input the function CF("<>ToString<>") in terms of x1,x2,x3\n"];
CF := Evaluate];
];
PT = 0;
DER = D,x1];
PT = PT+2*TEMP*DER;
DER = D,x1];
PT = PT+2*TEMP*DER;
DER = D,x1];
PT = PT+2*TEMP*DER;
P := Evaluate;
PT = 0;
DER = D,x2];
PT = PT+2*TEMP*DER;
DER = D,x2];
PT = PT+2*TEMP*DER;
DER = D,x2];
PT = PT+2*TEMP*DER;
P := Evaluate;
PT = 0;
DER = D,x3];
PT = PT+2*TEMP*DER;
DER = D,x3];
PT = PT+2*TEMP*DER;
DER = D,x3];
PT = PT+2*TEMP*DER;
P := Evaluate;
OK = 0;
While[OK == 0,
TOL = Input["Input the tolerance.\n"];
If[TOL > 0,
OK = 1,
Input["Tolerance must be positive\n
\n
Press 1 to continue\n"];
];
];
OK = 0;
While[OK == 0,
NN = Input["Input maximum number of iterations\n
no decimal points\n"];
If[NN <= 0,
Input["Must be a positive integer.\n
Enter 1 to continue\n"],
OK = 1;
];
];
For[i = 1, i <= n, i++,
X =
Input["Input the initial approximation X("<>ToString<>")\n"];
];
If[OK == 1,
FLAG1 = Input["Select output destination\n
1. Screen\n
2. Text file\n
Enter 1 or 2\n"];
If[FLAG1 == 2,
NAME = InputString["Input the file name\n
For example: output.dta\n"];
OUP = OpenWrite,
OUP = "stdout";
];
FLAG1 = Input["Select amount of output\n
1. Answer only\n
2. All intermediate approximations\n
enter 1 or 2\n"];
Write;
If[FLAG1 == 2,
Write;
];
(* Step 1*)
K = 1;
(* Step 2*)
While[OK == 1 && K <= NN,
(* Step 3*)
G = N,X,X,n]];
Z0 = 0;
For[i = 1, i <= n, i++,
Z = N,X,X]];
Z0 = Z0+(Z)^2;
];
Z0 = Sqrt;
(* Step 4*)
If[Z0 <= 10^-20,
OK = 0;
Write,
(* Step 5*)
For[i = 1, i <= n, i++,
Z = Z/Z0;
];
A = 0;
X0 = 1;
For[i = 1, i <= n, i++,
c = X-X0*Z;
];
G0 = N,c,c,n]]];
(* Step 6*)
FLAG = 1;
If,
FLAG = 0;
];
While[FLAG == 1 && OK == 1,
(* Steps 7 and 8*)
X0 = 0.5*X0;
If <= 10^-20,
OK = 0;
Write,
For[i = 1, i <= n, i++,
c = X-X0*Z;
];
G0 = N,c,c,n]]];
];
If,
FLAG = 0;
];
];
If[OK == 1,
A = X0;
G = G0;
(* Step 9*)
X0 = 0.5*X0;
For[i = 1, i <= n, i++,
c = X-X0*Z;
];
A = X0;
G = N,c,c,n]]];
(* Step 10*)
H1 = (G-G)/(A-A);
H2 = (G-G)/(A-A);
H3 = (H2-H1)/(A-A);
(* Step 11*)
X0 = 0.5*(A+A-H1/H3);
For[i = 1, i <= n, i++,
c = X-X0*Z;
];
G0 = N,c,c,n]]];
(* Step 12*)
A0 = X0;
For[i = 1, i <= n, i++,
If] < Abs,
A0 = A;
G0 = G;
];
];
If <= 10^-20,
OK = 0;
Write;
Write,
(* Step 13*)
For[i = 1, i <= n, i++,
X = Evaluate-A0*Z];
];
(* Step 14*)
If[FLAG1 == 2,
Write;
For[i = 1, i <= n, i++,
Write,10]];
];
Write;
];
If < TOL || Abs] < TOL,
OK = 0;
Write;
Write;
Write;
For[i = 1, i <= n, i++,
Write,10]];
];
Write;
Write;
Write;
Write;
Write,
(* Step 15*)
K = K+1;
];
];
];
];
];
(* Step 16*)
If[K > NN,
Write;
];
If",
Print["Output file: ",NAME," created successfully\n"];
Close;
];
]; 感谢,共扼梯度法的程序吗 原帖由 zhangmeng 于 2006-12-23 19:03 发表
感谢,共扼梯度法的程序吗
是最速下降法
页:
[1]