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


沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...