plot the slope of a curve in the same plot

13 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 1 Apr 2013
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 Commenti
Walter Roberson
Walter Roberson il 1 Apr 2013
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 il 1 Apr 2013
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?

Accedi per commentare.

Più risposte (1)

Anand
Anand il 1 Apr 2013
Add this at the end of your code snippet:
dy = gradient(y,.01);
hold on;
plot(x,dy,'r');
  1 Commento
Locks
Locks il 1 Apr 2013
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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by