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


沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...