Fitting strains and displacements by minimizing dislocation energy