Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const killFloat = number => Number(number.toFixed(2)); // потому что в js неправильное деление и умножение
- // чисел типа double
- const epsilon = 0.5 * Math.pow(10, -5);
- const RungeKutta3 = (xi, y, p, h, derivative) => {
- const yi = y[killFloat(xi * 10)];
- const pi = p[killFloat(xi * 10)];
- const k1 = h * pi;
- const l1 = h * (yi + 2.6 * Math.pow(xi, 3) - 2.6 * xi * xi + 5.2);
- const k2 = h * (pi + l1 / 2);
- const l2 = h * (yi + k1 / 2 + 2.6 * Math.pow(xi + h / 2, 3) - 2.6 * Math.pow(xi + h / 2, 2) + 5.2);
- const k3 = h * (pi + 2 * l2 - l1);
- const l3 = h * (yi + 2 * k2 - k1 + 2.6 * Math.pow(xi + h, 3) - 2.6 * Math.pow(xi + h, 2) + 5.2);
- const yNew = yi + (k1 + 4 * k2 + k3) / 6;
- const pNew = pi + (l1 + 4 * l2 + l3) / 6;
- const xNew = killFloat(xi + h);
- y[killFloat(xNew * 10)] = yNew;
- p[killFloat(xNew * 10)] = pNew;
- // console.info(xNew);
- // console.info('RK-3:', xNew, yNew);
- // console.info('Actual:', xNew, derivative(xNew));
- if (xNew >= 5) {
- resultt = yNew;
- return yNew;
- }
- RungeKutta3(xNew, y, p, h, derivative);
- }
- const thirdDerAnswer = x => Math.pow(Math.E, x) + 2 * Math.pow(Math.E, -x) - 2.6 * x * x * x + 2.6 * x * x - 15.6 * x;
- const shooting = () => {
- const h = 0.2;
- const actualY = Math.pow(Math.E, 5) + 2 * Math.pow(Math.E, -5) - 338;
- let left = 0;
- let right = 5;
- let root = 0;
- const y = [];
- const x = [];
- const p = [];
- p[0] = -16.6;
- y[0] = 0;
- while (Math.abs(y[y.length - 1] - actualY) > epsilon) {
- const c = (left + right) / 2;
- y[0] = left;
- RungeKutta3(0, y, p, h, thirdDerAnswer);
- const res1 = y[y.length - 1] - actualY;
- y[0] = c;
- RungeKutta3(0, y, p, h, thirdDerAnswer)
- const res2 = y[y.length - 1] - actualY;
- root = res1 * res2;
- if (root >= 0) {
- left = c;
- } else {
- right = c;
- }
- }
- let current = 0;
- while (current <= 5) {
- // console.info('Actual:', current, thirdDerAnswer(current));
- // console.info(y[killFloat(current * 10)]);
- current = killFloat(current + h);
- }
- }
- const my_q = x => 2.6 * x * x * x - 2.6 * x * x + 5.2;
- const myProgonka = () => {
- const h = 0.05;
- const mu = [];
- const y = [];
- const lambda = [];
- mu[0] = 16.6 * h;
- lambda[0] = 1;
- let x = h;
- for (let i = 1; i < killFloat(5 / h); i++) {
- lambda[i] = 1 / (2 + h * h - lambda[i - 1]);
- mu[i] = (my_q(x) * h * h - mu[i - 1]) / (lambda[i - 1] - 2 - h * h);
- x = killFloat(x + h);
- }
- y[lambda.length] = Math.pow(Math.E, 5) - 2 * Math.pow(Math.E, -5) - 338;
- let current = killFloat(5 - h);
- for (let i = killFloat(5 / h) - 1; i > -1; i--) {
- y[i] = lambda[i] * y[i + 1] + mu[i];
- // console.info('Computed:', y[i]);
- // console.info('Actual:', current, thirdDerAnswer(current));
- current = killFloat(current - h);
- }
- for (let num of y) {
- // console.info(num);
- }
- }
- myProgonka();
- shooting();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement