如何编程实现一个基本的微分器
接此;
https://blog.csdn.net/bcbobo21cn/article/details/106674597
? ? 假設要自己編程實現Matlab仿真功能;那么,必然要實現一個一個的仿真組件;就是可拖動到仿真窗口的一個一個小方塊組件;有共用
基本庫;還有進一步的具體庫;
? ? 其中每一個組件,其實質是實現一種數學運算;目前這塊沒聽說有類庫之類的可調用的組件開發出來;
? ? 先來看一個最最基本問題;如果實現一些基本組件的話,一般的數學和邏輯運算,原理還是不難,要具體編程實現一個個組件然后可連
線運算,初想一下巨復雜;
? ? 基本加減乘除和邏輯與或非原理還是好理解;
? ? 但是仿真必然包含的基本組件有積分器和微分器;就像學電路原理時,里面都有積分和微分組件;
? ? 我們知道;常用基本函數的微分和積分都有固定公式;先看微分器;
? ? sin(x)的微分是cos(x);x平方的微分是2x,x^2 = 2x;斜線的微分是一根橫線,y=3+4t,微分是,y=3;
? ? 仿真組件就要實現一個微分器;如何編程實現一個微分器呢?
? ? 這個并不是按前面公式來的;一個電路的微分器來說,它輸入端有一個輸入,瞬間同時,輸出端就有一個值;如果輸入端的序列是sin
(x),輸出端的序列自然就是cos(x);如果輸入端的序列是x^2,輸出端的序列自然就是2*x;如果輸入端的序列是x^3,輸出端的序列自然就
是3*x^2;如果輸入端的序列是一根斜線,輸出端的序列自然就是一根橫線;
? ? 并不能等到輸入端的值都收集齊,判斷這是一個正弦序列,然后再在輸出端輸出一個余弦序列;
? ? 就是不能按初等函數的微積分公式做這玩意;得按學微積分時 dy/dx 類似的那一套來;這塊上學的時候好像聽懂的人不多,都辜負了教授;
? ? 或者按數值微分的計算方法,如下圖的公式來;或者按微積分數值計算方法,算來的也是一樣;
? ? 下面來看一下初步的編程實現微分器的思路,并用mschart控件繪制曲線驗證一下;
每個窗體使用2個mschart控件;第一個控件繪制一條數學曲線;然后按數值微分的計算方法對第一條曲線計算微分,結果繪制第二條曲線;看第二條曲線是否是第一條曲線的微分;
按數值微分的含義,一個函數某點的微分,等于 (y值增量-y值) / x變化量;程序里面直接算Y值增量,X變化量繪制到控件上都是1;然后把各點的微分值連接起來;Y縱軸,X橫軸;
看sin(x)的微分;代碼如下;
private double pi = 3.14159265;......chart1.Series["Series1"].ChartType=SeriesChartType.Line;chart1.Series["Series1"].BorderWidth = 3;chart1.Series["Series1"].Points.AddY(Math.Sin(0));chart1.Series["Series1"].Points.AddY(Math.Sin(pi/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*2/8)); chart1.Series["Series1"].Points.AddY(Math.Sin(pi*3/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*4/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*5/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*6/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*7/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*8/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*9/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*10/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*11/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*12/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*13/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*14/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*15/8));chart1.Series["Series1"].Points.AddY(Math.Sin(pi*16/8));chart2.Series["Series1"].ChartType=SeriesChartType.Line;chart2.Series["Series1"].BorderWidth = 3;double d1 = Math.Sin(pi/8) - Math.Sin(0);double d2 = Math.Sin(pi*2/8) - Math.Sin(pi/8);double d3 = Math.Sin(pi*3/8) - Math.Sin(pi*2/8);double d4 = Math.Sin(pi*4/8) - Math.Sin(pi*3/8);double d5 = Math.Sin(pi*5/8) - Math.Sin(pi*4/8);double d6 = Math.Sin(pi*6/8) - Math.Sin(pi*5/8);double d7 = Math.Sin(pi*7/8) - Math.Sin(pi*6/8);double d8 = Math.Sin(pi*8/8) - Math.Sin(pi*7/8);double d9 = Math.Sin(pi*9/8) - Math.Sin(pi*8/8);double d10 = Math.Sin(pi*10/8) - Math.Sin(pi*9/8);double d11 = Math.Sin(pi*11/8) - Math.Sin(pi*10/8);double d12 = Math.Sin(pi*12/8) - Math.Sin(pi*11/8);double d13 = Math.Sin(pi*13/8) - Math.Sin(pi*12/8);double d14 = Math.Sin(pi*14/8) - Math.Sin(pi*13/8);double d15 = Math.Sin(pi*15/8) - Math.Sin(pi*14/8);double d16 = Math.Sin(pi*16/8) - Math.Sin(pi*15/8);chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d2);chart2.Series["Series1"].Points.AddY(d3);chart2.Series["Series1"].Points.AddY(d4);chart2.Series["Series1"].Points.AddY(d5);chart2.Series["Series1"].Points.AddY(d6);chart2.Series["Series1"].Points.AddY(d7);chart2.Series["Series1"].Points.AddY(d8);chart2.Series["Series1"].Points.AddY(d9);chart2.Series["Series1"].Points.AddY(d10);chart2.Series["Series1"].Points.AddY(d11);chart2.Series["Series1"].Points.AddY(d12);chart2.Series["Series1"].Points.AddY(d13);chart2.Series["Series1"].Points.AddY(d14);chart2.Series["Series1"].Points.AddY(d15);y=3+3t的微分;t橫軸;
chart1.Series["Series1"].ChartType=SeriesChartType.Line;chart1.Series["Series1"].BorderWidth = 3;chart1.Series["Series1"].Points.AddY(0);chart1.Series["Series1"].Points.AddY(3);chart1.Series["Series1"].Points.AddY(6);chart1.Series["Series1"].Points.AddY(9);chart1.Series["Series1"].Points.AddY(12);chart1.Series["Series1"].Points.AddY(15);chart1.Series["Series1"].Points.AddY(18);chart1.Series["Series1"].Points.AddY(21);chart1.Series["Series1"].Points.AddY(24);chart1.Series["Series1"].Points.AddY(27);chart1.Series["Series1"].Points.AddY(30);chart1.Series["Series1"].Points.AddY(33);chart1.Series["Series1"].Points.AddY(36);chart1.Series["Series1"].Points.AddY(39);chart1.Series["Series1"].Points.AddY(42);chart1.Series["Series1"].Points.AddY(45);chart1.Series["Series1"].Points.AddY(48);double d1 = 3-0;double d2 = 6-3;double d3 = 9-6;double d4 = 12-9;double d5 = 15-12;double d6 = 18-15;double d7 = 21-18;double d8 = 24-21;double d9 = 27-24;double d10 = 30-27;double d11 = 33-30;double d12 = 36-33;double d13 = 39-36;double d14 = 42-39;double d15 = 45-42;double d16 = 48-45;chart2.Series["Series1"].ChartType=SeriesChartType.Line;chart2.Series["Series1"].BorderWidth = 3;chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d2);chart2.Series["Series1"].Points.AddY(d3);chart2.Series["Series1"].Points.AddY(d4);chart2.Series["Series1"].Points.AddY(d5);chart2.Series["Series1"].Points.AddY(d6);chart2.Series["Series1"].Points.AddY(d7);chart2.Series["Series1"].Points.AddY(d8);chart2.Series["Series1"].Points.AddY(d9);chart2.Series["Series1"].Points.AddY(d10);chart2.Series["Series1"].Points.AddY(d11);chart2.Series["Series1"].Points.AddY(d12);chart2.Series["Series1"].Points.AddY(d13);chart2.Series["Series1"].Points.AddY(d14);chart2.Series["Series1"].Points.AddY(d15);chart2.Series["Series1"].Points.AddY(d16);y=x^2的微分;按初等函數微分公式是2x;看下按數值微分思路算出來的情況;
chart1.Series["Series1"].ChartType=SeriesChartType.Line;chart1.Series["Series1"].BorderWidth = 3;chart1.Series["Series1"].Points.AddY(0);chart1.Series["Series1"].Points.AddY(2);chart1.Series["Series1"].Points.AddY(4);chart1.Series["Series1"].Points.AddY(9);chart1.Series["Series1"].Points.AddY(16);chart1.Series["Series1"].Points.AddY(25);chart1.Series["Series1"].Points.AddY(36);chart1.Series["Series1"].Points.AddY(49);chart1.Series["Series1"].Points.AddY(64);chart1.Series["Series1"].Points.AddY(81);chart1.Series["Series1"].Points.AddY(100);chart1.Series["Series1"].Points.AddY(121);chart1.Series["Series1"].Points.AddY(144);chart1.Series["Series1"].Points.AddY(169);chart1.Series["Series1"].Points.AddY(196);chart1.Series["Series1"].Points.AddY(225);chart1.Series["Series1"].Points.AddY(256);double d1 = 2-0;double d2 = 4-2;double d3 = 9-4;double d4 = 16-9;double d5 = 25-16;double d6 = 36-25;double d7 = 49-36;double d8 = 64-49;double d9 = 81-64;double d10 = 100-81;double d11 = 121-100;double d12 = 144-121;double d13 = 169-144;double d14 = 196-169;double d15 = 225-196;double d16 = 256-225;chart2.Series["Series1"].ChartType=SeriesChartType.Line;chart2.Series["Series1"].BorderWidth = 3;chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d1);chart2.Series["Series1"].Points.AddY(d2);chart2.Series["Series1"].Points.AddY(d3);chart2.Series["Series1"].Points.AddY(d4);chart2.Series["Series1"].Points.AddY(d5);chart2.Series["Series1"].Points.AddY(d6);chart2.Series["Series1"].Points.AddY(d7);chart2.Series["Series1"].Points.AddY(d8);chart2.Series["Series1"].Points.AddY(d9);chart2.Series["Series1"].Points.AddY(d10);chart2.Series["Series1"].Points.AddY(d11);chart2.Series["Series1"].Points.AddY(d12);chart2.Series["Series1"].Points.AddY(d13);chart2.Series["Series1"].Points.AddY(d14);chart2.Series["Series1"].Points.AddY(d15);chart2.Series["Series1"].Points.AddY(d16);? ? 到此;從曲線看,計算數值微分的基本思路是沒有問題的;第一個點有點問題,可能是第一個點算的不對,也可能是控件的使用有點問題;
把上面思路理清,做出一個通用數值微分計算函數,一個基本的微分器就實現了;或者完全理解一種數值微分的計算方法,做一個通用函數也一樣;
總結
以上是生活随笔為你收集整理的如何编程实现一个基本的微分器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 驱动开发头文件wdfch
- 下一篇: 在共享DLL中使用MFC 和在静态库中使