Skip to content

Commit e2465db

Browse files
Extra code
Legandre Numerical solution of nonlinear equations with one unknown
1 parent 6652817 commit e2465db

25 files changed

+1097
-0
lines changed
10.6 KB
Loading
8.83 KB
Loading
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Newton's method.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
2+
//
3+
4+
#include <iostream>
5+
#include <cmath>
6+
#include <iomanip>
7+
8+
using namespace std;
9+
10+
double function(double);
11+
double derivative_first(double(double), double);
12+
/*double derivative_second(double(double), double);*/
13+
14+
int main()
15+
{
16+
//Distance
17+
double a = 0, b = 0;
18+
cout << " Input a: "; cin >> a;
19+
cout << " Input b: "; cin >> b;
20+
cout << " Distance: [" << a << " , " << b << "]" << endl;
21+
22+
double x0 = 0.0; // начальное приближение
23+
cout << " Initial value x0 in [a,b] : "; cin >> x0;
24+
if (x0<a || x0>b)
25+
{
26+
while (x0 < a || x0 > b)
27+
{
28+
cout << " !!! x !>b | x!<a. Input x0 in [a,b] : ";
29+
cin >> x0;
30+
}
31+
}
32+
/*if (function(a) * derivative_second(function, a) > 0 || function(a) * derivative_second(function, a) < 0)
33+
x0 = a;
34+
else
35+
if (function(b) * derivative_second(function, b) > 0 || function(b) * derivative_second(function, b) < 0)
36+
x0 = b;
37+
if (x0 == a)
38+
cout << " x0 = a: " << x0 << endl;
39+
else
40+
if(x0==b)
41+
cout << " x0 = b: " << x0 << endl;
42+
else
43+
{
44+
cout << " x0 = inf";
45+
return 0;
46+
}*/
47+
48+
//y=f(x0)+f'(x0)(x-x0)
49+
double x = 0;
50+
int iter = 0;
51+
cout << " Epsilon : " << DBL_EPSILON << endl;
52+
for(;;)
53+
{
54+
double F = function(x0);
55+
double F1 = derivative_first(function,x0);
56+
x = x0 - F / F1;
57+
cout << "Iteration : " << iter << " F = " << F << setprecision(5) << ";" << setprecision(5) <<" F1 = " << F1 << ";" << setprecision(5) << " x = " << x << endl;
58+
59+
if (abs(x - x0) < DBL_EPSILON)
60+
break;
61+
else
62+
x0 = x;
63+
64+
iter++;
65+
66+
if (iter > 1000)
67+
{
68+
cout << " Count iteration very much. The initial value(x0) is not selected correctly. Return programm...";
69+
return 0;
70+
}
71+
}
72+
73+
74+
cout << endl;
75+
cout << " Count iteration : " << iter << "; root x = " << x << setprecision(5) << "; f(x) = " << function(x) << setprecision(5);
76+
cout << endl;
77+
return 0;
78+
}
79+
80+
double function(double x)
81+
{
82+
return x*x*x+3*x*x-3; //[0.5,1.5]
83+
}
84+
85+
double derivative_first(double func(double), double x)
86+
{
87+
const double dx = 1e-10;
88+
return (function(x + dx) - function(x)) / dx;
89+
}
90+
91+
/*double derivative_second(double func(double), double x)
92+
{
93+
const double dx = 1e-10;
94+
return ((function(x + dx) - function((x + dx) + dx)) / ((x + dx) - ((x + dx) + dx)) - (function(x) - function(x + dx)) / (x - (x + dx)))/dx;
95+
}
96+
97+
/*double derivative_second(double derivative_first(double,double), double x)
98+
{
99+
const double dx = 1e-10;
100+
double y = x + dx;
101+
return (derivative_first(function(x),(x + dx)) - derivative_first(function(x), x)) / dx;
102+
}*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28803.202
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Newton's method", "Newton's method.vcxproj", "{67C03C6F-7898-43AB-830F-349B3720B0E4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Debug|x64.ActiveCfg = Debug|x64
17+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Debug|x64.Build.0 = Debug|x64
18+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Debug|x86.ActiveCfg = Debug|Win32
19+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Debug|x86.Build.0 = Debug|Win32
20+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Release|x64.ActiveCfg = Release|x64
21+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Release|x64.Build.0 = Release|x64
22+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Release|x86.ActiveCfg = Release|Win32
23+
{67C03C6F-7898-43AB-830F-349B3720B0E4}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {57D037DA-8BB5-4242-BBF2-5B6F268027C9}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<ProjectGuid>{67C03C6F-7898-43AB-830F-349B3720B0E4}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>Newtonsmethod</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
77+
<LinkIncremental>true</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
80+
<LinkIncremental>false</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
86+
<ClCompile>
87+
<PrecompiledHeader>
88+
</PrecompiledHeader>
89+
<WarningLevel>Level3</WarningLevel>
90+
<Optimization>Disabled</Optimization>
91+
<SDLCheck>true</SDLCheck>
92+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<ConformanceMode>true</ConformanceMode>
94+
</ClCompile>
95+
<Link>
96+
<SubSystem>Console</SubSystem>
97+
<GenerateDebugInformation>true</GenerateDebugInformation>
98+
</Link>
99+
</ItemDefinitionGroup>
100+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
101+
<ClCompile>
102+
<PrecompiledHeader>
103+
</PrecompiledHeader>
104+
<WarningLevel>Level3</WarningLevel>
105+
<Optimization>Disabled</Optimization>
106+
<SDLCheck>true</SDLCheck>
107+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
108+
<ConformanceMode>true</ConformanceMode>
109+
</ClCompile>
110+
<Link>
111+
<SubSystem>Console</SubSystem>
112+
<GenerateDebugInformation>true</GenerateDebugInformation>
113+
</Link>
114+
</ItemDefinitionGroup>
115+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
116+
<ClCompile>
117+
<PrecompiledHeader>
118+
</PrecompiledHeader>
119+
<WarningLevel>Level3</WarningLevel>
120+
<Optimization>MaxSpeed</Optimization>
121+
<FunctionLevelLinking>true</FunctionLevelLinking>
122+
<IntrinsicFunctions>true</IntrinsicFunctions>
123+
<SDLCheck>true</SDLCheck>
124+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
125+
<ConformanceMode>true</ConformanceMode>
126+
</ClCompile>
127+
<Link>
128+
<SubSystem>Console</SubSystem>
129+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
130+
<OptimizeReferences>true</OptimizeReferences>
131+
<GenerateDebugInformation>true</GenerateDebugInformation>
132+
</Link>
133+
</ItemDefinitionGroup>
134+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
135+
<ClCompile>
136+
<PrecompiledHeader>
137+
</PrecompiledHeader>
138+
<WarningLevel>Level3</WarningLevel>
139+
<Optimization>MaxSpeed</Optimization>
140+
<FunctionLevelLinking>true</FunctionLevelLinking>
141+
<IntrinsicFunctions>true</IntrinsicFunctions>
142+
<SDLCheck>true</SDLCheck>
143+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
144+
<ConformanceMode>true</ConformanceMode>
145+
</ClCompile>
146+
<Link>
147+
<SubSystem>Console</SubSystem>
148+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
149+
<OptimizeReferences>true</OptimizeReferences>
150+
<GenerateDebugInformation>true</GenerateDebugInformation>
151+
</Link>
152+
</ItemDefinitionGroup>
153+
<ItemGroup>
154+
<ClCompile Include="..\..\Newton%27s method\Newton%27s method\Newton%27s method.cpp" />
155+
</ItemGroup>
156+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
157+
<ImportGroup Label="ExtensionTargets">
158+
</ImportGroup>
159+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Исходные файлы">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Файлы заголовков">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Файлы ресурсов">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="..\..\Newton%27s method\Newton%27s method\Newton%27s method.cpp">
19+
<Filter>Исходные файлы</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup />
4+
</Project>

0 commit comments

Comments
 (0)