⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chafen.cpp

📁 差分法求解微分方程:古典显式法,收敛性最差;古典隐式法;Crank-Nicolson法,收敛性最好
💻 CPP
字号:
#include <vector>
#include <fstream>
#include <iostream>

#include "phi.h"
#include "arpha.h"
#include "beta.h"

#include "old_chafen.h"		//古典显式法,收敛性最差
#include "old_y_chafen.h"	//古典隐式法
#include "crank_nicolson.h"	//Crank-Nicolson法,收敛性最好

#include "math.h"
//#include "fn_chafen.h"

//crank_nicolson方法的结果最好,
//编程时 一定要注意下标,大部分错误都是它造成的


//本程序所涉及到的子程序编程所花时间可能有六个小时.太慢了.

using namespace std;
//有关不确定维数的数组编程用vector是最好的了.

main()
{

	double a;

	double x0, xn, t0;
	int total, n;//total = (tn - t0) / tao

	double h, r;

	cout << "input x0 and xn\n";
	cin >> x0;
	cin >> xn;

	cout << "input h and r\n";
	cin >> h;
	cin >> r;

	cout << "input t0 and total\n";
	cin >> t0;
	cin >> total;

	cout << "input a\n";
	cin >> a;

	n = (int) (xn - x0) / h;


	vector<double> u0,	//t=0是的解,即初始条件
		us,				//x0时的解,即边界条件
		um;				//xn时的解,即边界条件

	for(int i = 0; i < n + 1; i++)
		u0.push_back(phi(x0 + h * i));

	total++;

	for(i = 1; i < total; i++)///
	{
		us.push_back(arpha(t0 + i * r*h*h/a));
		um.push_back(beta(t0 + i *r*h*h/a));
	}

	vector<double> u;
	u = old_chafen(a, u0, us, um, x0, t0, r, r*h*h/a, total);

	for(i = 0; i < n + 1; i++)
		cout << "\t" << x0 + h * i;

	cout << "\n" << t0;
	for(int j = 0; j < u.end() - u.begin(); j++)
	{
		cout << "\t" << u[j];
		if(j % (n + 1) == n)
		{
			cout << endl;
			cout << (j / (n + 1) + 1) * r*h*h/a + t0;
		}
	}



	cout << "\ncalculate by old_y_fancha\n";
	u = old_y_chafen(a, u0, us, um, x0, t0, r, r*h*h/a, total);


	cout << "\n" << t0;
	for(j = 0; j < u.end() - u.begin(); j++)
	{
		cout << "\t" << u[j];
		if(j % (n + 1) == n)
		{
			cout << endl;
			cout << (j / (n + 1) + 1) * r*h*h/a + t0;
		}
	}


	
	
	cout << "\ncalculate by crank_nicolson\n";
	u = crank_nicolson(a, u0, us, um, x0, t0, r, r*h*h/a, total);

	cout << "\n" << t0;
	for(j = 0; j < u.end() - u.begin(); j++)
	{
		cout << "\t" << u[j];
		if(j % (n + 1) == n)
		{
			cout << endl;
			cout << (j / (n + 1) + 1) * r*h*h/a + t0;
		}
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -