2018年12月23日 星期日

DEV C++ 求 f(x)=log e (x) 之值

DEV C++ 求 f(x)=log e (x) 之值

使用c++ Code 




// C++ program for implementation of Lagrange's Interpolation
#include<bits/stdc++.h>
using namespace std;

// To represent a data point corresponding to x and y = f(x)
struct Data
{
    double x, y;
};

// function to interpolate the given data points using Lagrange's formula
// xi corresponds to the new data point whose value is to be obtained
// n represents the number of known data points
double interpolate(Data f[], double xi, int n)
{
    double result = 0; // Initialize result

    for (int i=0; i<n; i++)
    {
        // Compute individual terms of above formula
        //double term = f[i].y;
        double term = 1;
     
        for (int j=0;j<n;j++)
        {
            if (j!=i)
                term = term*(xi - f[j].x)/double(f[i].x - f[j].x);
        }

        // Add current term to result
        result = result+term*f[i].y;
    }

    return result;
}

// driver function to check the program
int main()
{
    // creating an array of 4 known data points
    Data f[] = {{1,0}, {2,0.693}, {3,1.099}, {4,1.386}};

    // Using the interpolate function to obtain a data point
    // corresponding to x=1.5
    cout << "Value of f(1.5) is : " << interpolate(f, 1.5, 4);

    return 0;
}




//=====================================================

// driver function to check the program
int main()
{
    // creating an array of 4 known data points
    Data f[] = {{1,0}, {1.2,0.182}, {1.4,0.336}, {2.0,0.693}}; 

    // Using the interpolate function to obtain a data point
    // corresponding to x=1.5
    cout << "Value of f(1.5) is : " << interpolate(f, 1.5, 4);

    return 0;
}


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...