finished code and plot 4