Skip to content

Commit 8ca17ac

Browse files
committed
牛顿迭代法
1 parent 2dff469 commit 8ca17ac

18 files changed

+76
-7
lines changed

BinaryDivide.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ double binaryDivide(double a,double b,double tol){
3434
}
3535

3636
}
37-
return result;
3837
cout << "二分法求根结束:" << endl;
38+
return result;
39+
3940
}
4041

4142
double binaryDivide(double a, double b, double tol, double(*function)(double)){
@@ -66,6 +67,7 @@ double binaryDivide(double a, double b, double tol, double(*function)(double)){
6667
}
6768
}
6869

69-
return result;
7070
cout << "二分法求根结束:" << endl;
71+
return result;
72+
7173
}

CNumericalCalculation.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@
7777
</ItemDefinitionGroup>
7878
<ItemGroup>
7979
<ClInclude Include="BinaryDivide.h" />
80+
<ClInclude Include="FixedIterator.h" />
8081
</ItemGroup>
8182
<ItemGroup>
8283
<ClCompile Include="BinaryDivide.cpp" />
84+
<ClCompile Include="FixedIterator.cpp" />
8385
<ClCompile Include="main.cpp" />
8486
</ItemGroup>
8587
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

CNumericalCalculation.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<ClInclude Include="BinaryDivide.h">
1919
<Filter>头文件</Filter>
2020
</ClInclude>
21+
<ClInclude Include="FixedIterator.h">
22+
<Filter>头文件</Filter>
23+
</ClInclude>
2124
</ItemGroup>
2225
<ItemGroup>
2326
<ClCompile Include="BinaryDivide.cpp">
@@ -26,5 +29,8 @@
2629
<ClCompile Include="main.cpp">
2730
<Filter>源文件</Filter>
2831
</ClCompile>
32+
<ClCompile Include="FixedIterator.cpp">
33+
<Filter>源文件</Filter>
34+
</ClCompile>
2935
</ItemGroup>
3036
</Project>

Debug/BinaryDivide.obj

-4 Bytes
Binary file not shown.
12.5 KB
Binary file not shown.
1.04 KB
Binary file not shown.
692 Bytes
Binary file not shown.
192 Bytes
Binary file not shown.
686 Bytes
Binary file not shown.
146 Bytes
Binary file not shown.

Debug/CNumericalCalculation.log

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
生成启动时间为 4/4/2014 11:27:11 PM。
1+
生成启动时间为 4/5/2014 6:09:47 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 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 FixedIterator.cpp main.cpp
55
main.cpp
6-
1>d:\c++\workspace\sunsystem\cnumericalcalculation\main.cpp(7): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
6+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\main.cpp(8): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
7+
FixedIterator.cpp
8+
1>d:\c++\workspace\sunsystem\cnumericalcalculation\fixediterator.cpp(14): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
9+
正在生成代码...
710
Link:
811
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
12+
Debug\FixedIterator.obj
913
Debug\main.obj
1014
CNumericalCalculation.vcxproj -> D:\C++\WorkSpace\SunSystem\Debug\CNumericalCalculation.exe
1115
1>已完成生成项目“D:\C++\WorkSpace\SunSystem\CNumericalCalculation\CNumericalCalculation.vcxproj”(Build 个目标)的操作。
1216

1317
生成成功。
1418

15-
已用时间 00:00:01.23
19+
已用时间 00:00:02.89

Debug/FixedIterator.obj

154 KB
Binary file not shown.

Debug/main.obj

1.78 KB
Binary file not shown.

Debug/vc120.idb

40 KB
Binary file not shown.

Debug/vc120.pdb

0 Bytes
Binary file not shown.

FixedIterator.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "FixedIterator.h"
2+
#include <iostream>
3+
#include <iomanip>
4+
5+
using namespace std;
6+
7+
8+
double fixedIterator(double p0, double tol,int N){
9+
10+
int i = 0;
11+
double p;
12+
cout << "不动点迭代开始:" << endl;
13+
while (i < (N+1)){
14+
p = p0 - (powf(p0, 3) + 4 * powf(p0, 2) - 10.0) / (3 * powf(p0, 2) + 8 * p0);
15+
cout << setprecision(16)<<"p:" << p << endl << endl;
16+
if (fabs(p - p0) < tol){
17+
cout << "不动点迭代结束:" << endl;
18+
return p;
19+
}
20+
p0 = p;
21+
}
22+
cout << "不动点迭代结束:迭代失败!" << endl;
23+
return NULL;
24+
}
25+
26+
double fixedIterator(double p0, double tol,int N, double(*function)(double p)){
27+
int i = 0;
28+
double p;
29+
cout << "不动点迭代开始:" << endl;
30+
while (i < (N + 1)){
31+
p = function(p0);
32+
cout << setprecision(16)<<"p:" << endl << endl;
33+
if (fabs(p - p0) < tol){
34+
cout << "不动点迭代结束:" << endl;
35+
}
36+
p0 = p;
37+
}
38+
cout << "不动点迭代结束:迭代失败!" << endl;
39+
return NULL;
40+
}

FixedIterator.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+
//˛ťśŻľăľü´úˇ¨p=g(p)
7+
double fixedIterator(double p0, double tol,int N);
8+
double fixedIterator(double p0, double tol,int N, double(*function)(double p));

main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "BinaryDivide.h"
2+
#include "FixedIterator.h"
23
#include <iostream>
34

45
using namespace std;
@@ -7,6 +8,12 @@ double test(double p){
78
return powf(p, 3) + 4 * powf(p, 2) - 10.0;
89
};
910

11+
void testBinaryDivide(){
12+
cout << "¸ùΪ£º"<<binaryDivide(1.0, 2.0,0.000002,&test) << endl;
13+
}
14+
void testFixedIterator(){
15+
cout << "¸ùΪ:" << fixedIterator(1.0, 0.000001, 10) << endl;
16+
}
1017
int main(){
11-
cout << "¸ùΪ£º"<<binaryDivide(1.0, 2.0,0.000001,&test) << endl;
18+
testFixedIterator();
1219
}

0 commit comments

Comments
 (0)