Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
tangent line from curve graph

Subject: tangent line from curve graph

From: Stephen

Date: 22 Feb, 2009 00:19:01

Message: 1 of 6

hi friends,
i need your help. I wish to do tangent line from curve graph plotted.
I have data for P at time,t. Then, i plot the graph of P against t which shows it is curve line. By using only this graph, how can i do tangent line at specific t in order to obtain value for dP/dt?? In addition, no equation for P is available.

Thanks in advance.

Subject: tangent line from curve graph

From: Matt Fig

Date: 22 Feb, 2009 00:34:02

Message: 2 of 6

At point n, you could find the forward difference or the backward difference or take a secant from n-1 to n+1. For example:

dy/dx ~ [P(n+1) - P(n)] / [(n+1) - n]

Alternatively, you could fit a polynomial or spline and take it's derivative at that point. There are many ways to approximate the derivative.

Subject: tangent line from curve graph

From: Stephen

Date: 22 Feb, 2009 02:22:02

Message: 3 of 6

Thanks for response, Matt Fig.
I really not good in matlab, so can u explain a little bit about forward difference, backward difference and secant. I'm not really understand.
I already tried spline, but seems it shows the values of P, not dP/dt.
Maybe i wrong in giving information earlier but what i wish to obtain is the value of dP/dt at specified t and in my mind, this value can be obtained by finding tangent line (or slope) at that specified t.
Meaning that, the tangent (or slope) should represents as dP/dt.

Sorry,if i already misunderstood your opinion.

Subject: tangent line from curve graph

From: Matt Fig

Date: 22 Feb, 2009 03:16:01

Message: 4 of 6

"Stephen " <mulan_nuri@yahoo.com> wrote in message <gnqcsa$cnb$1@fred.mathworks.com>...
> Thanks for response, Matt Fig.
> I really not good in matlab, so can u explain a little bit about forward difference, backward difference and secant. I'm not really understand.


Those aren't terms specific to Matlab. I was talking about numerical derivative approximations. See this link:
http://en.wikipedia.org/wiki/Numerical_differentiation
For example, say we have two vectors:
x = 0:.1:pi;
y = sin(x); % Pretend we don't know it is sin(x) for arguments sake.
idx = find(x==1) % We will find the derivative at x = 1;
Because the slope is decreasing at x=1, the forward difference will underestimate the derivative:
(y(idx+1)-y(idx))/(x(idx+1)-x(idx))
while the backward difference will overestimate the derivative:
(y(idx)-y(idx-1))/(x(idx)-x(idx-1))
The secant difference is the average of these:
(y(idx+1)-y(idx-1))/(x(idx+1)-x(idx-1))
Compare these estimates with the true derivative:
cos(1)

> I already tried spline, but seems it shows the values of P, not dP/dt.
> Maybe i wrong in giving information earlier but what i wish to obtain is the value of dP/dt at specified t and in my mind, this value can be obtained by finding tangent line (or slope) at that specified t.
> Meaning that, the tangent (or slope) should represents as dP/dt.
>
> Sorry,if i already misunderstood your opinion.


The idea with the polynomial fit is that it is easy to take the derivative of a polynomial. You fit a polynomial to the data, over some region of interest, then take the derivative of the polynomial. If the fit is very good, the derivative should be o.k. Things can go wrong with this method too, that is why numerical differentiation is tricky.

Subject: tangent line from curve graph

From: Roger Stafford

Date: 22 Feb, 2009 04:50:18

Message: 5 of 6

"Stephen " <mulan_nuri@yahoo.com> wrote in message <gnq5ll$bih$1@fred.mathworks.com>...
> hi friends,
> i need your help. I wish to do tangent line from curve graph plotted.
> I have data for P at time,t. Then, i plot the graph of P against t which shows it is curve line. By using only this graph, how can i do tangent line at specific t in order to obtain value for dP/dt?? In addition, no equation for P is available.
>
> Thanks in advance.

  If you are willing to do additional computation, you can approximate the derivative (slope) at each point with the following second order differencing. Suppose t and p are n-element row vectors. (The stuff below can easily be adjusted for column vectors.) Then do this:

 td = [t(3),t(1:n-1)]; tu = [t(2:n),t(n-2)];
 pd = [p(3),p(1:n-1)]; pu = [p(2:n),p(n-2)];
 dpdt = ((pu-p)./(tu-t).*(t-td)+(p-pd)./(t-td).*(tu-t))./(tu-td);

Then dpdt will be an n-element row vector of corresponding derivative approximations of dp/dt. The fact that it is a second order approximation means that if you make p any linear or quadratic function of t, the quantity dpdt is an exact derivative at each of the points. Note that the t-intervals need not be equally-spaced for this to be true. Note also that this property holds even for the two endpoints.

  There are also some higher order derivative approximations on the file exchange, probably listed under the term "gradient".

Roger Stafford

Subject: tangent line from curve graph

From: Stephen

Date: 22 Feb, 2009 09:46:02

Message: 6 of 6

Thanks to Matt Fig and Roger Stafford.
You have give me some valuable ideas on what should i do. just i need to further study about that. I have studied before but already forgotten. ;P

Thanks again!!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us