It's so incredibly easy. I've done it many times in both directions.
Get yourself to Barcelona coach station, bus number 54 will get you there - I'm sure there are others, but I've used 54 many times. Also on Renfe (trains) - lines (Linea) 1, 3 and 4.
Barcelona to Madrid on the coach takes less than half a day. So if you get the coach at noon, you'll be there mid-evening. No need to book a ticket in advance, just turn up and buy one at the ticket casa. Expect to pay around €25 - €30 for a seat.
The fast trains take around three hours and are just superb. Expect to pay around €10 - €20 more for a ticket, than the bus price.