Plotting a function with discrete x values in gnuplot - graph

I need to plot a function f(x), where x is discrete set of values (in my case positive integers). I couldn't find a way to specify a step-size when using the range option and samples doesn't seem to be the right solution. Finally, I would like to approximate f(x) with a smooth function.

I don't quite understand why samples is not the solution to your problem.
If I want to plot sin(x) on an interval between 0 and 10 with a point at every integer I use
set xrange [0:10]
set sample 11
plot sin(x) w p
Obviously the number of samples is xmax-xmin+1 (10 - 0 + 1 = 11).
Finally to tackle the approximation problem have a look at this website which discusses linear least squares fitting. For simple linear interpolation use lp instead of p.

Or alternatively, play around with the ceil(x) or floor(x) functions.
Maybe have a look at this example:
http://gnuplot.sourceforge.net/demo/prob2.html

You can do:
plot [1:12] '+' u ($0):(f($0))
Where, $0 will be replaced by 1, 2, ..., 12. You can even do a smooth on this. For instance:
f(x)=sin(2*x)
plot [1:12] f(x) t 'the function'\
, '+' u ($0):(f($0)) t 'the points'\
, '+' u ($0):(f($0)) smooth cspline t 'the smooth'

Related

Plotting a one dimensional curve in Maple with implicitplot3d

I want to plot in Maple the solutions to the equation (x-y)^2+(1-z)^2=0.
However, implicitplot3d is not able to plot them, at least using the default arguments. Any recommendations?
I know a priori that the set of solutions is going to be a curve contained in a plane, because I want to plot solutions of equations of the form 'f(x,y)^2+(z-1)^2=0'. Where 'f(x,y)' is a polynomial.
If x, y, and z are all real then those two squares must both equal zero, and thus z=1.
In that case you can simply utilize the implicitplot command for a 2-D plot of f(x,y)=0, and if you wish you can transform that to a 3-D plot with z=1.
restart;
with(plots,display): with(plots,implicitplot):
with(plottools,transform):
eqn := (x-y)^2+(1-z)^2 = 0:
P2D := implicitplot(eval(eqn,z=1)):
display(transform((x,y)->[x,y,1])(P2D),
labels=[x,y,z]);
eqn := (x^2-y)^2+(1-z)^2 = 0:
P2D := plots:-implicitplot(eval(eqn,z=1)):
display(transform((x,y)->[x,y,1])(P2D),
labels=[x,y,z]);

How to draw graph of Gauss function?

Gauss function has an infinite number of jump discontinuities at x = 1/n, for positive integers.
I want to draw diagram of Gauss function.
Using Maxima cas I can draw it with simple command :
f(x):= 1/x - floor(1/x); plot2d(f(x),[x,0,1]);
but the result is not good ( near x=0 it should be like here)
Also Maxima claims:
plot2d: expression evaluates to non-numeric value somewhere in plotting
range.
I can define picewise function ( jump discontinuities at x = 1/n, for positive integers )
so I tried :
define( g(x), for i:2 thru 20 step 1 do if (x=i) then x else (1/x) - floor(1/x));
but it don't works.
I can also use chebyshew polynomials to aproximate function ( like in : A Graduate Introduction to Numerical Methods From the Viewpoint of Backward Error Analysis by Corless, Robert, Fillion, Nicolas)
How to do it properly ?
For plot2d you can set the adapt_depth and nticks parameters. The default values are 5 and 29, respectively. set_plot_option() (i.e. with no argument) returns the current list of option values. If you increase adapt_depth and/or nticks, then plot2d will use more points for plotting. Perhaps that makes the figure look good enough.
Another way is to use the draw2d function (in the draw package) and explicitly tell it to plot each segment. We know that there are discontinuities at 1/k, for k = 1, 2, 3, .... We have to decide how many segments to plot. Let's say 20.
(%i6) load (draw) $
(%i7) f(x):= 1/x - floor(1/x) $
(%i8) makelist (explicit (f, x, 1/(k + 1), 1/k), k, 1, 20);
(%o8) [explicit(f,x,1/2,1),explicit(f,x,1/3,1/2),
explicit(f,x,1/4,1/3),explicit(f,x,1/5,1/4),
explicit(f,x,1/6,1/5),explicit(f,x,1/7,1/6),
explicit(f,x,1/8,1/7),explicit(f,x,1/9,1/8),
explicit(f,x,1/10,1/9),explicit(f,x,1/11,1/10),
explicit(f,x,1/12,1/11),explicit(f,x,1/13,1/12),
explicit(f,x,1/14,1/13),explicit(f,x,1/15,1/14),
explicit(f,x,1/16,1/15),explicit(f,x,1/17,1/16),
explicit(f,x,1/18,1/17),explicit(f,x,1/19,1/18),
explicit(f,x,1/20,1/19),explicit(f,x,1/21,1/20)]
(%i9) apply (draw2d, %);
I have made a list of segments with ending points. The result is :
and full code is here
Edit: smaller size with shorter lists in case of almost straight lines,
if (n>20) then iMax:10 else iMax : 250,
in the GivePart function

Integration Method, hit and miss in R

I want to calculate the following integrate by using the hit and miss method.
I=∫x^3dx with lower= 0 and upper =1
I know how to solve it but I cannot find the right code in R to calculate it and generate -for example 100000 random- and then plot them like this:
Thank you.
1. Generate 2 vectors from uniform distribution of the desired length
l = 10000
x = runif(l)
y = runif(l)
2. The approximation of the integral is the number of cases where the (x,y) points are below the function you want to integrate:
sum(y<x^3)/l
3. For the plot, you just have to plot the points, changing their color depending whether they are above or below the curve, and add the function with curve():
plot(x,y,col=1+(y<x^3))
curve(x^3,add=T,col=3)

unexpected line in a plot of function defined by parts - gnuplot

I'm trying to plot a piece wise defined function give by,
equation
Implemented in gnuplot, like this,
h(x)=0
g(x)=(4/3)*(1-x**3)
plot h(x)*(x<0) + g(x)*(x>0)*(x<1) + h(x)*(x>1)
The problem is that the line that goes from h(x) to g(x) is not a vertical line, is a inclined one. I really need a vertical one.
How to fix it?
plot
You have to increase the sampling rate, for example by
set samples 1000
Also note that you might have to change the definition of the function g(x) to
g(x)=(4./3.)*(1-x**3)
because you otherwise evaluate (4/3), which is equal to 1 rather than 1.333...
You mustn't have (mathematically speaking) a connecting line between the noncontinous parts, because that implies that there are actual function values there.
You should do this instead:
plot sample [:0] h(x) lc 1 title "f(x)", \
[0:1] g(x) lc 1 notitle, \
[1:] h(x) lc 1 notitle

Plotting fit in gnuplot log($1):($2) defining xrange

I have a question regarding plotting a exponential fit in Gnuplot and having the y-axis set in logscale. I am using Gnuplot 4.6.3.
I will present the few ways I have tried setting a range to fit in:
The function I want to fit on the data is f(x)=a+b*x
The first:
set log y
f(x)=a+b*x
fit [4:8] f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Resutls:
Read 15 points
Skipped 15 points outside range [x=4:8]
No data to fit
error during fit
The second:
fit f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Results:
Final set of parameters Asymptotic Standard Error
a =16.9858 +/- 6.299 (37.08%)
b =-8.43215 +/- 3.502 (41.53%)
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
Obviously the fit is ridiculous but this is the only way I know of that actually gives me a fit. I know the problem most likely lies within the defining of the xrange when fitting but what else can I write there to fix this?
Below I will show image of my plot (f(x) is not showing as it is plotted in the negative area.)
'cause a logscale of the y axis requires the logarithm of the y axis during the fit ;-)
fit [4:8] f(x) 'CT2A_OH_R_log.dat' u 1:(log($2)) via a,b
NOTE
If you want to use the logarithm based on 10, you have to use log($2)/log(10) or log10($2).
#Tom Solid
gnuplot> fit [4:8] [0.1:60] f(x) 'CT2A_OH_R_log.dat' using 1:(log($2))
via a,b
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
This time the fit is exponential like.
So, I will post the picture of what I am getting now:

Resources