Reputation: 524
I two functions:
f(x) = x**2
g(x) = -(x-4)*x
I need to calculate the area between these two curves, for that I would like to have visualization. WolframAlpha has exactly what I want: https://www.wolframalpha.com/input/?i=area+between+the+curves+y%3Dx%5E2+and+y%3D-x%5E2%2B4x, however, I need to make the graphics in gnuplot. Here is my code so far:
set border linewidth 1
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 1
set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 1
set key at 6.1,1.3
set xlabel 'x'
set ylabel 'y'
set xrange [-0.5:3]
set yrange [0:5]
set xtics 2
set ytics 2
set tics scale 2
f(x) = x**2
g(x) = -(x-4)*x
set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
plot f(x) title 'f(x)' with lines linestyle 1, \
g(x) title 'g(x)' with lines linestyle 2
It produces this output:
Is there any way I can highlight the area between these two curves like in WolframAlpha? Assuming that we know where they intersect (0 and 2), unless gnuplot can do that on its own.
If this is not something vanilla gnuplot is capable of I apologize, in that case some pointers to resources that can do that would be appreciated.
Upvotes: 2
Views: 1151
Reputation: 5237
This can be achieved using filledcurves
(see docs: http://gnuplot.sourceforge.net/docs_4.2/node245.html). You just need to use the below
option to ensure only the area between the two curves is filled.
As an example, try this:
plot '+' using 1:(f($1)):(g($1)) title '' with filledcurves below lc rgb 'green', \
f(x) title 'f(x)' with lines linestyle 1, \
g(x) title 'g(x)' with lines linestyle 2
This is based on the way its described in the FAQ here for two curves: http://www.gnuplot.info/faq/#x1-460005.3
Upvotes: 3