This is because lat has 1001 values. You have to modify lat to match the spacing in your original data.
To generate 1000 values from 30 to 40, you can use
lat = linspace(30, 40, 1000)
But if this is right you have to check, based on on the boundaries of lat in your data. It could also be
lat = linspace(30, 39.99, 1000)
or
lat = linspace(30.01, 40, 1000)
or something else. linespace is always your friend, it ensures that you have the right number of values.
