Changed solve to work for non-linear piecewise functions

Changed solve to fix #8587 and added a test.
Merge pull request #8680 from nishithshah2211/issue8587

Bug fix #8587