Ok, Thanks KKSV for the help. However, here is a problem. The order of elements in the vectors X(:), Y(:) and Z(:) is not the same as the order of element when you vectorize Tn, that's why when you plot them, they do not match.
So, I had to write a nested loop to re-arrange the order of elements in Tn and store it in another vector called TT to make it match with the order of X(:), Y(:) and Z(:). (By 'order', I mean the way they are arranged)
Here is the nested loop attached to this post for your reference.
I am sure there is a better and more efficient way of doing this, but this is all I can do about it.