Last active
December 31, 2015 17:59
-
-
Save Corwinpro/8023743 to your computer and use it in GitHub Desktop.
Hopf
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.0001; | |
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); | |
} | |
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; //Left border condition | |
*(U_new+size - 1) = *(u+size -1); //Right border condition | |
for (int i = 1; i < size-1; 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 | |
} | |
void main() | |
{ | |
FILE * file = fopen("file.txt", "w"); | |
double * u = new double[size * sizeof(double)]; //Создаем массив для старого слоя размером size | |
double * u_next = new double[size * sizeof(double)]; //То же самое для подсчета нового слоя | |
set_conditions(u); //Задаем начальные условияы | |
double time = 5.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_next | |
*(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