We present a new second-order stable Cartesian grid algorithm for solving anisotropic elliptic boundary value problems on bounded irregular domains in two dimensions (2D) and three dimensions (3D). The irregular domain is embedded in a uniform Cartesian mesh, but grid points outside of the domain are not used. Second- order local truncation error and the sufficient Gerschgorin criterion for stability impose some conditions to be satisfied by the weights of the discretization scheme at a particular interior grid point. A necessary and sufficient condition, in terms of the anisotropy matrix, for the existence of a Gerschgorin second-order scheme at a given interior grid point is found. This theorem is proved in 2D and 3D. The governing partial differential equations are discretized through a new technique which uses a linear programming approach to find the scheme at points far away from the irregular boundary. Near the irregular boundary, with the addition of boundary information,special discretizations are found by using an optimization approach.