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_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...