matlab追赶法求三对角线性方程组程序代码
时间: 2023-07-20 13:06:01 浏览: 104
以下是使用追赶法求解三对角线性方程组的MATLAB代码:
```matlab
function x = thomas(a, b, c, d)
% a, b, c are the subdiagonal, diagonal, and superdiagonal of the matrix A,
% d is the right-hand side vector
n = length(d);
c_star = zeros(n-1,1);
d_star = zeros(n,1);
% Forward elimination
c_star(1) = c(1) / b(1);
d_star(1) = d(1) / b(1);
for i = 2:n-1
temp = b(i) - a(i) * c_star(i-1);
c_star(i) = c(i) / temp;
d_star(i) = (d(i) - a(i) * d_star(i-1)) / temp;
end
d_star(n) = (d(n) - a(n) * d_star(n-1)) / (b(n) - a(n) * c_star(n-1));
% Back substitution
x = zeros(n,1);
x(n) = d_star(n);
for i = n-1:-1:1
x(i) = d_star(i) - c_star(i) * x(i+1);
end
end
```
其中,`a`, `b`, `c` 分别为三对角矩阵 $A$ 的下对角线、主对角线和上对角线,`d` 为方程组的右侧向量。函数返回方程组的解向量 `x`。
阅读全文