plot a line that shows the difference between two lines

hi,
I have the following code:
x=[0:0.0001:20]
y1=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
x1=[8:0.001:12]
y2=[0.85:-0.000425:-0.85]
plot(x,y1,x1,y2,'k')
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?

Respuestas (2)

Walter Roberson
Walter Roberson el 1 de Abr. de 2013
Use interp1() to produce values with a common x base so that you can subtract meaningfully.

10 comentarios

I am not sure how to include that into the code, could you give me an example how you would do that?
x=[0:0.0001:20]
y1=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
x1=[8:0.001:12]
y2=[0.85:-0.000425:-0.85]
xi = 0:.25:10;
interp1(y1,y,xi)
plot(x,y1,x1,y2,'k')
xlabel('Stock Price ($)');
ylabel('Option price ($)');
axis([8 12 -1 2]);
text(10,1.1,'delta-hedge at t=0')
%set(gca,'Box','off')
I tired this, but that doesn't work at all
Image Analyst
Image Analyst el 1 de Abr. de 2013
Editada: Image Analyst el 1 de Abr. de 2013
You have to accept the output of interp1() into a variable and then use that variable, plus pass it the proper variables of course.
x=[0:0.5:20]
y=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
xi=0:0.25:20
yi=interp1(x,y,xi);
x1=[8:0.001:12]
y2=[0.85:-0.000425:-0.85]
xj=0:0.25:20
yj=interp(x1,y1,xj)
plot(x,y,'o',xi,yi,x1,y2,xj,yj)
it worked for the first series, but if I try to add the second, it doesn't. what am I doing wrong?
You passed the undefined y1 in to interp() (which is a different routine) instead of passing in y2.
Also, to prevent possible problems, it is best to use the same xi in both cases instead of reassigning it with something that "should" be the same.
I changed the code to this:
x=[0:0.5:20]
y=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
xi=0:0.25:20
yi=interp1(x,y,xi);
x1=[8:0.001:12]
y2=[0.85:-0.000425:-0.85]
yj=interp(xi,y2,xj)
plot(x,y,'o',xi,yi,x1,y2,xi,yj)
but I get the following error message:
Operands to the || and && operators must be convertible to logical scalar values.
Error in interp (line 44)
if l < 1 || r < 1 || cutoff <= 0 || cutoff > 1
Error in just_testing_2 (line 10)
yj=interp(xi,y2,xj)
interp1() not interp()
x=[0:0.5:20]
y=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
xi=0:0.25:20
yi=interp1(x,y,xi);
%x1=[8:0.001:12]
x1=[8:0.5:12]
%y2=[0.85:-0.000425:-0.85]
y2=[0.85:-0.2125:-0.85]
yj=interp1(x1,y2,xi)
plot(x,y,'o',xi,yi,x1,y2,xi,yj,'o')
I got this now, but it's not really possible to substract the variables this way. what do I need to change to get the same number of elements and how can I substract them?
plot(xi, yi - yj)
You interpolated over the same x values, xi, so there must be the same number of elements between the two.
Locks
Locks el 1 de Abr. de 2013
thanks
is there a way to include also the other plots I have above?
hold on
after the first plot.

Iniciar sesión para comentar.

Locks
Locks el 2 de Abr. de 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

2 comentarios

Walter Roberson
Walter Roberson el 2 de Abr. de 2013
Editada: Walter Roberson el 2 de Abr. de 2013
As discussed in http://www.mathworks.co.uk/matlabcentral/answers/69325-plot-the-slope-of-a-curve-in-the-same-plot#answer_80575 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?
Locks
Locks el 2 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Preguntada:

el 1 de Abr. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by