Інтерполяція функцій

Додаткі.doc (1 стор.)
курсова_ластl.xls (1 стор.)
Курсова_ЧМ.doc (3 стор.)
Оригінал


Додаток 1. Код програми

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using Microsoft.Office.Interop.Excel;

using System.Windows.Forms;

using System.IO;
namespace InterPolation

{

public class InterPolationClass

{

public int N;

public double Shag;

public int Ntable;

public int NN;

public double [] [] deltamas;

public double [] masY, masX;

public int countgraphics;

bool bFN = true;

bool bSN = true;

double [] [] F = null;

List <String> Y = new List <String> ();

List <String> lbls = new List <String> ();
public System.Windows.Forms.PictureBox p;

public Chart chartPage;

public System.Reflection.Missing misValue;

Workbook xlBook;

Worksheet xlSheet;

SeriesCollection sC;
public InterPolationClass (double shag, int ntable, int n, int Nnerivni, double [] masX0, double [] masY0, / * System.Windows.Forms.Label label, / * System.Windows.Forms.Panel p * / System. Windows.Forms.PictureBox p)

{

Shag = shag;

N = n;

/ / Lbl = label;

masX = masX0;

masY = masY0;

Ntable = ntable;

NN = Nnerivni;

this.p = p;

DeltaCount ();

countgraphics = 0;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass ();
misValue = System.Reflection.Missing.Value;
xlBook = xlApp.Workbooks.Add (misValue);

xlSheet = (Worksheet) xlBook.Worksheets.get_Item (1);
ChartObjects xlChart = (ChartObjects) xlSheet.ChartObjects (Type.Missing);

ChartObject myChart = (ChartObject) xlChart.Add (10, 80, 300, 250);

chartPage = myChart.Chart;

/ / Series [] s = new Series [8];

chartPage.ChartType = XlChartType.xlXYScatterLines;

sC = (SeriesCollection) myChart.Chart.SeriesCollection (Type.Missing);

Додаток 1 (продовження 1)

}
public bool DeltaCount ()

{

deltamas = new double [N] [];

for (int i = 0; i <N; i + +)

{

deltamas [i] = new double [Ntable - i];

}

for (int i = 0; i <N; i + +)

for (int j = 0; j <Ntable - i - 1; j + +)

{

deltamas [i] [j] = masY [j + 1] - masY [j];

}

for (int i = 1; i <N; i + +)

for (int j = 0; j <Ntable - i - 1; j + +)

deltamas [i] [j] = deltamas [i - 1] [j + 1] - deltamas [i - 1] [j];

if (IsCount ()) return true;

else return false;

}

public Int32 Fact (int x)

{

if ((x == 1) | | (x == 0)) return 1;

else return Fact (x - 1) * x;

}

public bool IsCount ()

{

bool b = true;

for (int i = 0; i <N; i + +)

{

for (int j = 0; j <Ntable-i; j + +)

{

if (deltamas [i] [j]! = 0)

{

b = false; break;

}

}

if (b == true) return false;

}

return true;

}

public bool IsCount2 ()

{

for (int i = 0; i <N; i + +)

{

for (int j = 0; j <Ntable; j + +)

{

if (deltamas [i] [j] == 0)

{

return false;

}

}

}

return true;

}


public double EvalNewtonNerivni (double x)

{

# Region Evaluate

F = new double [N] [];

for (int i = 0; i <N; i + +)

Додаток 1 (продовження 2)
F [i] = new double [Ntable - i];

F [0] = masY;

for (int i = 1; i <N; i + +)

{

for (int j = 0; j <Ntable-i; j + +)

{

F [i] [j] = (F [i - 1] [j +1] - F [i - 1] [j]);

}

}

# Endregion

double curr;

double tmp = 0.0;

int currnum = 0;

if (x <= masX [0]) currnum = 0;

else if (x> = masX [Ntable - 1]) currnum = Ntable - 1;

else

{

for (int t = 0; t <Ntable; t + +)

{

if (masX [t] <= x) currnum = t;

}

}

if (currnum> F [N-1]. Count () -1) currnum = F [N-1]. Count () -1;

curr = masY [currnum];

for (int i = 1; i <N; i + +)

{

tmp = F [i] [currnum];

for (int j = 0; j <i; j + +)

{

tmp * = (x - masX [j + currnum]);

}

curr + = tmp;

}

if (Y.Contains ("Формула Ньютона (нерівні Вузли):" + curr.ToString ()))

Y.Add ("Формула Ньютона (нерівні Вузли):" + curr.ToString ());

return curr;

}
public double EvalFFNewton (double x)

{

/ / Перша формула Ньютона

this.DeltaCount ();

double res, res2 = 1.0;

res = masY [GetX0 (x)];

/ / Double q = (1 / Shag) * (x - masX [0]);

double q = (1 / Shag) * (x - masX [GetX0 (x)]);

for (int i = 0; i <N; i + +)

{

res2 * = (q - i) / (i + 1) ;/ / Fact (i + 1);

try

{

res + = deltamas [i] [GetX0 (x)] * res2;

}

catch {/ * b = true; bFN = false; lbl.Text = ""; MessageBox.Show ("Не можу порахувати за першою формулою Ньютона!"); * / break;}

}

return res;

}

public void EvalFFNewton (double x, System.Windows.Forms.Label lbl)

{

/ / Перша формула Ньютона

Додаток 1 (продовження 3)
bool b = false;

if (this.DeltaCount ())

{

double res, res2 = 1.0;

res = masY [GetX0 (x)];

/ / Double q = (1 / Shag) * (x - masX [0]);

double q = (1/Shag) * (x - masX [GetX0 (x)]);

for (int i = 0; i <N; i + +)

{

res2 * = (q - i) / (i + 1) ;/ / Fact (i + 1);

try

{

res + = deltamas [i] [GetX0 (x)] * res2;

}

catch {b = true; bFN = false; lbl.Text = ""; MessageBox.Show ("Не можу порахувати за першою формулою Ньютона!"); break;}

}

if (! b)

{

if (! Y.Contains ("Перша формула Ньютона:" + res.ToString ()))

Y.Add ("Перша формула Ньютона:" + res.ToString ());

/ / Return res;

/ / If (IsCount ())

/ / {

try

{

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + / * EvalFFNewton (x). ToString () * / res.ToString () + "");

lbl.Text + = "=" + q.ToString ();

}

catch {}

/ /}

/ / Else lbl.Text = "";

}

}

else {MessageBox.Show (""); lbl.Text = "";}

}

public void FFNewton (System.Windows.Forms.Label lbl, double X)

{

/ / Перша формула Ньютона

lbl.Text = Convert.ToString (/ * masY [0] * / masY [GetX0 (X)]);

if (deltamas [0] [GetX0 (X)]> = 0) lbl.Text + = "+";

for (int i = 0; i <N; i + +)

{

try

{

lbl.Text + = Convert.ToString (Math.Round (deltamas [i] [GetX0 (X)], 4)) + "* q";

} Catch {}
for (int j = 1; j <i +1; j + +)

{

lbl.Text + = "* (q-" + Convert.ToString (j) + ")";

}

lbl.Text + = "/" + Fact (i +1). ToString ();

try

{

if ((i <N - 1) && (deltamas [i + 1] [GetX0 (X)]> = 0))

lbl.Text + = "+";

} Catch {}

}

lbl.Text + = ", де q =" + Convert.ToString (1 / Shag) + "(x";

Додаток 1 (продовження 4)
if (masX [/ * Ntable - 1 * / GetX0 (X)] <0) lbl.Text + = "+"; else lbl.Text + = "-";

lbl.Text + = Convert.ToString (Math.Abs ​​(masX [/ * 0 * / GetX0 (X)])) + ")";

lbls.Add ("Перша формула Ньютона: P =" + lbl.Text);

}
public double EvalSFNewton (double x)

{

double res, res2 = 1.0;

/ / Double q = (1 / Shag) * (x - masX [Ntable - 1]);

double q = (1 / Shag) * (x - masX [GetXn (x)]);

res = masY [GetXn (x)] ;/ / masY [Ntable - 1];

for (int i = 0; i <N; i + +)

{

res2 * = (q - i) / (i + 1) ;/ / Fact (i + 1);

try

{

res + = deltamas [i] [GetXn (x) - i - 1] * res2;

}

catch {}

/ / Res + = deltamas [i] [Ntable - i - 2] * res2;

}

return res;

}

public void EvalSFNewton (double x, System.Windows.Forms.Label lbl)

{

bool b = false;

if (DeltaCount ())

{

double res, res2 = 1.0;

/ / Double q = (1 / Shag) * (x - masX [Ntable - 1]);

double q = (1 / Shag) * (x - masX [GetXn (x)]);

res = masY [GetXn (x)] ;/ / masY [Ntable - 1];

for (int i = 0; i <N; i + +)

{

res2 * = (q - i) / (i +1) ;/ / Fact (i + 1);

try

{

res + = deltamas [i] [GetXn (x)-i-1] * res2;

}

catch {lbl.Text = ""; bSN = false; MessageBox.Show ("невоможно порахувати за другий формулою!"); b = true; break;}

/ / Res + = deltamas [i] [Ntable - i - 2] * res2;

}

if (! b)

{

if (! Y.Contains ("Друга формула Ньютона:" + res.ToString ()))

Y.Add ("Друга формула Ньютона:" + res.ToString ());

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + res.ToString () / * EvalSFNewton (x). ToString () * / + "");

lbl.Text + = "=" + q.ToString ();

}

}

else {lbl.Text = ""; MessageBox.Show ("");}

}

public void SFNewton (System.Windows.Forms.Label lbl, double x)

{

/ / Друга формула Ньютона

lbl.Text = Convert.ToString (masY [/ * Ntable-1 * / GetXn (x)]);

if (deltamas [0] [Ntable-2]> 0) lbl.Text + = "+";

for (int i = 0; i <N; i + +)

{

Додаток 1 (продовження 5)
lbl.Text + = Convert.ToString (Math.Round (deltamas [i] [Ntable-i-2], 4)) + "* q";
for (int j = 1; j <i + 1; j + +)

{

lbl.Text + = "* (q-" + Convert.ToString (j) + ")";

}

lbl.Text + = "/" + Convert.ToString (i + 1) + "!";

if ((i <N - 1) && (deltamas [i + 1] [0]> = 0))

lbl.Text + = "+";

}

lbl.Text + = ", де q =" + Convert.ToString (1 / Shag) + "(x";

if (masX [/ * Ntable - 1 * / GetXn (x)] <0) lbl.Text + = "+"; else lbl.Text + = "-";

lbl.Text + = Convert.ToString (Math.Abs ​​(masX [/ * Ntable - 1 * / GetXn (x)])) + ")";

lbls.Add ("Друга формула Ньютона: P =" + lbl.Text);

}
public double EvalFFGauss (double x)

{

double res, res2 = 1.0;
double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = masY [GetX0 (x)];
/ / Double q = (1 / Shag) * (x - masX [(masX.Length - 1) / 2]);

/ / Res = masY [(int) (masY.Length - 1) / 2];

int count = 1;

int count2 = (masY.Length - 1) / 2;

int count3 = 0;

int ch = 0;

for (int i = 0; i <N; i + +)

{

if (count% 2 == 0) count2 -;
if (i% 2! = 0) count3 + +;

ch = (int) Math.Pow (-1, i) * count3;

res2 * = (q + ch) / Fact (i + 1);;

res + = deltamas [i] [count2] * res2;

count + +;

}

return res;

}

public void EvalFFGauss (double x, System.Windows.Forms.Label lbl)

{

if (DeltaCount ())

{

double res, res2 = 1.0;
double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = masY [GetX0 (x)];

int count = 1;

int count2 = (masY.Length - 1) / 2;

int count3 = 0;

int ch = 0;

for (int i = 0; i <N; i + +)

{

if (count% 2 == 0) count2 -;
if (i% 2! = 0) count3 + +;

ch = (int) Math.Pow (-1, i) * count3;

res2 * = (q + ch) / Fact (i + 1);;

res + = deltamas [i] [count2] * res2;

Додаток 1 (продовження 6)
count + +;

}

if (! Y.Contains ("Перша формула Гауса:" + res.ToString ()))

Y.Add ("Перша формула Гауса:" + res.ToString ());

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + / * EvalFFGauss (x). ToString () * / res.ToString () + "");

lbl.Text + = "=" + q.ToString ();

}

else {lbl.Text = ""; MessageBox.Show ("");}

}

public void FFGauss (System.Windows.Forms.Label lbl, double x)

{

/ / Перша формула Гауса

lbl.Text = Convert.ToString (masY [/ * (int) (masY.Length-1) / 2 * / GetX0 (x)]);

int count = 1;

int count2 = (masY.Length-1) / 2;

int count3 = 0;
for (int i = 0; i <N; i + +)

{

if (count% 2 == 0) count2 -;

if (deltamas [i] [count2]> = 0) lbl.Text + = "+";

int ch = 0;

count3 = 0;

lbl.Text + = Math.Round (deltamas [i] [count2], 4) + "* q";

for (int j = 0; j <i; j + +)

{

if (j% 2 == 0) count3 + +;

ch = (int) Math.Pow (-1, j-1) * count3;

lbl.Text + = "* (q";

if (ch> = 0) lbl.Text + = "+";

lbl.Text + = Convert.ToString (ch) + ")";

}

lbl.Text + = "/" + Convert.ToString (Fact (i + 1));

count + +;

}

lbl.Text + = ", де q =" + Convert.ToString (1 / Shag) + "(x";

if (masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)] <= 0) lbl.Text + = "+" + Math.Abs ​​(masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)]);

else lbl.Text + = "-" + Math.Abs ​​(masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)]);

lbl.Text + = ")";

lbls.Add ("Перша формула Гауса: P =" + lbl.Text);

}
public double EvalSFGauss (double x)

{

double res, res2 = 1.0;

res = masY [GetXn (x)];

int count = 0;

int count2 = (masY.Length - 1) / 2;

int count3 = 0;

int ch = 0;

double q = (1 / Shag) * (x - masX [GetXn (x)]);

for (int i = 0; i <N; i + +)

{

if (count% 2 == 0) count2 -;

if (i% 2! = 0) count3 + +;

ch = (int) Math.Pow (-1, i) * count3;

res2 * = (q - ch) / (i + 1) ;/ / Fact (i + 1);

res + = deltamas [i] [count2] * res2;

count + +;

Додаток 1 (продовження 7)
}

return res;

}

public void EvalSFGauss (double x, System.Windows.Forms.Label lbl)

{

if (DeltaCount ())

{

double res, res2 = 1.0;

/ / Res = masY [(int) (masY.Length - 1) / 2];

res = masY [GetXn (x)];

int count = 0;

int count2 = (masY.Length - 1) / 2;

int count3 = 0;

int ch = 0;

/ / Double q = (1 / Shag) * (x - masX [(masX.Length - 1) / 2]);

double q = (1 / Shag) * (x - masX [GetXn (x)]);

for (int i = 0; i <N; i + +)

{

if (count% 2 == 0) count2 -;

if (i% 2! = 0) count3 + +;

ch = (int) Math.Pow (-1, i) * count3;

res2 * = (q - ch) / (i + 1) ;/ / Fact (i + 1);

res + = deltamas [i] [count2] * res2;

count + +;

}

if (! Y.Contains ("Друга формула Гауса:" + res.ToString ()))

Y.Add ("Друга формула Гауса:" + res.ToString ());

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + / * EvalSFGauss (x). ToString () * / res.ToString () + "");

lbl.Text + = "=" + q.ToString ();

}

else {MessageBox.Show (""); lbl.Text = "";}

}

public void SFGauss (System.Windows.Forms.Label lbl, double x)

{

/ / Друга формула Гауса

lbl.Text = Convert.ToString (masY [/ * (int) (masY.Length - 1) / 2 * / GetXn (x)]);

int count = 0;

int count2 = (masY.Length - 1) / 2;

int count3 = 0;
for (int i = 0; i <N; i + +)

{
if (count% 2 == 0) count2 -;

if (deltamas [i] [count2]> = 0) lbl.Text + = "+";

int ch = 0;

count3 = 0;

lbl.Text + = Math.Round (deltamas [i] [count2], 4) + "* q";

for (int j = 0; j <i; j + +)

{

if (j% 2 == 0) count3 + +;

ch = (int) Math.Pow (-1, j - 1) * count3;

lbl.Text + = "* (q";

if (ch> 0) lbl.Text + = "+";

lbl.Text + = Convert.ToString (ch) + ")";

}

lbl.Text + = "/" + Convert.ToString (Fact (i + 1));

count + +;

}

lbl.Text + = ", де q =" + Convert.ToString (1 / Shag) + "(x";

Додаток 1 (продовження 8)
if (masX [/ * (masX.Length - 1) / 2 * / GetXn (x)] <= 0) lbl.Text + = "+" + Math.Abs ​​(masX [/ * (masX.Length - 1) / 2 * / GetXn (x)]);

else lbl.Text + = "-" + Math.Abs ​​(masX [/ * (masX.Length - 1) / 2 * / GetXn (x)]);

lbl.Text + = ")";

lbls.Add ("Друга формула Гауса: P =" + lbl.Text);

}
public double EvalFStirling (double x)

{

int count2 = (masY.Length - 1) / 2;

double res, res2 = 1.0;

double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = masY [GetX0 (x)];
int count3 = 0;

for (int i = 0; i <N; i + +)

{

if (i% 2! = 0)

{
res2 = (deltamas [i] [count2 - 1] / Fact (i + 1)) * q * q;

count2 -;

}

else

{

double c = Math.Round ((deltamas [i] [count2 - 1] + deltamas [i] [count2]) / (2 * Fact (i + 1)), 4);

res2 = c * q;

if (i> 1)

count3 + +;

}

for (int j = 0; j <count3; j + +)

{

if (i> 1)

res2 * = (q * q - (j + 1) * (j + 1));

}

res + = res2;

}

return res;

}

public void EvalFStirling (double x, System.Windows.Forms.Label lbl)

{

if (DeltaCount ())

{

int count2 = (masY.Length - 1) / 2;

double res, res2 = 1.0;

double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = masY [GetX0 (x)];
int count3 = 0;

for (int i = 0; i <N; i + +)

{

if (i% 2! = 0)

{
res2 = (deltamas [i] [count2 - 1] / Fact (i + 1)) * q * q;

count2 -;

}

else

{
Додаток 1 (продовження 9)
double c = Math.Round ((deltamas [i] [count2 - 1] + deltamas [i] [count2]) / (2 * Fact (i + 1)), 4);

res2 = c * q;

if (i> 1)

count3 + +;

}

for (int j = 0; j <count3; j + +)

{

if (i> 1)

res2 * = (q * q - (j + 1) * (j + 1));

}

res + = res2;

}

if (! Y.Contains ("Формула Стірлінга:" + res.ToString ()))

Y.Add ("Формула Стірлінга:" + res.ToString ());

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + / * EvalFStirling (x). ToString () * / res.ToString () + "");

lbl.Text + = "=" + q.ToString ();

}

else {MessageBox.Show (""); lbl.Text = "";}

}

public void FStirling (System.Windows.Forms.Label lbl, double x)

{

/ / Формула Стірлінга

int count2 = (masY.Length - 1) / 2;

lbl.Text = Convert.ToString (masY [/ * (int) (masY.Length - 1) / 2 * / GetX0 (x)]);

int count3 = 0;

for (int i = 0; i <N; i + +)

{

if (i% 2! = 0)

{

if (Math.Round (deltamas [i] [count2 - 1] / Fact (i + 1), 4)> = 0) lbl.Text + = "+";

lbl.Text + = Math.Round (deltamas [i] [count2 - 1] / Fact (i +1), 4) + "* q ^ 2";

count2 -;

}

else

{

double c = Math.Round ((deltamas [i] [count2 - 1] + deltamas [i] [count2]) / (2 * Fact (i + 1)), 4);

if (c> 0) lbl.Text + = "+";

if (c! = 0) lbl.Text + = c + "* q";

if (i> 1)

count3 + +;

}

for (int j = 0; j <count3; j + +)

{

if (i> 1)

lbl.Text + = "* (q ^ 2 -" + Convert.ToString (j +1) + "^ 2)";

}

}

lbl.Text + = ", де q =" + 1 / Shag + "(x";

if (masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)]> 0) lbl.Text + = "-"; else lbl.Text + = "+";

lbl.Text + = masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)] + ")";

lbls.Add ("Формула Стірлінга: P =" + lbl.Text);

}
public double EvalFBessel (double x)

{

int count2 = (masY.Length - 1) / 2;

Додаток 1 (продовження 10)
int count4 = 0;

int count3 = 0;

double res, res2 = 0.0;

double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = (masY [GetX0 (x)] + masY [GetXn (x)]) / 2;


for (int i = 0; i <N; i + +)

{

if (i% 2 == 0)

{

res2 = Math.Round (deltamas [i] [count2], 4);

count2 -;

}

else

{

res2 = (deltamas [i] [count2] + deltamas [i] [count2 + 1]) / 2;

if (i> 1)

count3 + +;

}
count4 = i - 1;

for (int j = 0; j <= i; j + +)

{

if (i> 1)

{

if (i% 2 == 0 && j == 0) res2 * = (q - 0.5) * q * (q - 1);

else if (i% 2! = 0 && j == 0) res2 * = q * (q - 1);

count4 -;

if (count3> 0)

{

res2 * = (q + (j + 1)) * (q - (j + 2));

count4 -;

}

}

else

{

if (i% 2 == 0)

res2 * = (q - 0.5);

else res2 * = q * (q - 1);

j + +;

}

}

res + = res2 / Fact (i + 1);

}

return res;

}

public void EvalFBessel (double x, System.Windows.Forms.Label lbl)

{

if (DeltaCount ())

{

int count2 = (masY.Length - 1) / 2;

int count4 = 0;

int count3 = 0;

double res, res2 = 0.0;
double q = (1 / Shag) * (x - masX [GetX0 (x)]);

res = (masY [GetX0 (x)] + masY [GetXn (x)]) / 2;


for (int i = 0; i <N; i + +)

Додаток 1 (продовження 11)
{

if (i% 2 == 0)

{

res2 = Math.Round (deltamas [i] [count2], 4);

count2 -;

}

else

{

res2 = (deltamas [i] [count2] + deltamas [i] [count2 + 1]) / 2;

if (i> 1)

count3 + +;

}
count4 = i - 1;

for (int j = 0; j <= i; j + +)

{

if (i> 1)

{

if (i% 2 == 0 && j == 0) res2 * = (q - 0.5) * q * (q - 1);

else if (i% 2! = 0 && j == 0) res2 * = q * (q - 1);

count4 -;

if (count3> 0)

{

res2 * = (q + (j + 1)) * (q - (j + 2));

count4 -;

}

}

else

{

if (i% 2 == 0)

res2 * = (q - 0.5);

else res2 * = q * (q - 1);

j + +;

}

}

res + = res2 / Fact (i + 1);

}

if (! Y.Contains ("Формула Бесселя:" + res))

Y.Add ("Формула Бесселя:" + res);

lbl.Text = lbl.Text.Insert (lbl.Text.IndexOf (", де"), "=" + / * EvalFBessel (x). ToString () * / res.ToString () + "");

lbl.Text + = "=" + q.ToString ();

}

else {MessageBox.Show (""); lbl.Text = "";}

}

public void FBessel (System.Windows.Forms.Label lbl, double x)

{

/ / Формула Бесселя

int count2 = (masY.Length - 1) / 2;

int count4 = 0;

int count3 = 0;

lbl.Text = Convert.ToString (masY [/ * (int) (masY.Length - 1) / 2 * / GetX0 (x)]) + "+" + masY [/ * (int) (masY.Length - 1 ) / 2 +1 * / GetXn (x)] + "/ 2";

for (int i = 0; i <N; i + +)

{

if (i% 2 == 0)

{

if (Math.Round (deltamas [i] [count2], 4)> = 0) lbl.Text + = "+";

lbl.Text + = Math.Round (deltamas [i] [count2], 4);

count2 -;

}

Додаток 1 (продовження 12)
else

{

lbl.Text + = "+" + "((" + Convert.ToString (deltamas [i] [count2]) + "+" + Convert.ToString (deltamas [i] [count2 +1]) + ") / 2 ) ";/ / c;

if (i> 1)

count3 + +;

}
count4 = i-1;

for (int j = 0; j <= i; j + +)

{

if (i> 1)

{

if (i% 2 == 0 && j == 0) lbl.Text + = "(q-0, 5) q (q-1)";

else if (i% 2! = 0 && j == 0) lbl.Text + = "q (q-1)";

count4 -;

if (count3> 0)

{

lbl.Text + = "(q +" + Convert.ToString (j + 1) + ") (q-" + Convert.ToString (j + 2) + ")";

count4 -;

}

}

else

{

if (i% 2 == 0)

lbl.Text + = "(q-0, 5)";

else lbl.Text + = "q (q-1)";

j + +;

}

}

lbl.Text + = "/" + Fact (i +1);

}

lbl.Text + = ", де q =" + 1 / Shag + "(x";

if (masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)]> 0) lbl.Text + = "-"; else lbl.Text + = "+";

lbl.Text + = masX [/ * (masX.Length - 1) / 2 * / GetX0 (x)] + ")";

lbls.Add ("Формула Бесселя: P =" + lbl.Text);

}
public int GetX0 (double X)

{

int num = 0;

for (int i = 0; i <Ntable; i + +)

{

if (X <masX [i]) {if (i> 0) {num = i - 1; break;} else return 0;}

}

if (X> masX [Ntable - 1]) return (Ntable - 1);

return num;

}

public int GetXn (double X)

{

int num = 0;

for (int i = 0; i <Ntable; i + +)

{

if (X <masX [i]) {num = i; break;}

}

if (X> masX [Ntable - 1]) return (Ntable - 1);

return num;

}

Додаток 1 (продовження 13)
public void PaintPlot ()

{

Sort ();

for (int i = 0; i <Ntable; i + +)

{

xlSheet.Cells [i +1,1] = masX [i];

xlSheet.Cells [i +1,2] = masY [i];

}
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo ("EN-US");

Range r;

object [] args = new object [1];
int ii = 0;

/ / Double y = 0.0;

while (ii <Ntable)

{

xlSheet.Cells [ii + 1, 2 * countgraphics + 1] = masX [ii];

xlSheet.Cells [ii + 1, 2 * countgraphics + 2] = masY [ii];

ii + +;

}

Series stable;

stable = sC.NewSeries ();

stable.XValues ​​= xlSheet.get_Range ((string) (((char) (2 * countgraphics + 65)). ToString () + "1"), (string) (((char) (2 * countgraphics + 65)) . ToString () + Convert.ToString (Ntable + 1)));

r = xlSheet.get_Range ((string) (((char) (2 * countgraphics + 66)). ToString () + "1"), (string) (((char) (2 * countgraphics + 66)). ToString () + Convert.ToString (Ntable + 1)));

args [0] = r;

stable.GetType (). InvokeMember ("Values", System.Reflection.BindingFlags.SetField | System.Reflection.BindingFlags.SetProperty, null, stable, args, ci);

stable.Name = "Таблиця";
}
public void Save (String Filename, double x)

{

FileStream fs = new FileStream (Filename, FileMode.Create);

StreamWriter sw = new StreamWriter (fs);

sw.WriteLine ("n =" + N.ToString ());

sw.WriteLine ("Розмір таблицї =" + Ntable.ToString ());

sw.WriteLine ("h =" + Shag.ToString ());

sw.WriteLine ("x =" + x.ToString ());

sw.WriteLine ("Таблиця");

sw.WriteLine ("x \ ty \ t");

for (int i = 0; i <Ntable; i + +)

{

sw.WriteLine (masX [i]. ToString () + "\ t" + masY [i]. ToString ());

}
for (int i = 0; i <Y.Count; i + +)

{

sw.WriteLine (Y [i]);

}

for (int i = 0; i <lbls.Count; i + +)

{

sw.WriteLine (lbls [i]);

}

Додаток 1 (продовження 14)
if (deltamas! = null)

{

sw.WriteLine ("Таблиця різниць");

for (int i = 0; i <N; i + +)

if (i! = 0)

sw.Write ("d" + (i + 1). ToString () + "y \ t");

else sw.Write ("dy \ t");

sw.WriteLine ();
for (int i = 0; i <N; i + +)

{

for (int j = 0; j <Ntable - i; j + +)

{

sw.Write (deltamas [i] [j]. ToString () + "\ t");

}

sw.WriteLine ();

}

}

if (F! = null)

{

sw.WriteLine ("Таблиця різниць для формули Ньютона (нерівні Вузли)");

for (int i = 0; i <N; i + +)

if (i! = 0)

sw.Write ("d" + (i + 1). ToString () + "y \ t");

else sw.Write ("dy \ t");

sw.WriteLine ();
for (int i = 0; i <N; i + +)

{

for (int j = 0; j <Ntable - i; j + +)

{

sw.Write (F [i] [j]. ToString () + "\ t");

}

sw.WriteLine ();

}

}

sw.Flush ();

sw.Close ();

fs.Close ();
}
public void PaintPlot (double x, double y, bool b, string str)

{

countgraphics + +;

if (str == "Перша Інтерполяційна формула Ньютона") {if (! bFN) {goto Finish;}}

if (str == "Друга Інтерполяційна формула Ньютона") {if (! bSN) {goto Finish;}}

Sort ();


for (int i = 0; i <Ntable; i + +)

{

xlSheet.Cells [i + 1, 1] = masX [i];

xlSheet.Cells [i + 1, 2] = masY [i];

}
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo ("EN-US");

Range r;

object [] args = new object [1];

Додаток 1 (продовження 15)
xlSheet.Cells [1, 2 * countgraphics + 1] = x;

xlSheet.Cells [1, 2 * countgraphics + 2] = y;
Series stable;

stable = sC.NewSeries ();

stable.XValues ​​= xlSheet.get_Range ((string) (((char) (2 * countgraphics + 65)). ToString () + "1"), (string) (((char) (2 * countgraphics + 65)) . ToString () + "1"));

r = xlSheet.get_Range ((string) (((char) (2 * countgraphics + 66)). ToString () + "1"), (string) (((char) (2 * countgraphics + 66)). ToString () + "1"));

args [0] = r;

stable.GetType (). InvokeMember ("Values", System.Reflection.BindingFlags.SetField | System.Reflection.BindingFlags.SetProperty, null, stable, args, ci);

if (b)

{

stable.Name = str + "(найкращий вибір)";

}

else

{

stable.Name = str;

}

Finish: bFN = true;

/ /

}

public int WhereIsX (double x)

{

int ii = 0;

int where = 0;

for (int i = 0; i <masX.Count (); i + +)

{

if (x <masX [i]) {ii = i; break;} else {if (x> masX [masX.Count () - 1]) {ii = masX.Count () - 1; break;}}

}

if (ii <= (double) (masX.Count () / 4)) where = 0;

else

if (ii> masX.Count () && ii <(double) (masX.Count () / 2))

where = 1;

else

if (ii> (double) (masX.Count () / 2) && ii <(double) (3 * masX.Count () / 4))

where = 2;

else where = 3;

return where;

}

public void Export ()

{

String strpath = System.Windows.Forms.Application.ExecutablePath;

strpath + = DateTime.Now.ToString (). Replace (":", "_") + ". bmp";
chartPage.Export (strpath, "GIF", misValue);

p.Image = new Bitmap (strpath);

}

public double CountY (int num, double x)

{

switch (num)

{

case 0: return EvalFFNewton (x);

case 1: return EvalSFNewton (x);

case 2: return EvalFFGauss (x);

case 3: return EvalSFGauss (x);

case 4: return EvalFStirling (x);

Додаток 1 (продовження 16)
case 5: return EvalFBessel (x);

case 6: return EvalNewtonNerivni (x);

default: return 0.0;

}

}

public void Sort ()

{

bool b = true;

while (b)

{

b = false;

for (int i = 0; i <Ntable - 1; i + +)

{

if (masX [i]> masX [i + 1])

{

double temp = masX [i];

masX [i] = masX [i + 1];

masX [i + 1] = temp;

temp = masY [i];

masY [i] = masY [i + 1];

masY [i + 1] = temp;

b = true;

}

}

}

}

}

}

namespace InterPolation

{

public partial class Form1: Form

{

public static double shag = 0.0;

public static int n = 0;

public int NNerivni = 0;

public int ntable = 0;

public InterPolationClass inter;

public double [] masX0, masY0;

public double X;

public List <Variant> v = new List <Variant> ();

public Form1 ()

{

InitializeComponent ();

}

private void Form1_Load (object sender, EventArgs e)

{

dataGridView1.Columns.Add ("X", "X");

dataGridView1.Columns.Add ("Y", "Y");

string xml;

FileStream fs = new FileStream ("data.xml", FileMode.Open);

StreamReader sr = new StreamReader (fs);

xml = sr.ReadToEnd ();

v = SerializationUtil.DeserializeObject <Variant> (Encoding.UTF8, xml). ToList <Variant> ();

for (int i = 0; i <v.Count; i + +)

{

comboBox1.Items.Add ((i +1). ToString ());

}

}

private void button1_Click (object sender, EventArgs e)

{

Додаток 1 (продовження 17)

if (textBox1.Text! = "")

{

n = Convert.ToInt32 (textBox1.Text);

ntable = 2 * n - 1;

try

{

if (textBox2.Text! = "") shag = Convert.ToDouble (textBox2.Text);

}

catch

{

MessageBox.Show ("Неправильний формат вводу");

}

}

else {MessageBox.Show ("Усі поля дожни бути заповнені");}
if (tb.Text! = "")

{

NNerivni = Convert.ToInt32 (tb.Text);

ntable = NNerivni;

}

if (tb.Text! = "" && ntable> 0)

{

dataGridView1.Rows.Clear ();
for (int i = 0; i <ntable; i + +)

{

dataGridView1.Rows.Add ();

}

dataGridView1.AutoSize = true;

}

}

private void button2_Click (object sender, EventArgs e)

{

lblFB.Text = "";

lblFFG.Text = "";

lblFFN.Text = "";

lblFS.Text = "";

lblSFG.Text = "";

lblSFN.Text = "";

label5.Text = "";
X = Convert.ToDouble (textBox3.Text);

if ((2 * n - 1) <= ntable)

{

masX0 = new double [ntable];

masY0 = new double [ntable];
inter = new InterPolationClass (shag, ntable, n, NNerivni, masX0, masY0, pictureBox1);

for (int i = 0; i <ntable; i + +)

{

masX0 [i] = Convert.ToDouble (dataGridView1 [0, i]. Value);

masY0 [i] = Convert.ToDouble (dataGridView1 [1, i]. Value);

}
inter.DeltaCount ();
if (checkedListBox1.GetItemChecked (0))

{

Додаток 1 (продовження 18)

inter.FFNewton (lblFFN, X);

}

if (checkedListBox1.GetItemChecked (1))

{

inter.SFNewton (lblSFN, X);

}

if (checkedListBox1.GetItemChecked (2))

{

inter.FFGauss (lblFFG, X);

}

if (checkedListBox1.GetItemChecked (3))

{

inter.SFGauss (lblSFG, X);

}

if (checkedListBox1.GetItemChecked (4))

{

inter.FStirling (lblFS, X);

}

if (checkedListBox1.GetItemChecked (5))

{

inter.FBessel (lblFB, X);

}

if (checkedListBox1.GetItemChecked (6))

{

}

inter.PaintPlot ();


int best = 0;

double q = Math.Abs ​​((1 / shag) * (X - masX0 [(masX0.Length - 1) / 2]));

int where = inter.WhereIsX (X);
if (where == 0) {best = 0;}

else if (where == 4) {best = 2;}

else if ((where == 3 | | where == 4) && q <= 0.25) {best = 4;}

else if ((where == 3 | | where == 4) && q> 0.25 && q <= 0.5) {best = 5;}
if (where == 3 && q> 0.5) {best = 2;}

if (where == 4 && q> 0.5) {best = 3;}
for (int i = 1; i <masX0.Count () - 1; i + +)

{

if (Math.Round (masX0 [i] - masX0 [i - 1], 5)! = Math.Round (shag, 5))

{Best = 6;}

}
if (! checkedListBox1.GetItemChecked (best)) {checkedListBox1.SetItemChecked (best, true);}
if (checkedListBox1.GetItemChecked (0))

{

if (0 == best)

inter.PaintPlot (X, inter.EvalFFNewton (X), true, checkedListBox1.Items [0]. ToString ());

else inter.PaintPlot (X, inter.EvalFFNewton (X), false, checkedListBox1.Items [0]. ToString ());

if (lblFFN.Text == "-")

Додаток 1 (продовження 19)
inter.FFNewton (lblFFN, X);

inter.EvalFFNewton (X, lblFFN);

}

if (checkedListBox1.GetItemChecked (1))

{

if (1 == best)

inter.PaintPlot (X, inter.EvalSFNewton (X), true, checkedListBox1.Items [1]. ToString ());

else inter.PaintPlot (X, inter.EvalSFNewton (X), false, checkedListBox1.Items [1]. ToString ());

if (lblSFN.Text == "-")

inter.SFNewton (lblSFN, X);

inter.EvalSFNewton (X, lblSFN);

}

if (checkedListBox1.GetItemChecked (2))

{

if (2 == best)

inter.PaintPlot (X, inter.EvalFFGauss (X), true, checkedListBox1.Items [2]. ToString ());

else inter.PaintPlot (X, inter.EvalFFGauss (X), false, checkedListBox1.Items [2]. ToString ());

if (lblFFG.Text == "-")

inter.FFGauss (lblFFG, X);

inter.EvalFFGauss (X, lblFFG);

}

if (checkedListBox1.GetItemChecked (3))

{

if (3 == best)

inter.PaintPlot (X, inter.EvalSFGauss (X), true, checkedListBox1.Items [3]. ToString ());

else inter.PaintPlot (X, inter.EvalSFGauss (X), false, checkedListBox1.Items [3]. ToString ());

if (lblSFG.Text == "-")

inter.SFGauss (lblSFG, X);

inter.EvalSFGauss (X, lblSFG);

}

if (checkedListBox1.GetItemChecked (4))

{

if (4 == best)

inter.PaintPlot (X, inter.EvalFStirling (X), true, checkedListBox1.Items [4]. ToString ());

else inter.PaintPlot (X, inter.EvalFStirling (X), false, checkedListBox1.Items [4]. ToString ());

if (lblFS.Text == "-")

inter.FStirling (lblFS, X);

inter.EvalFStirling (X, lblFS);

}

if (checkedListBox1.GetItemChecked (5))

{

if (5 == best)

inter.PaintPlot (X, inter.EvalFBessel (X), true, checkedListBox1.Items [5]. ToString ());

else inter.PaintPlot (X, inter.EvalFBessel (X), false, checkedListBox1.Items [5]. ToString ());

if (lblFB.Text == "-")

inter.FBessel (lblFB, X);

inter.EvalFBessel (X, lblFB);

}

if (checkedListBox1.GetItemChecked (6))

{

if (6 == best)

Додаток 1 (продовження 20)
inter.PaintPlot (X, inter.EvalNewtonNerivni (X), true, checkedListBox1.Items [6]. ToString ());

else inter.PaintPlot (X, inter.EvalNewtonNerivni (X), false, checkedListBox1.Items [6]. ToString ());
label5.Text + = "=" + inter.EvalNewtonNerivni (X). ToString ();

}

inter.Export ();

}

else MessageBox.Show ("введення масиву НЕ й достатньо для розрахунку");

}

private void button3_Click (object sender, EventArgs e)

{

for (int i = 0; i <ntable; i + +)

{

dataGridView1 [0, i]. Value = "";

dataGridView1 [1, i]. Value = "";
}

tb.Text = "";

textBox1.Text = "";

textBox2.Text = "";

textBox3.Text = "";

pictureBox1.Image = null;

lblFB.Text = "";

lblFFG.Text = "";

lblFFN.Text = "";

lblFS.Text = "";

lblSFG.Text = "";

lblSFN.Text = "";

}

private void віхідToolStripMenuItem_Click (object sender, EventArgs e)

{

Application.Exit ();

}

private void tsSave_Click (object sender, EventArgs e)

{

if (saveFileDialog1.ShowDialog () == DialogResult.OK)

{

inter.Save (saveFileDialog1.FileName, X);

}

}

private void tsTest_Click (object sender, EventArgs e)

{

for (int i = 0; i <7; i + +)

checkedListBox1.SetItemChecked (i, true);

shag = 0.05;

ntable = 7;

n = 3;

masX0 = new double [ntable];

masY0 = new double [ntable];

masX0 [0] = 1.5;

masX0 [1] = 1.55;

masX0 [2] = 1.6;

masX0 [3] = 1.65;

masX0 [4] = 1.7;

masX0 [5] = 1.75;

Додаток 1 (продовження 21)
masX0 [6] = 1.8;

masY0 [0] = 15.132;

masY0 [1] = 17.422;

masY0 [2] = 20.393;

masY0 [3] = 23.994;

masY0 [4] = 28.16;

masY0 [5] = 32.812;

masY0 [6] = 37.857;

X = 1.606;

textBox1.Text = n.ToString ();

textBox2.Text = shag.ToString ();

textBox3.Text = X.ToString ();
dataGridView1.Rows.Clear ();
for (int i = 0; i <ntable; i + +)

{

dataGridView1.Rows.Add ();

}

dataGridView1.AutoSize = true;
inter = new InterPolationClass (shag, ntable, n, NNerivni, masX0, masY0, pictureBox1);

for (int i = 0; i <ntable; i + +)

{

dataGridView1 [0, i]. Value = masX0 [i];

dataGridView1 [1, i]. Value = masY0 [i];

}
inter.DeltaCount ();
inter.FFNewton (lblFFN, X);

inter.SFNewton (lblSFN, X);

inter.FFGauss (lblFFG, X);

inter.SFGauss (lblSFG, X);

inter.FStirling (lblFS, X);

inter.FBessel (lblFB, X);
inter.EvalFBessel (X, lblFB);

inter.EvalFFGauss (X, lblFFG);

inter.EvalFFNewton (X, lblFFN);

inter.EvalFStirling (X, lblFS);

inter.EvalSFGauss (X, lblSFG);

inter.EvalSFNewton (X, lblSFN);

inter.PaintPlot ();
int best = 0;

double q = Math.Abs ​​((1 / shag) * (X - masX0 [(masX0.Length - 1) / 2]));

int where = inter.WhereIsX (X);
if (where == 0) {best = 0;}

else if (where == 4) {best = 2;}

else if ((where == 3 | | where == 4) && q <= 0.25) {best = 4;}

else if ((where == 3 | | where == 4) && q> 0.25 && q <= 0.5) {best = 5;}
if (where == 3 && q> 0.5) {best = 2;}

if (where == 4 && q> 0.5) {best = 3;}
for (int i = 1; i <masX0.Count () - 1; i + +)

Додаток 1 (продовження 22)
{

if (Math.Round (masX0 [i] - masX0 [i - 1], 5)! = Math.Round (shag, 5))

{Best = 6;}

}


if (0 == best)

inter.PaintPlot (X, inter.EvalFFNewton (X), true, checkedListBox1.Items [0]. ToString ());

else inter.PaintPlot (X, inter.EvalFFNewton (X), false, checkedListBox1.Items [0]. ToString ());

if (1 == best)

inter.PaintPlot (X, inter.EvalSFNewton (X), true, checkedListBox1.Items [1]. ToString ());

else inter.PaintPlot (X, inter.EvalSFNewton (X), false, checkedListBox1.Items [1]. ToString ());

if (2 == best)

inter.PaintPlot (X, inter.EvalFFGauss (X), true, checkedListBox1.Items [2]. ToString ());

else inter.PaintPlot (X, inter.EvalFFGauss (X), false, checkedListBox1.Items [2]. ToString ());

if (3 == best)

inter.PaintPlot (X, inter.EvalSFGauss (X), true, checkedListBox1.Items [3]. ToString ());

else inter.PaintPlot (X, inter.EvalSFGauss (X), false, checkedListBox1.Items [3]. ToString ());
if (4 == best)

inter.PaintPlot (X, inter.EvalFStirling (X), true, checkedListBox1.Items [4]. ToString ());

else inter.PaintPlot (X, inter.EvalFStirling (X), false, checkedListBox1.Items [4]. ToString ());

if (5 == best)

inter.PaintPlot (X, inter.EvalFBessel (X), true, checkedListBox1.Items [5]. ToString ());

else inter.PaintPlot (X, inter.EvalFBessel (X), false, checkedListBox1.Items [5]. ToString ());
if (6 == best)

inter.PaintPlot (X, inter.EvalNewtonNerivni (X), true, checkedListBox1.Items [6]. ToString ());

else inter.PaintPlot (X, inter.EvalNewtonNerivni (X), false, checkedListBox1.Items [6]. ToString ());
label5.Text + = "=" + inter.EvalNewtonNerivni (X). ToString ();
inter.Export ();
}

private void comboBox1_SelectedIndexChanged (object sender, EventArgs e)

{

int sel = comboBox1.SelectedIndex;

ntable = v [sel]. Ntable;

dataGridView1.Rows.Clear ();

for (int i = 0; i <ntable; i + +)

{

dataGridView1.Rows.Add ();

}

dataGridView1.AutoSize = true;

Додаток 1 (продовження 23)
for (int i = 0; i <ntable; i + +)

{

dataGridView1 [0, i]. Value = v [sel]. X [i];

dataGridView1 [1, i]. Value = v [sel]. Y [i];

}

shag = v [sel]. Shag;

textBox2.Text = shag.ToString ();

tb.Text = "";

}


}

}
Навчальний матеріал
© uadoc.zavantag.com
При копіюванні вкажіть посилання.
звернутися до адміністрації