1. 程式人生 > >用gnuplot畫出c產生資料的波形圖

用gnuplot畫出c產生資料的波形圖

文章目錄

資料

用c產生表示式為: s(t)=sin(pit)+2cos(pit)的資料,輸出為t跟s。

程式碼

#include<stdio.h>
#include<math.h>
#define pi 3.14
int main()
{
   double  s;
    for (int t=0; t<10; t++)//十秒的資料
        {

            s=sin(pi*t)+2*cos(pi*t);
            printf("%d\t%f\n",t,s);
        }
}

產生的資料是 0 2.000000 1 -1.998405 2 1.996805 3 -1.995199 4 1.993589 5 -1.991973 6 1.990353 7 -1.988727 8 1.987097 9 -1.985461

畫圖

1.要先在DOS開啟編譯的程式 結果如圖 在這裡插入圖片描述 先開啟的原因是因為之前用gnuplot找不到資料。 2.接著開啟gnuplot 3.輸入檔名plot “<1.exe” w l 過程 在這裡插入圖片描述 結果圖 在這裡插入圖片描述

問題

資料太少。

修正

程式碼為

#include<stdio.h>
#include<math.h>
#define pi 3.14
int main()
{
   double t,s;
    for (int i=0; i<8000; i++)//4秒,產生更多資料
        {
            t=i/2000.0;
            s=sin(pi*t)+cos(2*pi*t);
            printf("%e\t%e\n",t,s);
        }
}

結果圖 在這裡插入圖片描述