Skip to content

Commit 5d3c1b9

Browse files
committed
Horner算法和Muller算法完成。
1 parent 3899993 commit 5d3c1b9

27 files changed

+220
-12
lines changed

CNumericalCalculation.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@
7878
<ItemGroup>
7979
<ClInclude Include="BinaryDivide.h" />
8080
<ClInclude Include="FixedIterator.h" />
81+
<ClInclude Include="Horner.h" />
82+
<ClInclude Include="Interpolation.h" />
8183
<ClInclude Include="NewTonIterator.h" />
84+
<ClInclude Include="Steffensen.h" />
8285
</ItemGroup>
8386
<ItemGroup>
8487
<ClCompile Include="BinaryDivide.cpp" />
8588
<ClCompile Include="FixedIterator.cpp" />
89+
<ClCompile Include="Horner.cpp" />
90+
<ClCompile Include="Interpolation.cpp" />
8691
<ClCompile Include="main.cpp" />
8792
<ClCompile Include="NewTonIterator.cpp" />
93+
<ClCompile Include="Steffensen.cpp" />
8894
</ItemGroup>
8995
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
9096
<ImportGroup Label="ExtensionTargets">

CNumericalCalculation.vcxproj.filters

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
<ClInclude Include="NewTonIterator.h">
2525
<Filter>头文件</Filter>
2626
</ClInclude>
27+
<ClInclude Include="Steffensen.h">
28+
<Filter>头文件</Filter>
29+
</ClInclude>
30+
<ClInclude Include="Horner.h">
31+
<Filter>头文件</Filter>
32+
</ClInclude>
33+
<ClInclude Include="Interpolation.h">
34+
<Filter>头文件</Filter>
35+
</ClInclude>
2736
</ItemGroup>
2837
<ItemGroup>
2938
<ClCompile Include="BinaryDivide.cpp">
@@ -38,5 +47,14 @@
3847
<ClCompile Include="NewTonIterator.cpp">
3948
<Filter>源文件</Filter>
4049
</ClCompile>
50+
<ClCompile Include="Steffensen.cpp">
51+
<Filter>源文件</Filter>
52+
</ClCompile>
53+
<ClCompile Include="Horner.cpp">
54+
<Filter>源文件</Filter>
55+
</ClCompile>
56+
<ClCompile Include="Interpolation.cpp">
57+
<Filter>源文件</Filter>
58+
</ClCompile>
4159
</ItemGroup>
4260
</Project>

Debug/BinaryDivide.obj

0 Bytes
Binary file not shown.
34.3 KB
Binary file not shown.
772 Bytes
Binary file not shown.
1.99 KB
Binary file not shown.
536 Bytes
Binary file not shown.
842 Bytes
Binary file not shown.
418 Bytes
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\vc120.pdb
2+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\vc120.idb
3+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\binarydivide.obj
4+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\main.obj
5+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\fixediterator.obj
6+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\newtoniterator.obj
7+
d:\c++\workspace\sunsystem\debug\cnumericalcalculation.ilk
8+
d:\c++\workspace\sunsystem\debug\cnumericalcalculation.exe
9+
d:\c++\workspace\sunsystem\debug\cnumericalcalculation.pdb
10+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\cl.command.1.tlog
11+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\cl.read.1.tlog
12+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\cl.write.1.tlog
13+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\link.command.1.tlog
14+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\link.read.1.tlog
15+
d:\c++\workspace\sunsystem\cnumericalcalculation\debug\cnumeric.7d3823d4.tlog\link.write.1.tlog

Debug/CNumericalCalculation.log

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
生成启动时间为 4/6/2014 8:11:34 PM。
1+
生成启动时间为 4/14/2014 9:18:22 PM。
22
1>项目“D:\C++\WorkSpace\SunSystem\CNumericalCalculation\CNumericalCalculation.vcxproj”在节点 2 上(Build 个目标)。
33
1>ClCompile:
4-
D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt NewTonIterator.cpp main.cpp
4+
D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt Horner.cpp main.cpp
55
main.cpp
6-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\main.cpp(10): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
7-
NewTonIterator.cpp
8-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\newtoniterator.cpp(6): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
9-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\newtoniterator.cpp(7): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
10-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\newtoniterator.cpp(23): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
6+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\main.cpp(12): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
7+
Horner.cpp
8+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\horner.cpp(37): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
9+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\horner.cpp(53): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
10+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\horner.cpp(55): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
1111
正在生成代码...
12-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\newtoniterator.cpp(27): warning C4715: “NewTonIterator”: 不是所有的控件路径都返回值
13-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\newtoniterator.cpp(48): warning C4715: “NewTonIterator”: 不是所有的控件路径都返回值
12+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\horner.cpp(80): warning C4715: “Muller”: 不是所有的控件路径都返回值
1413
Link:
1514
D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"D:\C++\WorkSpace\SunSystem\Debug\CNumericalCalculation.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"D:\C++\WorkSpace\SunSystem\Debug\CNumericalCalculation.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"D:\C++\WorkSpace\SunSystem\Debug\CNumericalCalculation.lib" /MACHINE:X86 Debug\BinaryDivide.obj
1615
Debug\FixedIterator.obj
16+
Debug\Horner.obj
17+
Debug\Interpolation.obj
1718
Debug\main.obj
1819
Debug\NewTonIterator.obj
20+
Debug\Steffensen.obj
1921
CNumericalCalculation.vcxproj -> D:\C++\WorkSpace\SunSystem\Debug\CNumericalCalculation.exe
2022
1>已完成生成项目“D:\C++\WorkSpace\SunSystem\CNumericalCalculation\CNumericalCalculation.vcxproj”(Build 个目标)的操作。
2123

2224
生成成功。
2325

24-
已用时间 00:00:02.24
26+
已用时间 00:00:02.26

Debug/FixedIterator.obj

-211 Bytes
Binary file not shown.

Debug/Horner.obj

158 KB
Binary file not shown.

Debug/Interpolation.obj

131 KB
Binary file not shown.

Debug/NewTonIterator.obj

0 Bytes
Binary file not shown.

Debug/Steffensen.obj

152 KB
Binary file not shown.

Debug/main.obj

3.09 KB
Binary file not shown.

Debug/vc120.idb

32 KB
Binary file not shown.

Debug/vc120.pdb

8 KB
Binary file not shown.

FixedIterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ double fixedIterator(double p0, double tol,int N, double(*function)(double p)){
3131
p = function(p0);
3232
cout << setprecision(16)<<"p:" << endl << endl;
3333
if (fabs(p - p0) < tol){
34-
cout << "不 动点迭代结束" << endl;
34+
cout << "不动点迭代结束" << endl;
3535
}
3636
p0 = p;
3737
}

Horner.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "Horner.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
7+
pair<double,double> Horner(double x0){
8+
double a[5];
9+
a[4] = 2;
10+
a[3] = 0;
11+
a[2] = -3;
12+
a[1] = 3;
13+
a[0] = -4;
14+
15+
double y = a[4];
16+
double z = a[4];
17+
for (int j = 1; j < 4; j++){
18+
y = x0*y + a[4 - j];
19+
z = x0*z + y;
20+
}
21+
y = x0*y + a[0];
22+
return pair<double,double>(y,z);
23+
}
24+
25+
pair<double,double> Horner(double x0,int n,double a[], double(*function)(double)){
26+
double y = a[n - 1];
27+
double z = a[n - 1];
28+
for (int j = 1; j < n - 1; j++){
29+
y = x0*y + a[n - 1 - j];
30+
z = x0*z + y;
31+
}
32+
y = x0*y + a[0];
33+
return pair<double,double>(y,z);
34+
}
35+
36+
double testMuller(double x){
37+
return 16 * powf(x, 4) - 40 * powf(x, 3) + 5 * powf(x, 2) + 20 * x + 6;
38+
}
39+
double Muller(double x0, double x1, double x2, double tol, int n, double(*function)(double)){
40+
41+
double h1 = x1 - x0;
42+
double h2 = x2 - x1;
43+
double p1 = (function(x1) - function(x0)) / h1;
44+
double p2 = (function(x2) - function(x1)) / h2;
45+
double d = (p2 - p1) / (h2 + h1);
46+
cout << d << endl;
47+
int i = 3;
48+
cout << "Muller算法开始:" << endl;
49+
while (i <= n){
50+
double b = p2 + h2*d;
51+
52+
//可能复数运算
53+
double D = sqrtf(powf(b, 2) - 4 * function(x2)*d);
54+
double E;
55+
if (fabsf(b - D) < fabsf(b + D)){
56+
E = b + D;
57+
}
58+
else{
59+
E = b - D;
60+
}
61+
double h = -2 * function(x2) / E;
62+
double p = x2 + h;
63+
cout << "p:" << p << endl;
64+
if (fabs(h) < tol){
65+
cout << "Muller算法结束!" << endl;
66+
return p;
67+
}
68+
//设置下一次迭代
69+
x0 = x1;
70+
x1 = x2;
71+
x2 = p;
72+
h1 = x1 - x0;
73+
h2 = x2 - x1;
74+
p1 = (function(x1) - function(x0)) / h1;
75+
p2 = (function(x2) - function(x1)) / h2;
76+
d = (p2 - p1) / (h2 + h1);
77+
i = i + 1;
78+
}
79+
cout << "Muller算法失败!" << endl;
80+
}

Horner.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
using namespace std;;
4+
5+
6+
//Horner算法,求多项式p(x)=AnX^n+An-1X^n-1+....a1X+a0=(x-x0)Q(x)+b0的值和导数
7+
8+
pair<double,double> Horner(double x0);
9+
pair<double,double> Horner(double x0, int n,double a[],double(*function)(double));
10+
11+
double testMuller(double x);
12+
// Muller算法,求多项式的m重根
13+
double Muller(double x0, double x1, double x2, double tol, int n, double(*function)(double));

Interpolation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "Interpolation.h"
2+
#include <iostream>
3+
4+
using namespace std;

Interpolation.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
/*
6+
²åÖµÓë¶àÏîʽ±Æ½ü
7+
*/
8+

Steffensen.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "Steffensen.h"
2+
#include <iostream>
3+
4+
double Steffensen(double p0, double tol, int n){
5+
int i = 1;
6+
double p1, p2,p;
7+
cout << "Steffense快速收敛算法开始:" << endl;
8+
while (i <= n){
9+
p1 = sqrtf(10/(p0+4));
10+
p2 = sqrtf(10/(p1+4));
11+
p = p0 - powf(p1 - p0, 2) / (p2 - 2 * p1 + p0);
12+
cout << "p:" << p << endl;
13+
if (fabs(p - p0) < tol){
14+
cout << "Steffense快速收敛算法结束:" << endl;
15+
return p;
16+
}
17+
i += 1;
18+
p0 = p;
19+
}
20+
cout << "Stenffense快速收敛算法失败!" << endl;
21+
return NULL;
22+
}
23+
24+
double Steffensen(double p0, double tol, int n, double(*function)(double)){
25+
int i = 1;
26+
double p1, p2, p;
27+
cout << "Steffense快速收敛算法开始:" << endl;
28+
while (i <= n){
29+
p1 = function(p0);
30+
p2 = function(p1);
31+
p = p0 - powf(p1 - p0, 2) / (p2 - 2 * p1 + p0);
32+
cout << "p:" << p << endl;
33+
if (fabsf(p - p0) < tol){
34+
cout << "Steffense快速收敛算法结束:" << endl;
35+
return p;
36+
}
37+
i = i + 1;
38+
p0 = p;
39+
}
40+
41+
cout << "Stenffense快速收敛算法失败!" << endl;
42+
return NULL;
43+
}

Steffensen.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
//Steffensen方法是具有二次收敛速度的算法,不动点迭代算法改进性
7+
double Steffensen(double p0, double tol, int N);
8+
double Steffensen(double p0, double tol, int N, double(*function)(double));

main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "BinaryDivide.h"
22
#include "FixedIterator.h"
33
#include "NewTonIterator.h"
4+
#include "Steffensen.h"
5+
#include "Horner.h"
46
#include <iostream>
57
#include <algorithm>
68

@@ -19,8 +21,17 @@ void testFixedIterator(){
1921
void testNewTonIterator(){
2022
cout << "根为:" << NewTonIterator(0.5, 3.1415926/4.0,0.000001,10,test) << endl;
2123
}
24+
void testSteffense(){
25+
cout << "根为:" << Steffensen(1.5, 0.00000001, 10) << endl;
26+
}
27+
void testHorner(){
28+
cout << "根为:" <<Horner(-2).second << endl;
29+
cout << "Muller根为:" << Muller(0.5, 1.0, 1.5, 0.00001, 10, testMuller) << endl;
30+
}
31+
2232
int main(){
2333
testFixedIterator();
2434
testNewTonIterator();
25-
35+
testSteffense();
36+
testHorner();
2637
}

0 commit comments

Comments
 (0)