Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void tridiagonal(vector <double> a, vector <double> b, vector <double> c, vector <double> d, vector <double> & x){
- int N = a.size();
- vector <double> alpha(N), beta(N);
- for(int i = 1; i < N; ++i){
- alpha[i] = -c[i - 1] / (a[i - 1] * alpha[i - 1] + b[i - 1]);
- beta[i] = (d[i - 1] - a[i - 1] * beta[i - 1]) / (a[i - 1] * alpha[i - 1] + b[i - 1]);
- }
- x[N - 1] = (d[N - 1] - a[N - 1] * beta[N - 1]) / (b[N - 1] + a[N - 1] * alpha[N - 1]);
- for(int i = N - 2; i >= 0; --i){
- x[i] = alpha[i + 1] * x[i + 1] + beta[i + 1];
- }
- }
- int main(){
- cout << "Enter N:\n";
- int N;
- cin >> N;
- cout << "Enter a (i != 0), b, c (i != N - 1) and d:\n";
- vector <double> a(N), b(N), c(N), d(N);
- for(int i = 0; i < N; ++i){
- if(i != 0){
- cin >> a[i];
- }
- cin >> b[i];
- if(i != N - 1){
- cin >> c[i];
- }
- cin >> d[i];
- }
- vector <double> x(N);
- tridiagonal(a, b, c, d, x);
- cout << "Solutions:\n";
- for(auto & res : x){
- cout << res << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment