2018年12月26日 星期三

利用online C語言 執行數值分析練習EX1-4.C

利用online C語言 執行數值分析練習
https://www.onlinegdb.com/online_python_compiler
設定C語言環境
輸入資料的設定
input --> Text --> Data

n=3
xa=1.5
1.0  0.0
2.0  0.693
3.0  1.099
4.0  1.386


Stdout 輸出畫面上檢視結果



/* ex1-4.c: Lagrange Interpolation Algorithm
 * Read in data file of ex1-4.dat which has n point values
 * and the value of interpolating point xa. Based on Lagrange
 * Interpolation algorithm to compute p(xa) and output its value.
 * (x[i],f[i]):given points and n+1 are number of points
 * Ln,k(x)=l=summation of (x-x[i])/(x[k]-x[i]).
 * p(x)=ff=L(x)*f(x[k])
 */
#include <stdio.h>
void main()
{
   double x[30],f[30],l,ff,xa;
   int i,k,n;
   scanf("n=%d xa=%lf",&n,&xa);
   for(k=0;k<=n;k++)
   {
      scanf("%lf %lf",&x[k],&f[k]);
   }
   ff=0.0;
   for(k=0;k<=n;k++)
   {
      l=1.0;
      for(i=0;i<=n;i++)
      {
if(i !=k)
{
    l=l*(xa-x[i])/(x[k]-x[i]);
}
      }
      ff=ff+l*f[k];
   }
      printf("The value of p(%.4lf)=%.4lf\n",xa,ff);
}

沒有留言:

張貼留言

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站 (ESP32 + DHT22 + 1602 LCD)

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站(溫度 濕度) (ESP32 + DHT22 + 1602 LCD) 想學物聯網(IoT)與 ESP32 電子實作,卻擔心接線與電壓規範搞砸嗎?透過強大的線上模擬器 Wokwi ,我們不需要花費任何硬體費用,打開瀏覽器...