ЧСМ 4 лаба

 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
#include "stdafx.h"
#include <iostream>
#include <math.h>
int N = 12, q = N, p = N;
double f(double x) {return 300*exp(x);}
double f1(double x) {return N*exp(x);}
double* diffur(double* x, double h, int n) {
double *alpha = new double[n-1];
double *beta = new double[n-1];
double *y = new double[n];
double a, b, c, d;
a = 1-p*h/2.0;
b = q*h*h-2.0;
c = 1.0+p*h/2.0;
d= f(x[1])*h*h-(1-p*h/2.0)*f1(x[0]);
alpha[0] = -c/b;
beta[0] = d/b;
for (int i=1;i<n-1;i++) {
d = f(x[i+1])*h*h;
alpha[i] = -c/(a*alpha[i-1]+ b);
beta[i] = (d-a*beta[i-1])/(a*alpha[i-1]+b);
}
d = f(x[n])*h*h-(1+p*h/2.0)*f1(x[n+1]);
y[n-1] = (d-beta[n-2]*a)/(a*alpha[n-2]+b);
for (int i=n-2;i>=0;i--) {
y[i] = alpha[i]*y[i+1]+beta[i];
}
return y;
}
int main(int argc, char* argv[]) {
int n = 20;
double *y = new double[n];
double *x = new double[n];
double h = (1.0-0.0)/(n-1);
for (int i=0;i<n;i++) {
x[i] = h*i;
}
double *buf = diffur(x, h, n-2);
y[0] = f1(x[0]);
y[n-1] = f1(x[n-1]);
for (int i=0;i<n-2;i++) {
y[i+1] = buf[i];
}
printf("y\"(x) + 12*e^x*y\'(x) + 12*e^x*y(x) = 300*e^x\n");
printf("x\t\t y\t\t y(calculated)\t abs\n");
for (int i=0;i<n;i++) {
printf("%f\t %f\t %f\t %f\n", x[i], f1(x[i]), y[i], abs(f1(x[i])-y[i]));
}
}