Добавил:
Eatmore
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:написанные программы / метод прогноза и коррекции / ChZ3
.CPP#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#define fun cos (x) - sin (x) - y
#define prec 0.00001
#define xm 0.0
#define ym 2.0
#define h 0.5
#define n 20
double func(double x)
{
return cos(x)+exp(-x);
}
double f(double x,double y)
{
return fun;
}
double Eiler ()
{
return
(ym+h*f(xm+h/2,ym+h/2*f(xm, ym)));
}
double *function ()
{
double *mas, a;
mas =(double*)malloc(sizeof (double) *n);
mas[0] =ym;mas[1] =Eiler ();
for (int i=2;i<n;i++)
{
a=mas[i-2]+2.0*h*f(xm+h*(i-1),mas[i-1]);
mas[i]=mas[i-1] + h * 0.5 * (f (xm+h*(i-1),mas[i-1]) + f (xm+h*i,a));
while (fabs(a-mas[i])>prec)
{
a=mas[i];
mas[i]=mas[i-1] +
h*0.5*(f(xm+h*(i-1),mas[i-1])+f(xm+h*i,a));
}
}
return mas;
}
void main()
{
clrscr();
double *mas=function ();
cout<<"----Xm---|-----Ym-----|-----Y(p&k)-----|"<<endl;
for (int i=0;i<n;i++)
cout <<" "<< xm + h * i <<" " << func (xm + h * i) <<" " <<mas[i]<<endl;
delete mas;
getch ();
}