I ran the code provided and did not receive any error and got a plot at the end.
Provide us with what is the error message that you receive along with the MATLAB version and OS being used as that might help us to figure out what might be going wrong!