-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
98 lines (87 loc) · 1.75 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
double fun1(double x, double y);
double fun2(double x, double y);
double fun3(double x, double y);
void Euler(int n, double b, double x, double y);
void Heunn(int n, double b, double x, double y);
void Euler2(int n, double b, double x, double y);
int main()
{
double a=0, b;
int n, t;
cout << "Podaj n i b: ";
cin >> n >> b;
cout << "1. Euler\n";
cout << "2. Heun\n";
cout << "3. Ten trzeci\n";
cin >> t;
switch(t)
{
case 1:
{
double x=0, y=3;
Euler(n, b, x, y);
break;
}
case 2:
{
double x=1, y=2;
Heunn(n, b, x, y);
break;
}
case 3:
{
double x=0, y=1;
Euler2(n, b, x, y);
break;
}
}
return 0;
}
double fun1(double x, double y)
{
return y*y/(x+1);
}
double fun2(double x, double y)
{
return y/x/x;
}
double fun3(double x, double y)
{
return 2*x*y;
}
void Euler(int n, double b, double x, double y)
{
double h=(b-x)/n;
for(int i=0; i<n; i++)
{
y = y + h*fun1(x, y);
x += h;
}
//cout << "x: " << x << endl;
cout << "y: " << y << endl;
}
void Heunn(int n, double b, double x, double y)
{
double h=(b-x)/n;
for(int i=0; i<n; i++)
{
double w = fun2(x,y);
y = y + 0.5*h*(w+fun2(x+h,y+h*w));
x += h;
}
//cout << "x: " << x << endl;
cout << "y: " << y << endl;
}
void Euler2(int n, double b, double x, double y)
{
double h=(b-x)/n;
for(int i=0; i<n; i++)
{
y = y + h*fun3(x+h/2,y+h/2*fun3(x,y));
x += h;
}
//cout << "x: " << x << endl;
cout << "y: " << y << endl;
}