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?
답변 (2개)
Walter Roberson
2013년 4월 1일
0 개 추천
Use interp1() to produce values with a common x base so that you can subtract meaningfully.
댓글 수: 10
Locks
2013년 4월 1일
Image Analyst
2013년 4월 1일
편집: Image Analyst
2013년 4월 1일
You have to accept the output of interp1() into a variable and then use that variable, plus pass it the proper variables of course.
Locks
2013년 4월 1일
Walter Roberson
2013년 4월 1일
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.
Locks
2013년 4월 1일
Walter Roberson
2013년 4월 1일
interp1() not interp()
Locks
2013년 4월 1일
Walter Roberson
2013년 4월 1일
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
2013년 4월 1일
Walter Roberson
2013년 4월 2일
hold on
after the first plot.
Locks
2013년 4월 2일
0 개 추천
댓글 수: 2
Walter Roberson
2013년 4월 2일
편집: Walter Roberson
2013년 4월 2일
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
2013년 4월 2일
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!