Home > calculus and analysis > Derivative from the left and right

# Derivative from the left and right

June 4Hits:1

I am currently trying to write a script that takes a curve $C$, a starting point $p$ and a direction vector $\vec{v}$ and calculates the path of a beam starting at $p$ in direction $\vec{v}$ when it is reflected on $C$.

The idea is basically: (Numerically) find the (first) crossing point of beam and $C$, calculate tangent in that point, calculate new beam direction, repeat.

Now, if my curve is point-wise not differentiable (in my case: $C$ has "kinks"), I would like to treat it as a limit case where both smooth curves around the kinks are considered, so the calculation forks and I get 2 possible beam pathes. For this, I need the left and the right side derivative of this point. How do I get those in Mathematica?

Also, if somebody knows a better way or this has already been written and is public, I appreciate any clues, of course.

The NumericalCalculus Package already treats this:

Use Scale to specify directional derivatives.

Needs["NumericalCalculus"]
f[x_] := TriangleWave[x];
ND[f[x], {x, 1}, 3/4, Scale -> -1]
ND[f[x], {x, 1}, 3/4, Scale -> 1]
(*
-> -3.99999
3.99999
*)



If your kinked curve is a function that is known to Mathematica or is entirely in terms of functions known to Mathematica, you can go back to using the definition of left and right derivatives, and use Limit[] for the purpose, along with its Direction option:

Plot[TriangleWave[x], {x, 0, 2},
Epilog -> {AbsolutePointSize[5], Point[{1/4, TriangleWave[1/4]}]}]



(* coming from the left *)
Limit[(TriangleWave[1/4 + h] - TriangleWave[1/4])/h, h -> 0, Direction -> 1]
4

(* coming from the right *)
Limit[(TriangleWave[1/4 + h] - TriangleWave[1/4])/h, h -> 0, Direction -> -1]
-4



If your function is an InterpolatingFunction[]` or some other black-box function that can only take numerical arguments, different approaches are needed. If that's what you're interested in, I'll edit this answer later for a possible strategy for those.

## Related Articles

• ### How to call inherited function from derived class if derived class inherit template base class with class of derived? January 30

Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable s

• ### How to call pure virtual member function from baseclass that is implemented in derived class? June 7

Suppose I have two C++ classes: class A { public: A() { fn(); } virtual void fn() { _n = 1; } int getn() { return _n; } protected: int _n; }; class B : public A { public: B() : A() {} virtual void fn() { _n = 2; } }; If I write the following code: ma

• ### What does derivative work mean in the context of websites?July 15

If a website uses pictures on some pages that are under cc-by-sa. The images have been modified slightly to fit into the page. What is the derivative work? only the images themselves only the part of the page that uses the image (i.e article copy, bu

• ### Can I trust that every Debian-derived distro will have a 'base-files' package that contains '/etc/debian_version'?October 5

What I want is a quick, reliable command to ascertain that the Linux I'm running in is Debian-based. The following command seems to work. It also returns the Debian version: dpkg -L base-files 2>/dev/null | grep '^/etc/debian_version\$' >/dev/null 2&

• ### WIM2VHD failing with "Cannot derive Volume GUID from mount point."November 2

I'm trying to use WIM2VHD according to the instructions on Scott Hanselman's blog post to create a Sysprepped VHD image to boot from. I've installed the WAIK, and I have my Windows 7 sources mounted as a virtual drive. When I try to run WIM2VHD like

• ### every derived table must have its own aliasDecember 11

I am running this query on MySQL SELECT ID FROM ( SELECT ID, msisdn FROM ( SELECT * FROM TT2 ) ); and it is giving this error: Every derived table must have its own alias. What is wrong ? --------------Solutions------------- Every derived table must

• ### How to correctly use DERIVE or COUNTER in munin pluginsMarch 30

I'm using munin to monitor my server. I've been able to write plugins for it, but only if the graph type is GAUGE. When I try COUNTER or DERIVE, no data is logged or graphed. The plugin i'm currently stuck on is for monitoring bandwidth usage, and is

• ### c++, calling base class protected method inside derived class July 14

I ran into an error yesterday and, while it's easy to get around, I wanted to make sure that I'm understanding C++ right. I have a base class with a protected member: class Base { protected: int b; public: void DoSomething(const Base& that) { b+=that

• ### can not access protected members of base class in derived class functions through a new instance of base class July 14

I ran into an error yesterday and, while it's easy to get around, I wanted to make sure that I'm understanding C++ right. I have a base class with a protected member: class Base { protected: int b; public: void DoSomething(const Base& that) { b+=that

• ### Camparison of Official Derivatives August 3

What different Ubuntu derivatives are there? And what is special with them? --------------Solutions------------- The main two differences between Ubuntu and its derivatives are the backing and the default installed packages. For all official derivati

• ### C++ cannot convert from base A to derived type B via virtual base ASeptember 19

I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual bas

• ### Scaling PID (Proportional Integral Derivative) OutputOctober 12

I have implemented a PID function using the formula, correction = Kp * error + Kd * (error - prevError) + kI * (sum of errors) What should I do to keep my output between a certain range? say 0-255 If I disregard any value not between 0 to 255 it prod

• ### Will there be a derivative with GNOME 3?October 31

I just wonder, will there be a separate version with GNOME desktop (GNObuntu/Gbuntu or something)? I think there are many users including me who like GNOME. Please don't take GNOME away from us. --------------Solutions------------- Unity is simply an

• ### Will there be a derivative with upstream GNOME 3.0?November 16

I want to ask if we're gonna see a version of Ubuntu such as Kubuntu for example but with GNOME shell and not with Unity ? A version called something like Gubuntu. Thanks in advance for your answers :) --------------Solutions------------- Such a rele

• ### Lubuntu as an official derivative of UbuntuDecember 19

The four main desktop environments are GNOME, KDE, XFCE and LXDE. While Kubuntu and Xubuntu are considered official derivatives of Ubuntu, Lubuntu is not. Why is that so? Update: Lubuntu has gained official status in 11.10. --------------Solutions---

• ### How do you derive the passphrase from hexadecimal wep key?January 19

I have a hexadecimal WEP key in the setting of my router. When I try to connect, the system asks me for the WEP passphrase. How can I derive it from my hexadecimal? This is what I have: 84:12:7D:13:4D:08:31:89:A4:AF:97:07:21 What is the way to get it

• ### What is the advantage of a Z transform derived PID implemenation?January 21

I've seen many PID articles, such as this, use a Z transform of the generic PID equation to derive some crazy difference equation which can then be implemented in software (or in this case an FPGA). My question is, what is the advantage to such an im

• ### TEA algorithm constant 0x9e3779b9 said to be derived from golden ratio, but the golden number is 1.618?March 30

I'm trying to understand a constant 0x9e3779b9. What kind of data is this? It's not binary, not decimal, what is this? It's a constant used on the TEA algorithm. It says it's derived from the golden number, but the golden number is 1.618? -----------

• ### Checking whether a password is derived from previous passwords April 19

In some environments, it is required that users change a certain number of characters every time they create a new password. This is of course to prevent passwords from being easily-guessable, especially with knowledge of old passwords such as a depa

• ### How much code does one have to derive from another source before the license rules are applicable?April 20

In an attempt to not re-invent the wheel, I've been working on a CMS's text-sanitization layer. I found that SimplePie had a really good one in use with it's RSS feed, but it had a lot of features that I didn't need (such as image cache-ing, making r