plot the slope of a curve in the same plot

24 ビュー (過去 30 日間)
Locks
Locks 2013 年 4 月 1 日
hi,
the code below gives me the value of an option depending on several stock prices, in the end it's an cruve that goes from bottom left to top right.
x=[0:0.01:20]
y=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
plot(x,y)
xlabel('Stock Price ($)');
ylabel('Gain/Loss');
axis([8 12 -0.5 2]);
How can I plot the slope of that curve, let's say the slope at point (10/0) into the image with the curve?

採用された回答

Walter Roberson
Walter Roberson 2013 年 4 月 1 日
You cannot plot the real slope there unless you have the formula for the curve. Otherwise you can only approximate the slope.
x = 10 occurs at x(101). You can calculate a slope (y(101)-y(100)) / (x(101)-x(100)) and another slope (y(102)-y(101)) / (x(102)-x(101)) (the "left" and "right" slope, one perhaps might say.) However, one cannot say with any certainty that the "real" slope is between the two: the "real" slope could be anything.
Imagine putting pegs in at (x(100),y(100)) and (x(101),y(101)) and (x(102),y(102)), and imagine attaching an elastic band to the first and third of those, and having the elastic band go "under" the middle one. Now reach between the first and second and stretch the band upwards. As it is still pinned at the two ends and blocked from moving upwards at the middle, the fixed points it goes through will not change, but clearly the slope to the left can vary considerably depending on how tightly one stretches it. By symmetry one can do the same thing on the right instead of the left. Now move the elastic to go "above" the middle peg and pull downwards on the elastic, first on the left and then on the right; the possible slopes can thus be all the negatives of the possible slopes from pulling upwards. We have thus established that the "real" slope at the middle point can vary arbitrarily in either direction, without moving any of the points.
So, now what? You know the points, but without the formula you don't know the slope at the center point.
You can make arbitrary guesses such as averaging the left and right slopes, or calculating the perpendicular to the resultant vector of both sides aimed "towards" the center.
Once you have some guess at the slope, then knowing the fixed point on it, you can construct a simple y = m*x + b formula and plot that some distance before and after the actual point.
  3 件のコメント
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
Pick an arbitrary slope such as 2. Plot it through (x(101),y(101)) for as far before and after as you want. Unless someone knows the formula for blsprice(), they will be unable to prove that that arbitrary slope is wrong.
Locks
Locks 2013 年 4 月 1 日
thanks, I did something similar as you've proposed and I have an additional question posted here:
do you have any idea how to do that?

サインインしてコメントする。

その他の回答 (1 件)

Anand
Anand 2013 年 4 月 1 日
Add this at the end of your code snippet:
dy = gradient(y,.01);
hold on;
plot(x,dy,'r');
  1 件のコメント
Locks
Locks 2013 年 4 月 1 日
that code:
x=[0:0.01:20]
y=blsprice(x,10,0.02,0.2,0.2)-blsprice(10,10,0.02,0.2,0.2)
plot(x,y)
xlabel('Stock Price ($)');
ylabel('Gain/Loss');
axis([8 12 -0.5 2]);
dy = gradient(y,.01);
hold on;
plot(x,dy,'r');
doesn't give me the slope but another curve

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by