复化Simpson自动控制误差计算积分

#include
#include
using namespace std;

vector<vector<double>> I(int n) {
	vector<vector<double>>ans(n, vector<double>(n));
	for (int i = 0; i < n; ++i)ans[i][i] = 1;
	return ans;
}
vector<double> Doolittle(int n, vector<vector<double>>A, double*b) {
	vector<vector<double>> L = I(n); 
	vector<vector<double>>U(n, vector<double>(n));
	vector<double>y(n);
    double sum = 0; 
	vector<double>x(n);

	for (int k = 0; k < n; ++k) {
		for (int j = k; j < n; ++j) {
			for (int r = 0; r <=k - 1; ++r)
				sum += L[k][r] * U[r][j];
			U[k][j] = A[k][j] - sum;
			sum = 0;
		}

		for (int i = k + 1; i < n; ++i) {
			for (int r = 0; r <=k - 1; ++r)
				sum += L[i][r] * U[r][k];
			L[i][k] = (A[i][k] - sum) / U[k][k];
			sum = 0;
		}
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j <=i-1; ++j)
			sum += L[i][j] * y[j];
		y[i] = b[i] - sum;
		sum = 0;
	}
	for (int i = n - 1; i >= 0; --i) {
		for (int j = i+1; j<n; ++j)
			sum += U[i][j] * x[j];
		x[i] = (y[i]-sum)/ U[i][i];
		sum = 0;
	}
	return x;
}
void main() {
	vector<vector<double>>A ={{2,1,1},{1,3,2},{1,2,2}};
	double b[] = { 4,6,5 };
	vector<double>ans=Doolittle(3, A, b);
	for (double i : ans)cout << i << ' '; //输出类型也要为double
}

你可能感兴趣的:(干货,c++,算法)