2018年12月24日 星期一

online_c++_compiler

https://www.onlinegdb.com/online_c++_compiler

// CPP program for implementing
// Newton divided difference formula
#include <bits/stdc++.h>
using namespace std;

// Function to find the product term
float proterm(int i, float value, float x[])
{
 float pro = 1;
 for (int j = 0; j < i; j++) {
  pro = pro * (value - x[j]);
 }
 return pro;
}

// Function for calculating
// divided difference table
void dividedDiffTable(float x[], float y[][10], int n)
{
 for (int i = 1; i < n; i++)
 {
    for (int j = 0; j < n - i; j++)
   y[j][i] = (y[j][i - 1] - y[j + 1] [i - 1]) / (x[j] - x[i + j]);
 }
}

// Function for applying Newton's
// divided difference formula
float applyFormula(float value, float x[],
    float y[][10], int n)
{
 float sum = y[0][0];

 for (int i = 1; i < n; i++) {
 sum = sum + (proterm(i, value, x) * y[0][i]);
 }
 return sum;
}

// Function for displaying
// divided difference table
void printDiffTable(float y[][10],int n)
{
 for (int i = 0; i < n; i++) {
  for (int j = 0; j < n - i; j++) {
   cout << setprecision(4) <<
        y[i][j] << "\t ";
  }
  cout << "\n";
 }
}

// Driver Function
int main()
{
 // number of inputs given
 int n = 6;
 float value, sum, y[10][10];
 float x[] = { 0, 0.1, 0.3, 0.6 , 1.0 , 1.1 };

 // y[][] is used for divided difference
 // table where y[][0] is used for input
 y[0][0] = -6;
 y[1][0] = -5.89483;
 y[2][0] = -5.65014;
 y[3][0] = -5.17788;
 y[4][0] = -4.28172;
 y[5][0] = -3.99583;


 // calculating divided difference table
 dividedDiffTable(x, y, n);

 // displaying divided difference table
 printDiffTable(y,n);

 // value to be interpolated
 value = 0.8;

 // printing the value
 cout << "\nValue at " << value << " is "
   << applyFormula(value, x, y, n) << endl;
 return 0;
}


-6       1.052   0.5725  0.215   0.06305         0.01413                                                                
-5.895   1.223   0.7015  0.278   0.07859                                                                                
-5.65    1.574   0.9517  0.3566                                                                                         
-5.178   2.24    1.237                                                                                                  
-4.282   2.859                                                                                                          
-3.996                                                                                                                  
                                                                                                                        
Value at 0.8 is -4.774                                                                                                  
                                                                                                                        
                                                                                                                        
...Program finished with exit code 0                                                                                    
Press ENTER to exit console.          


沒有留言:

張貼留言

ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中

  一個典型的 IoT(物聯網)與工業自動化系統整合(SCADA/機台連線) 的架構圖。它透過 Node-RED 作為核心資料網關,將前端 ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中。 1. 各模組功能拆解...