function u = solve ( adiag, aleft, arite, f, nu ) %% SOLVE solves a tridiagonal matrix system of the form A*x = b. % % Modified: % % 06 November 2006 % % Parameters: % % Input, real ADIAG(NU), ALEFT(NU), ARITE(NU). % the diagonal, left and right entries of the equations. % % Input, real F(NU), the right hand side of the linear system. % % Input, INTEGER NU, the number of equations to be solved. % % Output, real U(NU), the solution of the linear system. % % % Handle the special case of a single equation. % if ( nu == 1 ) u(1) = f(1) / adiag(1); % % The general case, when NU is greater than 1. % else arite(1) = arite(1) / adiag(1); for i = 2 : nu-1 adiag(i) = adiag(i) - aleft(i) * arite(i-1); arite(i) = arite(i) / adiag(i); end adiag(nu) = adiag(nu) - aleft(nu) * arite(nu-1); u(1) = f(1) / adiag(1); for i = 2 : nu u(i) = ( f(i) - aleft(i) * u(i-1) ) / adiag(i); end for i = nu-1 : -1 : 1 u(i) = u(i) - arite(i) * u(i+1); end end