Программная реализация метода дифференцирования по методу производных

#pragma once

#include <math.h>

#include <fstream>

#include "iostream"

using namespace std;

int metod=1;

double xm=5.5, ym=20;

const double pi=3.1415926535897932384626433832795;

double a=0, b=pi, h, X=pi/4, rez;

int i,j,k;

double mass1[4][9],Sravn[4][9], r[4][9], o[4][9];

double tochn, etalon;

double func(double X)

{

return double (-sin(X));

};

//методы дифференцирования

namespace kurs {

double defmid()

{

return double((func(X+h)-func(X-h))/(2*h)); };

double defleft()

{

return double((func(X)-func(X-h))/h);};

double defright()

{

return double((func(X+h)-func(X))/h);};

double (*El[6])()={defleft,defmid,defright};

void massPrep(void)

{

for ( j=0; j<4; j++)

for( i=0;i<9;i++)

{

mass1[j][i]=0;

o[j][i]=0;

r[j][i]=0;

Sravn[j][i]=0;

}

h =(b-a)/2;

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

{ rez=El[metod]();

mass1[0][j]=rez;

h=h/2;

}

}

void FillExtr(void)

{

double razn,otn;

for (i=1; i<4; i++)

{

j=i*2;

for (k=j-1;k<9;k++)

{ if (k-2>-1)

razn=mass1[i-1][k-1]-mass1[i-1][k-2];

else razn=mass1[i-1][k-1];

r[i-1][k]=razn; };

for (k=j+1;k<9;k++)

{otn=r[i-1][k-1]/r[i-1][k];

o[i-1][k]=otn; }

for (k=j;k<9;k++)

{

razn=r[i-1][k+1];

otn=o[i-1][k+1]-1;

mass1[i][k]=mass1[i-1][k]+razn/otn;

}

}

};

void FillTochn(void)

{

tochn=mass1[3][7];

for (i=0; i<8; i++)

{if (i/2+1>4) k=4; else k=i/2+1;

for (j=0; j<k; j++)

Sravn[j][i]=-log10(fabs(tochn - mass1[j][i]));;

}

};

void FillEtalon(void)

{

if (metod<3) etalon=func(X+pi/2);

else etalon=-func(b+pi/2)+func(a+pi/2);

tochn=mass1[3][7];

for (i=0; i<8; i++)

{if (i/2+1>4) k=4; else k=i/2+1;

for (j=0; j<k; j++)

Sravn[j][i]=-log10(fabs(etalon - mass1[j][i]));

}

};

void FillRunge(void)

{

for (i=0; i<9; i++)

{if (i/2+1>4) k=4; else k=i/2+1;

for (j=0; j<k; j++)

if (j>0) Sravn[j][i]=-log10(fabs(mass1[j-1][i] - mass1[j][i]));

else Sravn[j][i]=-log10(fabs(mass1[j][i]));

}

};

//отрисовка графиков

public: void OSI(void)

{

pictureBox1->Refresh();

pictureBox2->Refresh();

pictureBox3->Refresh();

Graphics ^ g=this->pictureBox1->CreateGraphics();

Graphics ^ g1=this->pictureBox2->CreateGraphics();

Graphics ^ g2=this->pictureBox3->CreateGraphics();

Pen^ set = gcnew Pen( Color::Black,2.0f );

g->DrawLine(set,20,0,20,pictureBox1->Height);

g->DrawLine(set,0,pictureBox1->Height-15,pictureBox1->Width,pictureBox1->Height-15);

g1->DrawLine(set,20,0,20,pictureBox1->Height);

g1->DrawLine(set,0,pictureBox1->Height-15,pictureBox1->Width,pictureBox1->Height-15);

g2->DrawLine(set,20,0,20,pictureBox1->Height);

g2->DrawLine(set,0,pictureBox1->Height-15,pictureBox1->Width,pictureBox1->Height-15);

}

public: void DrawBest()

{

FillTochn();

Graphics ^ g=this->pictureBox1->CreateGraphics();

Pen^ pen = gcnew Pen( Color::Red,1.7f );

float x0,x1,y0,y1;

for (j=1;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[0][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[0][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::Green,1.7f );

for (j=3;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[1][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[1][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::DarkBlue,1.7f );

for (j=5;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[2][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[2][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

}

public: void DrawIdeal()

{

FillEtalon();

Graphics ^ g=this->pictureBox2->CreateGraphics();

Pen^ pen = gcnew Pen( Color::Red,1.7f );

float x0,x1,y0,y1;

for (j=1;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[0][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[0][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::Green,1.7f );

for (j=3;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[1][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[1][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::DarkBlue,1.7f );

for (j=5;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[2][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[2][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::DarkViolet,1.7f );

for (j=7;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[3][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[3][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

};

public: void DrawRunge()

{

FillRunge();

Graphics ^ g=this->pictureBox3->CreateGraphics();

Pen^ pen = gcnew Pen( Color::Red,1.7f );

float x0,x1,y0,y1;

for (j=1;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[0][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[0][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::Green,1.7f );

for (j=3;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[1][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[1][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::DarkBlue,1.7f );

for (j=5;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[2][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[2][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

pen = gcnew Pen( Color::DarkViolet,1.7f );

for (j=7;j<8; j++)

{

y0=pictureBox1->Height-15-ym*Sravn[3][j-1];

x0=20+xm*pow(2.,j-1);

y1=pictureBox1->Height-15-ym*Sravn[3][j];

x1=20+xm*pow(2.,j);

g->DrawLine(pen,x0,y0,x1,y1);

}

};

ОЦЕНКА ПОГРЕШНОСТИ РЕЗУЛЬТАТА, УТОЧНЕНИЕ РЕЗУЛЬТАТА, ВИЗУАЛИЗАЦИЯ РЕЗУЛЬТАТОВ УТОЧНЕНИЯ

Наши рекомендации