Find Tangent at a given point on the curve in C++


Suppose we have a curve like y = x(A - x), we have to find the tangent at a given point (x,y) on that curve. Here A is an integer number, x and y are also integers.

To solve this, we have the check that the given point is on the curve or not, if so, then find the differentiation of that curve, so it will be −

$$\frac{\text{d}y}{\text{d}x}=A-2x$$

Then put x and y into the dy/dx, then find the tangent using this equation −

$$Y-y=-\lgroup\frac{\text{d}y}{\text{d}x}\rgroup*\lgroup X-x \rgroup$$

Example

 Live Demo

#include<iostream>
using namespace std;
void getTangent(int A, int x, int y) {
   int differentiation = A - x * 2;
   if (y == (2 * x - x * x)) {
      if (differentiation < 0)
         cout << "y = " << differentiation << "x" << (x * differentiation) + (y);
      else if (differentiation > 0)
         cout << "y = " << differentiation << "x+" << -x * differentiation + y;
      else
         cout << "Not possible";
   }
}
int main() {
   int A = 2, x = 2, y = 0;
   cout << "Equation of tangent is: ";
   getTangent(A, x, y);
}

Output

Equation of tangent is: y = -2x-4

Updated on: 19-Dec-2019

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements