Skip to content

Commit 82f5d08

Browse files
Newton's interpolation polynomial
1 parent f10f975 commit 82f5d08

File tree

8 files changed

+391
-0
lines changed

8 files changed

+391
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
2+
#include <iostream>
3+
#include <iomanip>
4+
#include <vector>
5+
#include <fstream>
6+
#include <cmath>
7+
#define PI 3.14159265358979323846
8+
using namespace std;
9+
10+
void read_dataFile(vector <double>& , vector<double>& );
11+
void Newton(vector <double> , vector <double>);
12+
void Input_Decidee();
13+
double function(double);
14+
void printVector(vector<vector<double>>, int );
15+
16+
int main()
17+
{
18+
setlocale(LC_ALL, "russian");
19+
cout << " \t\t\tИнтерполяционный полином Ньютона\n";
20+
for (int i = 0; i < 80; i++)
21+
cout << "-";
22+
23+
vector <double>x, y;
24+
cout << "\n 1. Считать данные с файла.\n"
25+
" 2. Ввести данные в ручную.\n";
26+
int chose = 0;
27+
cin >> chose;
28+
switch (chose)
29+
{
30+
case 1:
31+
cout << " В файле должно быть следующие расположение: X Y[X]\n";
32+
read_dataFile(x, y);
33+
Newton(x, y);
34+
break;
35+
case 2:
36+
Input_Decidee();
37+
break;
38+
default:
39+
break;
40+
}
41+
}
42+
43+
void read_dataFile(vector <double>& x, vector<double>& y)
44+
{
45+
ifstream file("data.txt");
46+
if (!file.is_open())
47+
{
48+
cout << "Файла не существует.";
49+
return;
50+
}
51+
52+
double temp = 0;
53+
while (!file.eof())
54+
{
55+
file >> temp;
56+
x.push_back(temp);
57+
file >> temp;
58+
y.push_back(temp);
59+
}
60+
file.close();
61+
}
62+
63+
void Newton(vector <double> x, vector <double>y)
64+
{
65+
vector<vector<double>> dy; // аналог двумерный динамический массив
66+
double h=0; //шаг
67+
68+
h = (x[1] - x[0]);
69+
for (unsigned i = 0; i < x.size(); ++i)
70+
cout << "x[" << i << "] = " << x[i] << "\t" << "y[" << x[i] << "] = " << y[i] << endl;
71+
72+
int maxIndex = x.size() - 1;
73+
dy.resize(maxIndex);
74+
for (int i = 0; i < maxIndex; i++)
75+
{
76+
dy[i].resize(maxIndex);
77+
for (int j = 0; j < maxIndex; j++)
78+
dy[i][j] = 0;
79+
}
80+
81+
//Первые конечные разности
82+
for (int i = 0; i < maxIndex; i++)
83+
dy[i][0] = y[i + 1] - y[i];
84+
85+
//Вторые
86+
for (int j = 1; j < maxIndex; j++)
87+
{
88+
for (int i = 0; i < maxIndex - j; i++) // потому что идем лестницей
89+
dy[i][j] = dy[i + 1][j - 1] - dy[i][j - 1];
90+
}
91+
cout << endl;
92+
93+
printVector(dy,maxIndex);
94+
95+
double x0 = PI / 9;
96+
cout << " \n Ввести свое значения x или задать приближенное?(0/1): ";
97+
bool flag = false; cin >> flag;
98+
if (flag == false)
99+
{
100+
cin >> x0;
101+
}
102+
double mult = 1;
103+
double sum = y[0];
104+
double q = (x0 - x[0]) / h;
105+
int factorial = 1;
106+
107+
for (int i = 0; i < maxIndex; i++)
108+
{
109+
mult *= (q - i);
110+
factorial *= i + 1;
111+
sum += mult * dy[0][i] / (factorial);
112+
}
113+
cout << "\n Первая формула Ньютона: " << setprecision(5) << sum << endl;
114+
115+
q = (x0 - x[maxIndex]) / h;
116+
sum = y[maxIndex];
117+
mult = 1;
118+
factorial = 1;
119+
for (int i = 0; i < maxIndex; i++)
120+
{
121+
mult *= (q + i);
122+
factorial *= i+1;
123+
sum += mult * dy[maxIndex - i - 1][i] / (factorial); // по мнимой диагонали
124+
}
125+
126+
cout << "\n Вторая формула Ньютона: " << setprecision(5) << sum << endl;
127+
}
128+
129+
void Input_Decidee()
130+
{
131+
vector<double> x, y;
132+
cout << " Укажите количество точек: ";
133+
int n; cin >> n;
134+
for (int i = 0; i < n; i++)
135+
{
136+
double temp = 0;
137+
cin >> temp;
138+
x.push_back(temp);
139+
y.push_back(function(temp));
140+
}
141+
system("cls");
142+
cout << endl;
143+
Newton(x, y);
144+
}
145+
146+
double function(double x)
147+
{
148+
return cos(x)* sqrt(x);//sin(x)* x;
149+
}
150+
151+
void printVector(vector<vector<double>> V,int maxIndex)
152+
{
153+
for (int i = 0; i < maxIndex; i++)
154+
{
155+
for (int j = 0; j < maxIndex; j++)
156+
cout << V[i][j] << setprecision(4) << " ";
157+
cout << endl;
158+
}
159+
}
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}") = "InterpolationNewton", "InterpolationNewton.vcxproj", "{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}"
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+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Debug|x64.ActiveCfg = Debug|x64
17+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Debug|x64.Build.0 = Debug|x64
18+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Debug|x86.ActiveCfg = Debug|Win32
19+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Debug|x86.Build.0 = Debug|Win32
20+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Release|x64.ActiveCfg = Release|x64
21+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Release|x64.Build.0 = Release|x64
22+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Release|x86.ActiveCfg = Release|Win32
23+
{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {5662B9C4-5D59-4958-94C6-787F78CB952B}
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>{50AA8E0E-E077-436D-9253-C8E13BFD6DC3}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>InterpolationNewton</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="InterpolationNewton.cpp" />
155+
</ItemGroup>
156+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
157+
<ImportGroup Label="ExtensionTargets">
158+
</ImportGroup>
159+
</Project>
Lines changed: 22 additions & 0 deletions
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="InterpolationNewton.cpp">
19+
<Filter>Исходные файлы</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>
Lines changed: 4 additions & 0 deletions
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>

InterpolationNewton/data.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0 0
2+
2 -0.6
3+
4 -1.2
4+
6 2.4
5+
8 -0.3
6+
10 -2.9

InterpolationNewton/data1.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0 0
2+
0.5235987 0.5
3+
1.0471975 0.8660254
4+
1.5707963 1
5+
2.0943951 0.8660254

0 commit comments

Comments
 (0)