C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_gnuplot.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk GNUPlot Example *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <iostream>
22#include <string>
23#include <fstream>
24#include <iomanip>
25
26#include "exprtk.hpp"
27
28
30{
31public:
32
34 : min_x_(0.0)
35 , max_x_(0.0)
36 , min_y_(0.0)
37 , max_y_(0.0)
38 {}
39
40 exprtk_gnuplot_fx& set_title(const std::string& title)
41 {
42 title_ = title;
43 return *this;
44 }
45
46 exprtk_gnuplot_fx& set_domain(const double min_x, const double max_x)
47 {
48 min_x_ = min_x;
49 max_x_ = max_x;
50 return *this;
51 }
52
53 exprtk_gnuplot_fx& set_expression(const std::string& expression)
54 {
55 expression_ = expression;
56 return *this;
57 }
58
59 bool plot()
60 {
61 if (!generate_data())
62 return false;
63 else
64 return generate_gp_script();
65 }
66
67private:
68
70 {
71 std::ofstream stream("plot.gp");
72
73 if (!stream)
74 {
75 return false;
76 }
77
78 stream << "set term png\n";
79 stream << "set output 'plot.png'\n";
80 stream << "set xrange[" << min_x_ << ":" << max_x_ <<"]\n";
81 stream << "set yrange[" << min_y_ << ":" << max_y_ <<"]\n";
82 stream << "set xzeroaxis\n";
83 stream << "set yzeroaxis\n";
84 stream << "plot 'data.dat' using 1:2:(1.0) smooth unique title '" << title_ <<"'\n";
85
86 return true;
87 }
88
90 {
91 typedef exprtk::symbol_table<double> symbol_table_t;
92 typedef exprtk::expression<double> expression_t;
93 typedef exprtk::parser<double> parser_t;
94
95 double x = 0.0;
96
97 symbol_table_t symbol_table;
98 symbol_table.add_constants();
99 symbol_table.add_variable("x",x);
100
101 expression_t expression;
102 expression.register_symbol_table(symbol_table);
103
104 parser_t parser;
105
106 if (!parser.compile(expression_,expression))
107 {
108 return false;
109 }
110
111 std::ofstream stream("data.dat");
112
113 if (!stream)
114 {
115 return false;
116 }
117
118 min_y_ = +std::numeric_limits<double>::max();
119 max_y_ = -std::numeric_limits<double>::max();
120
121 stream << std::setprecision(10);
122
123 const double increment = std::min(0.00005,std::abs(max_x_ - min_x_) / 1000.0);
124
125 for (x = min_x_; x <= max_x_; x += increment)
126 {
127 const double y = expression.value();
128
129 if (y < min_y_) min_y_ = y;
130 else if (y > max_y_) max_y_ = y;
131
132 stream << x << "\t" << y << "\n";
133 }
134
135 const double diff_y = std::abs(max_y_ - min_y_);
136 const double perc7_5 = diff_y * 0.075; //7.5%
137
138 min_y_ -= perc7_5;
139 max_y_ += perc7_5;
140
141 return true;
142 }
143
144 std::string title_;
145 std::string expression_;
146 double min_x_;
147 double max_x_;
148 double min_y_;
149 double max_y_;
150};
151
152int main()
153{
154 exprtk_gnuplot_fx plotter;
155
156 plotter
157 .set_expression("clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)")
158 .set_domain(-5,+5)
159 .set_title("ExprTk GNUPlot Example");
160
161 plotter.plot();
162
163 return 0;
164}
165
166
167/*
168 Build and Run:
169 1. c++ -ansi -pedantic-errors -Wall -Wextra -Werror -Wno-long-long -O3 -DNDEBUG -o exprtk_gnuplot exprtk_gnuplot.cpp -lstdc++
170 2. ./exprtk_gnuplot
171 3. gnuplot plot.gp
172*/
exprtk_gnuplot_fx & set_domain(const double min_x, const double max_x)
exprtk_gnuplot_fx & set_expression(const std::string &expression)
exprtk_gnuplot_fx & set_title(const std::string &title)
int main()