plot the slope of a curve in the same plot

12 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by