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

📄 trige.cpp

📁 c++的数学物理方程数值算法源程序。这是"Numerical Methods for Physics"第二版的源程序。
💻 CPP
字号:
#include "NumMeth.h"

double trige( Matrix A, Matrix b, Matrix& x) {// Function to solve b = A*x by Gaussian elimination where// the matrix A is a packed tridiagonal matrix// Inputs//   A      Packed tridiagonal matrix, N by N unpacked//   b      Column vector of length N// Output //   x      Solution of b = A*x; Column vector of length N
// determ   Determinant of A  //* Check that dimensions of a and b are compatible  int N = A.nRow();  assert( N == b.nRow() && A.nCol() == 3 );  //* Unpack diagonals of triangular matrix into vectors  Matrix alpha(N), beta(N), gamma(N);
  int i;  for( i=1; i<=(N-1); i++ ) {    alpha(i) = A(i+1,1);    beta(i) = A(i,2);    gamma(i) = A(i,3);  }  beta(N) = A(N,2);  //* Perform forward elimination  for( i=2; i<=N; i++ )	{    double coeff = alpha(i-1)/beta(i-1);    beta(i) -= coeff*gamma(i-1);    b(i) -= coeff*b(i-1);  }

  //* Compute determinant as product of diagonal elements
  double determ = 1.0;
  for( i=1; i<=N; i++ )
	determ *= beta(i);  //* Perform back substitution  x(N) = b(N)/beta(N);  for( i=N-1; i>=1; i--)    x(i) = (b(i) - gamma(i)*x(i+1))/beta(i);

  return( determ );}

⌨️ 快捷键说明

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