'For and if in gnuplot
I have a little problem, I need to join the for loop with the if condition to graph different functions quickly, here I leave the prototype code
reset
set terminal png
plot for [i=1:2]\
set output "Graph".i.".png"\
if (i==1)\
x title 'Graph'.i;\
if (i==2)\
2x+3x**2 title 'Graph'.i;\
if (i==3)\
3x+8+4x**2-15x**3 title 'Graph'.i
unset output
Solution 1:[1]
I am not sure if you are looking for something like the following example. If not, please rephrase your question.
You could for example define several functions and select them by the ternary operator (check help ternary). In a for loop you can also define items as strings. This allows you to change the numerical order as you like.
Script:
### select function to plot via number
reset session
f1(x) = x
f2(x) = 2*x + 3*x**2
f3(x) = 3*x +8 +4*x**2 - 15*x**3
f4(x) = x**2
f5(x) = x**3
myFunction(n) = n==1 ? f1(x) : n==2 ? f2(x) : n==3 ? f3(x) : n==4 ? f4(x) : n==5 ? f5(x) : NaN
set xrange[-1:1]
plot for [n in '1 2 5 4'] '+' u (x):(myFunction(n)) w l ti sprintf("Graph %s",n)
### end of script
Result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | theozh |

