![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
いろいろなテクニック.Nucleo と DISCO-F746 用.
Dependencies: Array_Matrix mbed
FunctionObject.hpp@1:bbb9f0c3e523, 2019-04-10 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Apr 10 06:32:33 2019 +0000
- Revision:
- 1:bbb9f0c3e523
- Parent:
- 0:bb939e0bc6e2
2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:bb939e0bc6e2 | 1 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 2 | class Func |
MikamiUitOpen | 0:bb939e0bc6e2 | 3 | { |
MikamiUitOpen | 0:bb939e0bc6e2 | 4 | public: |
MikamiUitOpen | 0:bb939e0bc6e2 | 5 | virtual ~Func() {} |
MikamiUitOpen | 0:bb939e0bc6e2 | 6 | virtual float operator()(float) = 0; |
MikamiUitOpen | 0:bb939e0bc6e2 | 7 | }; |
MikamiUitOpen | 0:bb939e0bc6e2 | 8 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 9 | float Integrate(Func &f, float a, float b) |
MikamiUitOpen | 0:bb939e0bc6e2 | 10 | { |
MikamiUitOpen | 0:bb939e0bc6e2 | 11 | const int N = 1000; |
MikamiUitOpen | 0:bb939e0bc6e2 | 12 | float step = (b - a)/N; |
MikamiUitOpen | 0:bb939e0bc6e2 | 13 | float s = 0.0f; |
MikamiUitOpen | 0:bb939e0bc6e2 | 14 | while(a < b) |
MikamiUitOpen | 0:bb939e0bc6e2 | 15 | { |
MikamiUitOpen | 0:bb939e0bc6e2 | 16 | s += f(a)*step; |
MikamiUitOpen | 0:bb939e0bc6e2 | 17 | a += step; |
MikamiUitOpen | 0:bb939e0bc6e2 | 18 | } |
MikamiUitOpen | 0:bb939e0bc6e2 | 19 | return s; |
MikamiUitOpen | 0:bb939e0bc6e2 | 20 | } |
MikamiUitOpen | 0:bb939e0bc6e2 | 21 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 22 | float aFunc1(float x) { return x; } |
MikamiUitOpen | 0:bb939e0bc6e2 | 23 | float aFunc2(float x) { return x*x; } |
MikamiUitOpen | 0:bb939e0bc6e2 | 24 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 25 | class DerivFunc: public Func |
MikamiUitOpen | 0:bb939e0bc6e2 | 26 | { |
MikamiUitOpen | 0:bb939e0bc6e2 | 27 | public: |
MikamiUitOpen | 0:bb939e0bc6e2 | 28 | DerivFunc(float (*f)(float)) : f_(f) {} |
MikamiUitOpen | 0:bb939e0bc6e2 | 29 | virtual float operator()(float d) { return f_(d); } |
MikamiUitOpen | 0:bb939e0bc6e2 | 30 | private: |
MikamiUitOpen | 0:bb939e0bc6e2 | 31 | float (*f_)(float); |
MikamiUitOpen | 0:bb939e0bc6e2 | 32 | }; |
MikamiUitOpen | 0:bb939e0bc6e2 | 33 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 34 | void MyFunctionObject() |
MikamiUitOpen | 0:bb939e0bc6e2 | 35 | { |
MikamiUitOpen | 0:bb939e0bc6e2 | 36 | DerivFunc fx1(aFunc1); |
MikamiUitOpen | 1:bbb9f0c3e523 | 37 | float area = Integrate(fx1, 0.0, 1.0); // y = x の 区間 [0, 1] の定積分 |
MikamiUitOpen | 0:bb939e0bc6e2 | 38 | printf("\r\nArea = %f\r\n", area); |
MikamiUitOpen | 0:bb939e0bc6e2 | 39 | |
MikamiUitOpen | 0:bb939e0bc6e2 | 40 | DerivFunc fx2(aFunc2); |
MikamiUitOpen | 1:bbb9f0c3e523 | 41 | area = Integrate(fx2, 0.0, 1.0); // y = x*x の 区間 [0, 1] の定積分 |
MikamiUitOpen | 0:bb939e0bc6e2 | 42 | printf("Area = %f\r\n", area); |
MikamiUitOpen | 0:bb939e0bc6e2 | 43 | } |