Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ หาเส้นสัมผัสที่จุดที่กำหนดบนโค้ง


สมมติว่าเรามีโค้งแบบ y = x(A - x) เราต้องหาเส้นสัมผัสที่จุดที่กำหนด (x,y) บนโค้งนั้น ที่นี่ A เป็นจำนวนเต็ม x และ y ก็เป็นจำนวนเต็มเช่นกัน

เพื่อแก้ปัญหานี้ เราต้องตรวจสอบว่าจุดที่กำหนดอยู่บนโค้งหรือไม่ ถ้าอยู่ ให้หาอนุพันธ์ของโค้งนั้น ซึ่งจะเป็น:

$$\frac{dy}{dx}=A-2x$$

จากนั้นแทน x และ y ลงใน dy/dx แล้วหาเส้นสัมผัสโดยใช้สมการนี้:

$$Y-y=-\left(\frac{dy}{dx}\right)*(X-x)$$

ตัวอย่างโค้ด C++

#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);
}

ผลลัพธ์

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