Day 2 may be difficult to accomplish completely. La Sagrada Familia is a bit farther from the center than your other targets, so I suggest buying your entry ticket for the first available time. If you want to climb one of the towers, that will be a separate ticket. Maybe book it for an hour later. You can finish seeing the church after the tower if you need to. Then do Casa Mila and Casa Batllo. I think you need at least 90 minutes in each, not counting the time spent in line (can be very long if you need to buy a ticket). You'll want much more time for Casa Mila if you're seriously interested and want to review all the information about Gaudi's other buildings that's presented near the end of the tour. There's a bit of a walk between Casa Mila and Casa Batllo. Not far, but don't book the sites back-to-back.
Las Ramblas is basically a sea of people these days and far less interesting than the Barri Gotic, so leave Las Ramblas till last. If you don't get to it, no big deal. If you are still vertical on Day 1 after seeing Parc Guell, you'll probably have time for Las Ramblas then. If this is the day you arrive on an overseas flight, I can only wish you good luck.
La Sagrada Familia, Parc Guell, Casa Mila and Casa Batllo are all very popular and buying tickets in advance is highly advised. They might not actually sell out, but you have far too little time for Barcelona and must do everything very efficiently. You don't want to get stuck trying to guess how long Casa Mila and Casa Battlo will take in order to book just the right time for LSF, for example.
In Madrid you'll find the San Miguel Market (great for tapas if you prefer to avoid Spain's extraordinarily late meal times) very near the Plaza Mayor. Rick has a good walking tour in his book. You'll have time for more than the Palacio Real on Day 9, of course. Read about Madrid's museums to see whether there's one that might be of interest.
This is going to be an exhausting trip with five major destinations in 8-1/2 days.