Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:лабы / 4
.cpp/*#include <iostream>
#include <conio.h>
#include <iomanip>
#include <math.h>
#include <fstream>
using namespace std;
ofstream out("output.txt");
ofstream fout("output.csv");
const int m=3, k=3;
double t(double x)
{
return 1.0;
}
double p(double x)
{
return -3.0;
}
double q(double x)
{
return -2.0;
}
double f(double x)
{
return (pow(x,2)+3*x);
}
//прогонка
double *progonka(int n, double *x)
{
int i;
double bet1=0.0, bet2=0.0, al1=1.0, al2=1.0, G1=1.0, G2=exp(1.0), h, A=0.0, B=1.0;
double *y = new double[n+1];
double *k = new double[n+1];
double *l = new double[n+1];
double *a = new double[n+1];
double *b = new double[n+1];
double *c = new double[n+1];
h=(B - A)/n;
for (i=0;i<n+1;i++)
{
if(i==0)
{
b[i]=bet1/h + bet1*p(x[i])/2.0;
a[i]=al1+b[i]+bet1*q(x[i])*h/2.0;
c[i]=G1-bet1*h*f(x[i])/2.0;
}
else if(i==n)
{
b[i]=bet2/h-bet2*p(x[i])/2.0;
a[i]=al2+b[i]+bet2*h*q(x[i])/2.0;
c[i]=G2-bet2*h*f(x[i])/2.0;
}
else
{
b[i] = 2.0*t(x[i])/(h*h) + q(x[i]);
a[i] = t(x[i])/(h*h) - p(x[i])/(2.0*h);
c[i] = t(x[i])/(h*h) + p(x[i])/(2.0*h);
}
}
for(i=0;i<n+1;i++)
{
if(i==0 || i==n)
{
k[i]=b[i]/a[i];
l[i]=c[i]/a[i];
}
else
{
k[i]=c[i]/(b[i]-a[i]*k[i-1]);
l[i]=(a[i]*l[i-1]-f(x[i]))/(b[i]-a[i]*k[i-1]);
}
}
for (i=n; i>=0; i--)
{
if(i==n)
y[i]=(k[i]*l[i-1]+l[i])/(1.0-k[i]*k[i-1]);
else
y[i]=k[i]*y[i+1]+l[i];
}
return y;
}
//аналит.решение
double *U(int n, double *x)
{ double e=2.718282;
double *u = new double[n+1];
for(int i=0;i<n+1;i++)
u[i]=0.1*pow(e,2*x[i])+pow(e,x[i])+0.5*pow(x[i],2)+3*x[i]+4;
return u;
}
//производная от аналит.решения
double dU(double x)
{ double e=2.718282;
return (0.2*pow(e,2*x)+pow(e,x)+x+3.0);
}
//массив узловых точек
double *X(int n)
{
double h, A=0.0, B=1.0;
double *x = new double[n+1];
h=(B-A)/n;
for(int i=0;i<n+1;i++)
x[i]=A+i*h;
return x;
}
//поиск максимума норм
double max_norm(int n, double *razn)
{
int i;
double norm[m+1], sum, max;
for(i=0;i<m;i++)
norm[i]=0;
sum=0;
for(i=0;i<m;i++)
{
norm[0]+=razn[i];
sum+=pow(razn[i],2);
if(razn[i]>norm[2])
norm[2]=razn[i];
}
norm[0]=norm[0]/n;
norm[1]=pow(sum,0.5)/n;
max=norm[0];
for(i=1;i<m;i++)
if(norm[i]>max)
max=norm[i];
for(i=0;i<m;i++)
out<<setprecision(9)<<setw(12)<<norm[i];
out<<endl;
out<<setprecision(9)<<setw(12)<<"max norm = "<<max<<endl<<endl;
return max;
}
//вывод на экран
void print (int n, int n0, double *x, double *y, double *y2, double *u, double *dif, double *dif2)
{locale loc("Russian_Russia.866");
fout.imbue (loc);
int j=0, i, i1, i2, s;
double h, A=0.0, B=1.0;
double *dy2=new double[n+1];
double *du = new double[n+1];
//fout<<fixed;
fout<<"x"<<";"<<setw(9)<<"y"<<";"<<setw(14)<<"y*"<<";"<<setw(16)<<"|y-y*|"<<";"<<setw(17)<<"|yn-y2n|"<<";"<<setw(9)<<"y'"<<";"<<setw(16)<<"y*'"<<";"<<setw(20)<<"|y'-y*'|"<<";"<<endl;
h=(B-A)/20.0;
s=n/20.0;
for (i=1;i<n;i++)
dy2[i]=(y2[i+s]-y2[i-s])/(2.0*h);
dy2[0]=(-y[2*s]+ 4*y[s]-3*y[0])/(2*h);
dy2[n]=(y[n-2*s]-4*y[n-s]+3*y[n])/(2*h);
i=0;
while(j<21)
{
du[j]=dU(x[j]);
i=j*n/20.0;
i1=j*n/40.0;
i2=j*n0/20.0;
fout<<setprecision(3)<<x[j]<<";"<<setw(13)<<setprecision(8)<<y2[i]<<";"<<setw(13)<<u[i2]<<";"<<setw(13)<<dif2[i2]<<";"<<setprecision(8)<<setw(15)<<dif[i1]<<";"<<setprecision(8)<<setw(15)<<du[j]<<";";
// if(i==0 || i==n)
// out<<endl;
//else
out<<setprecision(8)<<setw(15)<<dy2[i]<<setw(15)<<fabs(du[j]-dy2[i])<<endl;
j++;i++;
}
}
//основная часть
int main()
{
int n=40, i, j, j1, n0=20;
double max, eps[k], max2;
eps[0]=0.00001; eps[1]=0.000001; eps[2]=0.0000001;
for(j=0;j<k;j++)
{
out<<endl;
out<<fixed;
out<<"E"<<(j+1)<<" = "<<setprecision(7)<<eps[j]<<endl<<endl;
do
{
out<<"n0 = "<<n0<<" n = "<<n<<endl<<endl;
double *y = progonka(n0,X(n0));
double *y2 = progonka(n,X(n));
double *u = U(n0,X(n0));
double *dif = new double[n/2];
double *dif2 = new double[n/2];
//out<<"|yn - y2n| :"<<endl<<endl;
//for(i=0;i<n0+1;i++)
// {
// j1=i*2.0;
// dif[i]=abs(y2[j1]-y[i]);
// dif2[i]=abs(y2[j1]-u[i]);
// out<<dif[i]<<endl;
// }
//for(i=0;i<n+1;i++)
// dif2[i]=abs(u[i]-y2[i]);
out<<endl<<endl;
max = max_norm(n0,dif);
if(max<eps[j])
{
out<<"Result :"<<endl<<endl;
out<<"|| yn - y2n || :"<<endl;
max=max_norm(n0,dif);
out<<"|| y* - y2n || :"<<endl;
max2=max_norm(n0,dif2);
print(n,n0,X(20),y,y2,u,dif,dif2);
}
out<<endl<<endl;
if(n0==20) max=1;
n0*=2;
n*=2;
}
while (max>eps[j]);
out<<"_______________________________________________________________________________________________"<<endl;
n=40; n0=20;
}
return 0;
}
*/
/*
#define _USE_MATH_DEFINES
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
ofstream out("output.csv");
ofstream fout("output.txt");
double c1 = 1,c2 = 1,e=2.718282;
double u(double x)
{
return (0.1*pow(e,2*x)+pow(e,x)+0.5*pow(x,2)+3*x+4);
}
double du(double x)
{
return (0.2*pow(e,2*x)+pow(e,x)+x+3.0);
}
double alf = 0,bet = 1;
double bet1 = 1,bet2 = 0,alf2 = 1;
double gam1 = 4.2, gam2 =5.1;
int n = 40;
int const nn = 2*n;
double h = (bet-alf)/n;
double p(double x)
{
return -3.0;
}
double q(double x)
{
return -2.0;
}
double f(double x)
{
return (pow(x,2)+3*x);
}
double a(double x)
{
return 1.0/pow(h,2)-p(x)/(2*h);
}
double b(double x)
{
return 2.0/pow(h,2)+q(x);
}
double c(double x)
{
return 1.0/pow(h,2)+p(x)/(2*h);
}
double d(double x)
{
return f(x);
}
void defineXY(double * x,double * y)
{
for(int i = 0; i < n + 1;i++)
{
x[i]=alf+i*h;
y[i]=u(x[i]);
}
}
void defineLK(double * L,double * K,double * x)
{
double def1 = 1+bet1/h+0.5*bet1*p(alf)+0.5*bet1*h*q(alf);
double def2 = bet1/h+0.5*bet1*p(alf);
double def3 = gam1-0.5*bet1*h*f(alf);
L[1]=def2/def1;
K[1]=def3/def1;
double def;
for(int i = 1;i < n;i++)
{
def = b(x[i])-L[i]*a(x[i]);
K[i+1]=(K[i]*a(x[i])-d(x[i]))/def;
L[i+1]=c(x[i])/def;
}
}
void solution(double * y4,double * L,double * K)
{
double def1 = alf2+bet2/h-0.5*bet2*p(bet)+0.5*bet2*h*q(bet);
double def2 = 0.5*bet2*p(bet)-bet2/h;
double def3 = gam2 - bet2*0.5*h*f(bet);
y4[n]=(def3-K[n]*def2)/(def1+L[n]*def2);
for(int i = n;i > 0; i--)
{
y4[i-1]=y4[i]*L[i]+K[i];
}
}
void printResult(double * x,double * y,double * y4,double * oldfun)
{
int m = n/40;
out<<setw(15)<<"X:"<<";"<<setw(15)<<"U:"<<";"<<setw(15)<<"Y:"<<";"<<setw(15)<<"oldY4:"<<";"<<setw(15)<<"|Y-Y4|"<<";"<<setw(15)<<"|Y-oldFun|"<<";"<<endl;
for(int i = 0; i < 20&&i*m<n+1; i++)
{
out<<setw(15)<<x[i*m*2]<<";"<<setw(15)<<y[i*m*2]<<";"<<setw(15)<<y4[i*m*2]<<";"<<setw(15)<<oldfun[i*m]<<";"<<setw(15)<<abs(y[i*m*2]-y4[i*m*2])<<";"<<setw(15)<<abs(oldfun[i*m]-y4[i*m*2])<<endl;
}
}
void rav(double * a,double * b)
{
for(int i = 0; i < n+1; i++)
{
a[i]=b[i];
}
}
double max(double a,double b,double c)
{
if(a>b)
{if(a>c)
return a;}
else
if(b>c)
return b;
return c;
}
bool check(double * fun,double * oldfun)
{
if(n==nn) return false;
double s1=abs(fun[2]-oldfun[1]),s2=0,s3=0;
for(int i=1;i<n/2;i++)
{
if(abs(fun[i*2]-oldfun[i])>s1)
{
s1=abs(fun[2*i]-oldfun[i]);
}
s2+=abs(fun[2*i]-oldfun[i]);
s3+=pow((fun[2*i]-oldfun[i]),2);
}
s2/=n/2;
s3=sqrt(s3)/n/2;
fout<<setw(16)<<s1<<setw(16)<<s2<<setw(16)<<s3<<endl<<endl;
return max(s1,s2,s3)<0.00001;
}
int main()
{
bool p = false;
double *oldfun =new double[100000];
do
{
n*=2;
fout<<n<<endl;
h=(bet-alf)/n;
double * x = new double [n+1];
double * fun = new double [n+1];
double * y = new double [n+1];
double * L = new double [n+1];
double * K = new double [n+1];
defineXY(x,y);
defineLK(L,K,x);
solution(fun,L,K);
p=check(fun,oldfun);
printResult(x,y,fun,oldfun);
rav(oldfun,fun);
}
while(!p);
return 0;
}
*/
/*
#define _USE_MATH_DEFINES
#include <fstream>
#include <conio.h>
#include <iomanip>
#include <math.h>
#include <fstream>
using namespace std;
ofstream cout("output.txt");
double A=1.0;
double B=2.0;
double U(double x) {
//return 1/(3*x)+x*x/2;
return pow(x,3)+2*x+1/x;
}
double prU(double x) {
return 3*x*x+2-1/(x*x);
}
double pr(double *a, long x,double h) {
return (a[x]-a[x-1])/h;
}
double t(double x)
{
return (x*x);
}
double p(double x)
{
return x;
}
double q(double x)
{
return 1;
}
double f(double x)
{
//return 1;
return 8*pow(x,3);
//return -4*x;
}
void solution(long n, double *y) {
long i;
double x;
double h=(B-A)/n;
for (i=0;i<n+1;i++) {
x=A+i*h;
y[i]=U(x);
}
}
void progonka(long n, double *y) {
double *a=new double[n+1];
double *b=new double[n+1];
double *c=new double[n+1];
double *d=new double[n+1];
double *k=new double[n+1];
double *l=new double[n+1];
double d1,d2,d3,gg1,gg2,bb1=1,bb2=1,aa1=1,aa2=1;
long i;
double x;
double h=(B-A)/n;
for(i=0;i<n+1;i++)
{ a[i]=0; b[i]=0; c[i]=0; y[i]=0; k[i]=0; l[i]=0; d[i]=0;}
for (i=0;i<=n;i++) {
x=A+i*h;
a[i]=t(x)/(h*h)-p(x)/(2*h);
b[i]=2*t(x)/(h*h)+q(x);
c[i]=t(x)/(h*h)+p(x)/(2*h);
d[i]=f(x);
}
gg1=U(A)-bb1*prU(A);
gg2=aa2*U(B)+bb2*prU(B);
d2=bb1/h+bb1*p(A)/2;
d1=1+d2+bb1*h*q(A)/2;
d3=gg1-bb1*h*f(A)/2;
l[0]=d2/d1;
k[0]=d3/d1;
for(i=1; i<=n; i++) {
l[i]=c[i]/(b[i]-a[i]*l[i-1]);
k[i]=(a[i]*k[i-1]-d[i])/(b[i]-a[i]*l[i-1]);
}
d1=aa2+bb2/h-bb2*p(B)/2+bb2*h*q(B)/2;
d2=bb2*p(B)/2-bb2/h;
d3=gg2-bb2*h*f(B)/2;
y[n-1]=(d3-k[n]*d2)/(d1+l[n]*d2);
for(i=n-1;i>0;i--) {
y[i-1]=l[i]*y[i]+k[i];
}
}
double n1 (double *a, int n) {
double norm = 0.0;
for (long i=0; i<n; i++)
norm += abs(a[i]);
return double(norm)/double(n);
}
double n2 (double *a, int n) {
double norm = 0.0;
for (int i=0; i<n; i++)
norm += pow(a[i], 2.0);
return sqrt(norm)/double(n);
}
double n3 (double *a, int n) {
double norm = -1.0;
for (int i=0; i<n; i++)
if (norm < abs(a[i]))
norm = abs(a[i]);
return norm/double(n);
}
void solve(double eps) {
long i,nn=5;
double mx=1000000000;
bool f=true;
double *aa=new double [2000000];
double *bb=new double [2000000];
double *cc=new double [2000000];
while (mx>=eps&&nn<2000000) {
nn*=2;
if (nn>2000000) {
f=false;
break;
}
solution(nn,aa);
progonka(nn,bb);
for (i=0;i<=nn;i++) cc[i]=abs(double(aa[i]-bb[i]));
mx=max(n1(cc,nn),n2(cc,nn));
mx=max(mx,n3(cc,nn));
}
if (!f) {
cout << "error..." << mx << " n=" <<nn;
exit(0);
}
double h=(B-A)/nn;
double x;
for (i=0;i<125;i++) cout << "-"; cout << endl;
cout << setprecision(6) << "eps= " << eps << " n= " << nn << endl;
for (i=0;i<125;i++) cout << "-"; cout << endl;
cout << "| x | U(x) | U*(x) | abs(U(x)-U*(x)) | U'(x) | U*'(x) | abs(U(x)-U*(x)) |" << endl;
for (i=0;i<nn;i++){
x=A+i*h;
cout << "| " << setprecision(6) << x << " | "<< setprecision(6) << aa[i] << " | " << setprecision(6) << bb[i] << " | " << setprecision(6)<< cc[i] << " |";
if (i!=0) cout << setprecision(6) << pr(aa,i,h) << " | "<< setprecision(6) << pr(bb,i,h) << " | "<< setprecision(6) << abs(pr(aa,i,h)-pr(bb,i,h)) << " |" << endl;
else cout << endl;
cout << cc[i] << endl;
cout << prU(x) << endl;
//cout << abs(pr(aa,i,h)-pr(bb,i,h)) << endl;
//cout << abs(pr(aa,i,h)-pr(bb,i,h)) << endl;
}
}
int main() {
cout.setf(ios_base::fixed);
solve(0.01);
//solve(0.001);
//solve(0.0001);
return 0;
}
*/
/*
#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <cmath>
using namespace std;
ofstream Fout;
double alpha = 0.0, betta = 1.0;
double alpha1 = 1.0, alpha2 = 100000.0, betta1 = 1.0, betta2 = 0.0;
double h,e=2.718282;
double U (double x)
{
return (0.1*pow(e,2*x)+pow(e,x)+0.5*pow(x,2)+3*x+4.0);
}
double dU (double x)
{
return (0.2*pow(e,2*x)+pow(e,x)+x+3.0);
}
//double gamma1 =5.1;
//double gamma2 =4.2;
double gamma1 = alpha1*U(alpha) - betta1*dU(alpha);
double gamma2 = alpha2*U(betta) + betta2*dU(betta);
double f (double x)
{
return (pow(x,2)+3*x);
}
double p (double x)
{
return -3.0;
}
double q (double x)
{
return -2.0;
}
double *progonka(int n, double *x)
{
int i;
double *y = new double [n+1];
double *k = new double [n+1];
double *l = new double [n+1];
double *a = new double [n+1];
double *b = new double [n+1];
double *c = new double [n+1];
double *d = new double [n+1];
h = (betta - alpha)/n;
cout<<gamma1<<endl;
cout<<gamma2<<endl;
a[0] = 0.0;
b[0] = alpha1 + betta1/h + p(x[0])*betta/2.0 + betta1*q(x[0])*h/2.0;
c[0] = -betta1/h - betta1*p(x[0])/2.0;
d[0] = gamma1 - betta1*h*f(x[0])/2.0;
a[n] = -betta2/h + betta2*p(x[n])/2.0;
b[n] = alpha2 + betta2/h - betta*p(x[n])/2.0 + betta2*h*q(x[n])/2.0;
c[n] = 0.0;
d[n] = gamma2 - betta2*h*f(x[n])/2.0;
for (i = 1; i < n; i++)
{
a[i] = 1.0/(h*h) - p(x[i])/(2*h);
b[i] = -2.0/(h*h) - q(x[i]);
c[i] = 1.0/(h*h) + p(x[i])/(2*h);
d[i] = f(x[i]);
}
// прямой проход
k[0] = d[0]/b[0];
l[0] = -c[0]/b[0];
for (i = 1; i <= n; i++)
{
l[i] = -c[i]/(a[i]*l[i-1] + b[i]);
k[i] = (d[i] - a[i]*k[i-1])/(a[i]*l[i-1] + b[i]);
}
y[n] = (d[n] - a[n]*k[n-1])/(a[n]*l[n-1] + b[n]);
for (i = n-1; i >= 0; i--)
y[i] = l[i]*y[i+1] + k[i];
delete [] k;
delete [] l;
delete [] a;
delete [] b;
delete [] c;
delete [] d;
return y;
}
double norma1 (double *a, double *b, int n)
{
double norma = 0.0;
for (int i = 0; i <= n; i++)
norma += (fabs(a[i] - b[2*i]));
return norma/(n+1);
}
double norma2 (double *a, double *b, int n)
{
double norma = 0.0;
for (int i = 0; i <= n; i++)
norma += pow((a[i] - b[2*i]), 2.0);
return pow(norma/(n+1), 0.5);
}
double norma3 (double *a, double *b, int n)
{
double norma = 0.0;
for (int i = 0; i <= n; i++)
if (norma < fabs(a[i] - b[2*i]))
norma = fabs(a[i] - b[2*i] );
return norma;
}
double max_norma(double *a, double *b, int n)
{
double max, n1 = norma1(a,b,n), n2 = norma2(a,b,n), n3 = norma3(a,b,n);
if (n2 > n1) n1 = n2;
if (n3 > n1) n1 = n3;
return n1;
}
void main()
{locale loc("rus_rus.866"); wcout.imbue(loc);
Fout.imbue(loc);
Fout.open("output.csv");
Fout<<setiosflags(ios::fixed);
unsigned long n = 10, n1 = 2*n;
int i, j;
double eps = 0.000001;
double max = -1.0;
cout<<fixed;
cout<<setprecision(8)<<"eps = "<<eps<<endl<<endl;
cout<<setw(8)<<"n "<<setw(13)<<"h "<<setw(13)<<"max norma"<<setw(13)<<"norma1"<<setw(13)<<"norma2"<<setw(13)<<"norma3"<<endl;
do
{
cout<<setw(8)<<n;
h = (betta - alpha)/n;
double *x = new double [n+1];
double *x2 = new double [n1+1];
double *u = new double [n+1];
double *du = new double [n+1];
double *y = new double [n+1];
double *dy = new double [n+1];
double *y2 = new double [n1+1];
for (i = 0; i <= n; i++)
{
x[i] = alpha + i*h;
u[i] = U(x[i]);
du[i] = dU(x[i]);
}
h = (betta - alpha)/n1;
for (i = 0; i <= n1; i++)
x2[i] = alpha + i*h;
y = progonka(n,x);
y2 = progonka(n1,x2);
h = (betta - alpha)/n;
///*dy[0] = (y[1] - y[0])/h - 0.5*h*(f(alpha) - p(alpha)*(y[1] - y[0])/h + q(alpha)*y[0]);
dy[n] = (y[n] - y[n-1])/h + 0.5*h*(f(betta) - p(betta)*(y[n] - y[n-1])/h + q(betta)*y[n-1]);
for(i = 1; i < n; i++)
dy[i] = (y[i+1] - y[i-1])/(2.0*h);*///
/* dy[0] = (-3.0*y[0] + 4.0*y[1] - y[2])/(2.0*h);
dy[n] = (3.0*y[n] - 4.0*y[n-1] + y[n-2])/(2.0*h);
for (i = 1; i < n; i++)
dy[i] = (y[i+1] - y[i-1])/(2.0*h);
cout<<setw(13)<<setprecision(8)<<h;
cout<<setw(13)<<setprecision(6)<<max_norma(y,y2,n)
<<setw(13)<<setprecision(6)<<norma1(y,y2,n)
<<setw(13)<<setprecision(6)<<norma2(y,y2,n)
<<setw(13)<<setprecision(6)<<norma3(y,y2,n)<<endl;
max = max_norma(y,y2,n);
Fout<<setw(12)<<"x[j]"<<";"<<setw(12)<<"y[k]"<<";"<<setw(12)<<"U(x[j])"<<";"<<setw(12)<<"fabs(y[k]-U(x[j]))"<<";"<<setw(12)<<endl;
if (max < eps)
{
h = (betta - alpha)/20;
cout<<"a="<<setprecision(2)<<alpha<<" b="<<betta<<endl;
cout<<"|"<<setw(5)<<" x "<<" | "<<setw(7)<<"y "<<" | "<<setw(7)<<"u "<<" | "
<<setw(7)<<"|y-u| "<<" | "<<setw(8)<<"y' "<<" | "<<setw(7)<<"u' "<<" | "
<<setw(8)<<"|y'-u'| "<<" | "<<setw(8)<<"|y - y2| "<<" | "<<endl;
for (i = 0; i <= 107; i++) cout<<"-";
cout<<endl;
j = 0;
while(j < 21)
{
x[j] = alpha + j*h;
int k = j*n/20;
cout<<"|"<<setw(5)<<setprecision(2)<<x[j]<<" | "<<setw(7)<<setprecision(7)
<<y[k]<<" | "<<setw(7)<<U(x[j])<<" | "<<setw(7)<<fabs(y[k]-U(x[j]))<<" | "
<<setprecision(8)<<setw(8)<<dy[k]<<" | "<<setprecision(8)<<setw(8)
<<dU(x[j])<<" | "<<setw(8)<<fabs(dU(x[j])-dy[k])<<" | "<<setw(8)<<fabs(y[k] - y2[2*k])<<" | "<<endl;
Fout<<setw(12)<<x[j]<<";"<<setw(12)<<y[k]<<";"<<setw(12)<<U(x[j])<<";"<<setw(12)<<fabs(y[k]-U(x[j]))<<";"<<setw(12)<<endl;
j++;
}
cout<<endl;
}
n*=2;
n1*=2;
delete [] x;
delete [] x2;
delete [] u;
delete [] du;
delete [] y;
delete [] dy;
delete [] y2;
}
while (max > eps);
Fout.close();
system ("PAUSE");
}
*/
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <cmath>
#include <string.h>
#include <fstream>
using namespace std;
ofstream fout;
double e=2.718282,A=0.0,B=1.0, eps=0.05;
double f(double x)
{
return (pow(x,2)+3*x);
}
double p(double x)
{ return -3.0;
}
double q(double x)
{ return -2.0;
}
double U(double x)
{
return (0.1*pow(e,2*x)+pow(e,x)+0.5*pow(x,2)+3*x+4.0);
}
double dU(double x)
{
return (0.2*pow(e,2*x)+pow(e,x)+x+3.0);
}
double norm1(double *a,double *b, int n)
{//сумма модулей / n
double norma=0;
int i;
for(i=0;i<=n;i++)
norma+=fabs(a[i]-b[i]);
norma/=n;
return norma;
}
double norm2(double *a,double *b, int n)
{//корень из суммы квадратов / корень из n
int i;
double norma=0,*raz=new double[n+1];
for(i=0;i<=n;i++)
{ raz[i]=fabs(a[i]-b[i]);
norma+=pow(raz[i],2.0);
}
norma/=n;
norma=sqrt(norma);
return norma;
}
double norm3(double *a,double *b, int n)
{//наибольший по модулю элемент
int i;
double norma;
norma=fabs(a[0]-b[0]);
for(i=1;i<=n;i++)
if(norma<fabs(a[i]-b[i]))
norma=fabs(a[i]-b[i]);
return norma;
}
double max_norma(double *a, double*b,int n)
{double n1=norm1(a,b,n),n2=norm2(a,b,n),n3=norm3(a,b,n);
if(n2>n1)
n1=n2;
if(n3>n1)
n1=n3;
return n1;
}
int main()
{ int n = 10,pr=0,j=1;
double u0,u1;
locale loc("rus_rus.866"); wcout.imbue(loc);
fout.imbue(loc);
fout.open("output.csv");
fout<<setiosflags(ios::fixed);
cout<<setiosflags(ios::fixed);
cout<<"[a, b] = ["<<A<<", "<<B<<"];";
do
{
double *x=new double[n+1], h, *a=new double[n+1], *b=new double[n+1],*c=new double[n+1], *y=new double[n+1],*dy=new double[n],*s=new double[n],*un=new double[n+1],*dun=new double[n+1],n1,n2,n3,max;
h=(B-A)/n;
u0=y[0]=5.1;
u1=dy[0]=4.2;
y[1]=u1*h+u0;
for(int i=0;i<=n;i++)
{ x[i]=A+i*h;
un[i]=U(x[i]);
dun[i]=dU(x[i]);
}
for ( int i = 1; i < n; i++)
{a[i] = 1/pow(h,2)-p(x[i])/(2*h);
b[i]=2/pow(h,2)+q(x[i]);
c[i]=1/pow(h,2)+p(x[i])/(2*h);
}
for( int i = 1; i < n; i++)
y[i+1]=(f(x[i])-a[i]*y[i-1]+b[i]*y[i])/c[i];
for( int i = 1; i < n; i++)
dy[i]=(y[i+1]-y[i-1])/(2*h);
n1=norm1(un,y,n);
n2=norm2(un,y,n);
n3=norm3(un,y,n);
max=max_norma(un,y,n);
cout<<" n = "<<n<<"norma1 "<<setprecision(4)<<n1<<"norma2 = "<<setprecision(4)<<n2<<"norma3 = "<<setprecision(4)<<n3<<"max_norma"<<setprecision(4)<<max<<endl;
if (max<=0.5) pr=1;
else {
//n=n*2;
j++;}
if(pr==1)
{int dop=n/20;
cout<<endl<<" n = "<<n<<endl;
cout<<" "<<setw(9)<<" x"<<setw(9)<<" y"<<setw(9)<<" u"<<setw(14)<<" [y-u]"<<endl ;
for(int i=0;i<20;i++)
cout<<endl<<setw(4)<<i+1<<" "<<setprecision(4)<<setw(9)<<x[i*dop]<<setw(9)<<y[i*dop]<<setw(9)<<U(x[i*dop])<< setw(14)<<(fabs(U(x[i*dop])-y[i*dop]))/n<<endl;
fout<<"x[i]"<<";"<<"y[i]"<<";"<<"dy[i]"<<";"<<"u[i]"<<";"<<"du[i]"<<";"<<"fabs(u[i]-y[i])"<<";"<<" n "<<";"<<"norma1 "<<";"<<"norma2 "<<";"<<"norma3 "<<";"<<"max_norma"<<endl;
for(int i=0;i<=n;i++)
fout<<x[i]<<";"<<y[i]<<";"<<dy[i]<<";"<<un[i]<<";"<<dun[i]<<";"<<fabs(un[i]-y[i])<<";"<<n<<"; "<<n1<<";"<<n2<<";"<<n3<<";"<<max<<endl;
}
}
while (pr==0);
fout.close();
_getch();
return 1;
}