Skip to content

Instantly share code, notes, and snippets.

@Corwinpro
Created November 24, 2013 12:42
Show Gist options
  • Save Corwinpro/7626869 to your computer and use it in GitHub Desktop.
Save Corwinpro/7626869 to your computer and use it in GitHub Desktop.
Теплопроводность явная схема
#define _CRT_SECURE_NO_DEPRECATE
#include "stdio.h"
#include "math.h"
#include <iostream>
using namespace std;
double lambda = 1.0; //Коэффициент теплопроводности
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) = lambda*(*(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
// 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