追赶法解方程
//追赶法解方程组Pursue.cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; class SolutionEquations { private: vector<double> a; vector<double> b; vector<double> c; vector<double> f; vector<double> x; public: void setABCF(vector<double> in_a, vector<double> in_b, vector<double> in_c, vector<double> in_f) { a = in_a; b = in_b; c = in_c; f = in_f; } //追赶法解方程组 vector<double>& Pursue() { vector<double> bita; vector<double> y; int n = f.size(); bita.push_back(c.at(0)/b.at(0)); y.push_back(f.at(0)/b.at(0)); for (int i=1; i<n-1; i++) { double alpha = b.at(i)-a.at(i)*bita.at(i-1); double tmp = c.at(i)/alpha; bita.push_back(tmp); tmp = (f.at(i)-a.at(i)*y.at(i-1))/alpha; y.push_back(tmp); } double alpha = b.at(n-1)-a.at(n-1)*bita.at(n-2); double tmp = (f.at(n-1)-a.at(n-1)*y.at(n-2))/alpha; y.push_back(tmp); x.push_back(y.at(n-1)); for (int i=n-2,k=0; i>=0; i--,k++) { double tmp = y.at(i)-bita.at(i)*x.at(k); x.push_back(tmp); } reverse(x.begin(),x.end()); return x; } }; int main() { SolutionEquations solution; vector<double> a; vector<double> b; vector<double> c; vector<double> f; a.push_back(0); a.push_back(-1); a.push_back(-2); a.push_back(-3); b.push_back(2); b.push_back(3); b.push_back(4); b.push_back(5); c.push_back(-1); c.push_back(-2); c.push_back(-3); f.push_back(6); f.push_back(1); f.push_back(-2); f.push_back(1); solution.setABCF(a,b,c,f); vector<double>x = solution.Pursue(); for (int i=0; i<x.size(); i++) { cout << "x=" << x.at(i) << endl; } }
作者:涵曦(www.hanxi.cc)
出处:hanxi.cnblogs.com
GitHub:github.com/hanxi
Email:im.hanxi@gmail.com
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
《 Skynet 游戏服务器开发实战》
-
学习地址:
-
优惠推荐码:
2CZ2UA5u
-
可以先免费试学前 2 章内容