using System using System Collections Generic using System Drawing usi

  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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LR2
{
class MnkOrtho
{
PictureBox pictureBox;
RichTextBox richTextBox;
List<PointF> listPoints;
Graphics g;
float scale = 35;
public MnkOrtho(PictureBox pictureBox, RichTextBox richTextBox, float scale = 35)
{
this.scale = scale;
this.pictureBox = pictureBox;
this.richTextBox = richTextBox;
if (pictureBox != null)
{
g = pictureBox.CreateGraphics();
g.TranslateTransform(pictureBox.Width / 2, pictureBox.Height / 2);
g.Clear(Color.White);
g.DrawLine(Pens.Black, 0, -200, 0, 200);
g.DrawLine(Pens.Black, -200, 0, 200, 0);
}
}
public void solve(List<PointF> listPoints)
{
this.listPoints = listPoints;
List<PointF> resultPoints = new List<PointF>();
for (double x = -7; x < 9; x += 3)
{
resultPoints.Add(new PointF((float)x, (float)GetApproximation(x)));
}
drawOriginalCurve();
drawApproximationCurve(resultPoints);
}
public double GetQ(double x, int k)
{
double retValue = 0.0;
if (k < 0)
{
retValue = 0.0;
return retValue;
}
else if (k == 0)
{
retValue = 1.0;
return retValue;
}
else if (k > 0)
{
if (k > 1)
retValue = x * GetQ(x, k - 1) - GetAlpha(x, k - 1) * GetQ(x, k - 1) - GetBeta(x, k - 1) * GetQ(x, k - 2);
else
retValue = x * GetQ(x, k - 1) - GetAlpha(x, k - 1) * GetQ(x, k - 1);
return retValue;
}
return retValue;
}
public double GetAlpha(double x, int k)
{
double chisl = 0.0,
znam = 0.0,
retValue = 0.0,
temp = 0.0;
for (int i = 0; i < listPoints.Count; ++i)
{
temp = GetQ(listPoints[i].X, k);
chisl += listPoints[i].X * temp * temp;
znam += temp * temp;
}
retValue = chisl / znam;
return retValue;
}
public double GetBeta(double x, int k)
{
double chisl = 0.0,
znam = 0.0,
retValue = 0.0,
temp1 = 0.0,
temp2 = 0.0;
for (int i = 0; i < listPoints.Count; ++i)
{
temp1 = GetQ(listPoints[i].X, k);
temp2 = GetQ(listPoints[i].X, k - 1);
chisl += listPoints[i].X * temp1 * temp2;
znam += temp2 * temp2;
}
retValue = chisl / znam;
return retValue;
}
public double GetFx(double x)
{
return Math.Sin(x) * Math.Sin(x) * Math.Sin(x) * Math.Sin(x) * Math.Sin(x);//Math.Abs(Math.Sin(2 * x)) * Math.Cos(x);
}
public double GetC(int k)
{
double retValue = 0.0,
chisl = 0.0,
znam = 0.0;
for (int i = 0; i < listPoints.Count; ++i)
{
double curQ = GetQ(listPoints[i].X, k);
chisl += curQ * listPoints[i].Y;
znam += curQ * curQ;
}
retValue = chisl / znam;
return retValue;
}
public double GetApproximation(double x)
{
double retValue = 0.0;
for (int i = 0; i < listPoints.Count; ++i)
retValue += GetC(i) * GetQ(x, i);
return retValue;
}
private bool isEqualNull(double x)
{
return Math.Abs(x) <= 1e-8;
}
private void drawApproximationCurve(List<PointF> points)
{
int n = points.Count;
for (int i = 1; i < n; ++i)
{
g.DrawLine(Pens.Blue, points[i].X * scale, -points[i].Y * scale,
points[i - 1].X * scale, -points[i - 1].Y * scale);
}
}
private void drawOriginalCurve()
{
PointF curPoint = new PointF(0, 0),
nextPoint = new PointF(0, 0);
bool firstIter = true;
for (double x = -10; x < 10; x += 0.01)
{
curPoint = nextPoint;
nextPoint = new PointF((float)x, (float)GetFx(x));
if (firstIter)
{
firstIter = false;
continue;
}
g.DrawLine(Pens.Red, nextPoint.X * scale, -nextPoint.Y * scale,
curPoint.X * scale, -curPoint.Y * scale);
}
}
}
}