I don't know the answer to your question but I looked at your model and I have a few observations. In your model I assume the inputs to your forward kinematics function are the joint angle values and the output is the end effector location. And I assume the inputs to your inverse kinematics function is the end effector location and the outputs are the joint values.
If this is correct, then why would you not pass the output from forward kinematics back to your inverse kinematics function to validate that you are getting back the same joint values?
I made this change and it seems like the first output value matches.
The reason the other joint angles might still not be matching is because you could have multiple solutions to worry about. For example, in the computation of your inverse kinematics function, you have
r = sqrt(ph1^2+(ph3-d1)^2);
There could be two solutions to the sqrt function. A positive and a negative value. And MATLAB returns the positive value. Similar thing is true for other functions like acos, where multiple angles can give the same result. All these will result in multiple solutions to result in the same end effector location.