MATLAB Answers


plot a line that shows the difference between two lines

Asked by Locks
on 1 Apr 2013


I have the following code:

xlabel('Stock Price ($)');
ylabel('Option price ($)');
axis([8 12  -1 2]);
text(10,1.1,'delta-hedge at t=0')

that gives me one straight line and a curve and I would like to plot a third line with the difference (y1-y2) but it's not working, probably due to the fact that there are different elements, is there any way to do this?



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 1 Apr 2013

Use interp1() to produce values with a common x base so that you can subtract meaningfully.


plot(xi, yi - yj)

You interpolated over the same x values, xi, so there must be the same number of elements between the two.

on 1 Apr 2013


is there a way to include also the other plots I have above?

hold on

after the first plot.

Answer by Locks
on 2 Apr 2013

thanks! it's working now, but is there really no way to get the exact slope? because I plot the difference (or to be more exact it's the sum because the curve starts negative and the line becomes negative) and the combined position should show a delta hedged position and at the moment it's obvious that this is just an bad proxy


As discussed in in order to get the exact slope, you need to work with the formula, not with the calculated data points.

Imagine you are heading due west on a road, and you can see 1 km ahead that the road continues on due west. What is the exact slope?

Oh, did I forget to mention that you are on a mountain-side and that the road you can see ahead is on the next peak over?

on 2 Apr 2013

the slope at a specific point should be the first dderivative or am I mistaken? I don't see how I can implement your example in a way to create a line with the same slope just with the negative slope starting on the upper left corner

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!