Created
December 16, 2013 17:08
-
-
Save Corwinpro/7990486 to your computer and use it in GitHub Desktop.
Хопф (крутой)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_DEPRECATE | |
#include "stdio.h" | |
#include "math.h" | |
#include <iostream> | |
using namespace std; | |
double t = 0.001; | |
double h = 0.1; | |
double a = -5.0; | |
double b = 10.0; | |
int size = 1+(b-a)/h; | |
double function(double x) | |
{ | |
return exp(-x*x/2); | |
} | |
void set_conditions(double * u) //Задание начальных условий | |
{ | |
for (int i = 0; i*h <= (b-a); i++) | |
*(u+i) = function(i*h+a); | |
} | |
void get_U_next(double * u, double * U_new) | |
{ | |
*U_new = *u; | |
*(U_new+size - 1) = *(u+size -1); //Right border condition | |
for (int i = size-2; i > 0; i--) //Заполнение внутри | |
*(U_new+i) = (-1)*h/t + pow(h*h/(t*t) +2*h/t * *(u+i) + (*(U_new+i-1))*(*(U_new+i-1)), 0.5); //Метод из учебника | |
//*(U_new+i) = -(*(u+i)*(*(u+i+1) - *(u+i-1)) + 2 * ((*(u+i+1))*(*(u+i+1)) - (*(u+i-1))*(*(u+i-1))))/(6*h)*t + *(u+i); //Метод комбинированный | |
//*(U_new+i) = *(u+i) - t/(2*h) * (*(u+i) * (*(u+i+1) - *(u+i-1))); //Метод 1 | |
//*U_new = (*(U_new+1)*4 - *(U_new+2) - 2*h*1)/3; //Left border condition | |
// O(h) precision: | |
//*U_new = *(U_new+1) - h; | |
} | |
void main() | |
{ | |
FILE * file = fopen("file.txt", "w"); | |
double * u = new double[size * sizeof(double)]; | |
double * u_next = new double[size * sizeof(double)]; | |
set_conditions(u); | |
double time = 4.0; | |
double time_current = 0.0; | |
while (time_current < time) //Пересчет по слоям до определенного времени | |
{ | |
time_current = time_current + t; | |
get_U_next(u, u_next); | |
for (int i = 0; i < size; i++) | |
*(u+i) = *(u_next + i); | |
} | |
for (int i = 0; i < size; i++) | |
fprintf(file, "%e %e\n", i*h, *(u+i)); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment