function [ a, f ] = assemble_heat_sparse ( node_num, node_xy, node_condition, ... element_order, element_num, element_node, quad_num, nz_num, time ) %% ASSEMBLE_HEAT_SPARSE assembles the finite element system for the heat equation. % % Discussion: % % MATLAB's built-in sparse matrix storage is used. % % Modified: % % 07 January 2007 % % Author: % % John Burkardt % % Parameters: % % Input, integer NODE_NUM, the number of nodes. % % Input, real NODE_XY(2,NODE_NUM), the coordinates of nodes. % % Input, integer NODE_CONDITION(NODE_NUM), reports the condition % used to set the unknown associated with the node. % 0, unknown. % 1, finite element equation. % 2, Dirichlet condition; % 3, Neumann condition. % % Input, integer ELEMENT_ORDER, the order of the elements. % % Input, integer ELEMENT_NUM, the number of elements. % % Input, integer ELEMENT_NODE(ELEMENT_ORDER,ELEMENT_NUM); % ELEMENT_NODE(I,J) is the global index of local node I in element J. % % Input, integer QUAD_NUM, the number of quadrature points used in assembly. % % Input, integer NZ_NUM, the (maximum) number of nonzeros in the matrix. % If set to 0 on input, we hope MATLAB's sparse utility will be able % to take over the task of reallocating space as necessary. % % Input, real TIME, the current time. % % Output, real sparse A, the NODE_NUM by NODE_NUM coefficient matrix, % stored in MATLAB's sparse format. % % Output, real F(NODE_NUM), the right hand side. % % Local parameters: % % Local, real BI, DBIDX, DBIDY, the value of some basis function % and its first derivatives at a quadrature point. % % Local, real BJ, DBJDX, DBJDY, the value of another basis % function and its first derivatives at a quadrature point. % % % Initialize the arrays to zero. % f(1:node_num) = 0.0; fprintf ( 1, '\n' ); fprintf ( 1, 'ASSEMBLE_HEAT_SPARSE:\n' ); fprintf ( 1, ' Setting up sparse heat matrix with NZ_NUM = %d\n', nz_num ); a = sparse ( [], [], [], node_num, node_num, nz_num ); % % Get the quadrature weights and nodes. % [ quad_w, quad_xy ] = quad_rule ( quad_num ); % % Add up all quantities associated with the TRIANGLE-th triangle. % for element = 1 : element_num % % Make a copy of the triangle. % t3(1:2,1:3) = node_xy(1:2,element_node(1:3,element)); t6(1:2,1:6) = node_xy(1:2,element_node(1:6,element)); % % Map the quadrature points QUAD_XY to points XY in the physical triangle. % xy(1:2,1:quad_num) = reference_to_physical_t3 ( t3, quad_num, quad_xy ); area = abs ( triangle_area_2d ( t3 ) ); w(1:quad_num) = area * quad_w(1:quad_num); k_quad = k_coef ( quad_num, xy, time ); rhs_quad = rhs ( quad_num, xy, time ); % % Consider the QUAD-th quadrature point. % for quad = 1 : quad_num % % Consider the TEST-th test function. % % We generate an integral for every node associated with an unknown. % for test = 1 : element_order i = element_node(test,element); [ bi, dbidx, dbidy ] = basis_11_t6 ( t6, test, xy(1:2,quad) ); f(i) = f(i) + w(quad) * rhs_quad(quad) * bi; % % Consider the BASIS-th basis function, which is used to form the % value of the solution function. % for basis = 1 : element_order j = element_node(basis,element); [ bj, dbjdx, dbjdy ] = basis_11_t6 ( t6, basis, xy(1:2,quad) ); a(i,j) = a(i,j) ... + w(quad) * ( dbidx * dbjdx + dbidy * dbjdy + k_quad(quad) * bj * bi ); end end end end