See this documentation on Using Multiple X- and Y-Axes. Something like this should do the trick:
figure
ax1 = gca;
hold on
plot(x,y1)
plot(x,y2)
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
plot(x,y3,'Parent',ax2);
Edit: whoops, missed a hold command. Should work now. Also, to remove the second x-axis on top, simply add 'XTickLabel',[]
to the axes
command.
As an aside, you really shouldn't use arrayfun
for y1=arrayfun(@(x) x^2,x);
. Instead, use the .^
operator: y1=x.^2;
. It's much better style and is much quicker.