Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <fstream>
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- #include <ctype.h>
- using namespace std;
- template<typename T>
- using func = T(*)(T, T);
- template <typename T>
- T fun(T x) {
- return cos(x / 2.);
- }
- template <typename T>
- T poch(T x) {
- return -0.5 * sin(x / 2.);
- }
- template <typename T>
- T progresywna(T h, T x) {
- return (fun(x + h) - fun(x)) / h;
- }
- template <typename T>
- T wsteczna(T h, T x) {
- return (fun(x) - fun(x - h)) / h;
- }
- template <typename T>
- T centralna(T h, T x) {
- return (fun(x + h) - fun(x - h)) / (2 * h);
- }
- template <typename T>
- T x0_3pkt(T h, T x) {
- return (-1.5*fun(x) + 2 * fun(x + h) - 0.5*fun(x + 2 * h)) / h;
- }
- template <typename T>
- T xn_3pkt(T h, T x) {
- return (0.5*fun(x - 2 * h) - 2 * fun(x - h) + 1.5*fun(x)) / h;
- }
- template <typename T>
- void plot(string name, T x, func<T> f) {
- T h = 0.2;
- ofstream outfile(name);
- for (int i = 0; i < 100; i++) {
- outfile << log10(h) << " " << log10(fabs(poch(x) - f(h, x))) << endl;
- h /= 2.0;
- }
- outfile.close();
- }
- template <typename T>
- void make_plot(T x0, T xn, T xc) {
- string name = typeid(T).name();
- plot(name + "1.txt", x0, progresywna);
- plot(name + "2.txt", x0, x0_3pkt);
- plot(name + "3.txt", xn, wsteczna);
- plot(name + "4.txt", xn, xn_3pkt);
- plot(name + "5.txt", xc, progresywna);
- plot(name + "6.txt", xc, wsteczna);
- plot(name + "7.txt", xc, centralna);
- }
- int main() {
- make_plot<double>(0, M_PI, M_PI / 2);
- make_plot<float>(0, M_PI, M_PI / 2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement