Created
December 19, 2013 03:17
-
-
Save Corwinpro/8033862 to your computer and use it in GitHub Desktop.
Явная схема теплопроводности
This file contains hidden or 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.006; | |
| double h = 0.1; | |
| double a = 0.0; | |
| double b = 1.0; | |
| int size = 1+(b-a)/h; | |
| double function(double x) | |
| { | |
| return x*x*(1-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+size - 1) = 0; //Right border condition | |
| for (int i = size-2; i > 0; i--) //Заполнение внутри | |
| *(U_new+i) = (*(u+i-1)-*(u+i)*2+*(u+i+1))/(h*h)*t + *(u+i); | |
| *U_new = (*(U_new+1)*4 - *(U_new+2) - 2*h*1)/3; //Left border condition c точностью O(h*h) | |
| // 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 = 25.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