I would like to fit data with a function f(x), where f(x) = (K)(xlog(x) + (1-x)log(1-x)) + Ax*(1-x) + B*(x)*2(1-x)**2, and K, A, B are the fitted constants. And, is what I have until now, where green solid line is the fitted curve. Here is the Data and below my gnuplot script
f(x)= (K)*(x*log(x) + (1-x)*log(1-x)) + A*x*(1-x) + B*(x)**2*(1-x)**2
pl '1417.dat' u 1:2
fit f(x) '1417.dat' u 1:2 via K,A,B
Obtained K = 8116.63, A = 2.20, B=43692
I need to fit accurately at the minima regions, can anyone suggest how to fit with less deviation.
I guess something must be "wrong" with your function/model (or with your data).
If k(x), a(x), and b(x) are all symmetric functions to x=0.5, and so is f(x).
How should the sum of 3 symmetric functions give an asymmetric function?
Code:
### impossible fitting
reset session
FILE = "SO/1417.dat"
k(x) = K*(x*log(x) + (1-x)*log(1-x))
a(x) = A*x*(1-x)
b(x) = B*x**2*(1-x)**2
f(x)= k(x) + a(x) + b(x)
set fit nolog
fit f(x) FILE u 1:2 via K,A,B
plot FILE u 1:2, k(x), a(x), b(x), f(x) lw 2 lc "red"
### end of code
Result:
For example the slightly different function below allows a better fitting :
Note that ln(x)ln(1-x) is exactly equal to zero at x=0 and x=1.
Related
This is similar to this question and this but my problem is that I have several hundred files that need to be simultaneously fitted and plotted on one graph. Unlike the other questions posted I'm looking for the best fits for each file, not for the global data set so cat won't work.
I was hoping to use fit.. for like I do for plot but it's not working so well. Here's what I've got so far:
f(x) = 1+d*exp(-(x-f)**2/(2*(g**2)))+a*exp((-(x-b)**2)/(2*(c**2)))
filename(n) = sprintf("rheosaxs_saxs_%005d_0001_var_rebin_div.dat", n)
fit f(x) for [n=575:584] filename(n) u 1:2 via a,b,c,d,f,g
plot for [n=575:584] filename(n) using 1:2, f(x)
the error I get is: line 60: undefined variable: for
which corresponds to the fit f(x) for [n=a:b]
I know that my starting parameters are reasonable because I can plot them without the fit command and they look sensible. Similarly my plot for works ok.
Any ideas? Thank you :)
In version 5.2 gnuplot introduces arrays, which allow you to save the results of each fit and plot those later.
A simplified example script would be
file(n) = sprintf('myfile_%d.dat', n)
f(a, x) = a*x
array A[10]
do for [i=1:10] {
tmpA = 1
fit f(tmpA, x) file(i) via tmpA
A[i] = tmpA
}
plot for [i=1:10] file(i),\
for [i=1:10] f(A[i], x)
Although gnuplots arrays are implemented as linked list of user variables, it is not possible to use A[i] directly for the fit, but I had to use a temporary variable to get it right.
A full working example, including generation of random data, with python from gnuplot, uargh ;):
# generate some random data
system("python3 -c 'import random\nfor i in range(1, 11):\n\twith open(\"output_{0}.dat\".format(i), \"w\") as f:\n\t\tf.write(chr(10).join([str(i*100 + i* x * (1 + 0.1*(random.random()-0.5))) for x in range(0,100)]))'")
file(n) = sprintf('output_%d.dat', n)
f(a, b, x) = a*x + b
array A[10]
array B[10]
do for [i=1:10] {
tmpA = 1
tmpB = 1
fit f(tmpA, tmpB, x) file(i) u 0:1 via tmpA, tmpB
A[i] = tmpA
B[i] = tmpB
}
plot for [i = 1:10] file(i) u 0:1 with points lt i notitle, \
for [i=1:10] f(A[i], B[i], x) with lines lt i notitle
BTW: There is no fit for, because that is equivalent to do for { fit }. But when plotting, plot for generates a single plot with multiple functions, whereas do for { plot } makes several plots and should be used with multiplot
Suppose E:y^2=x^3+Ax+B mod p, I have two questions?
how can I draw the graph of E with pari-gp.
how can I get the list of all points over the E.
thank you for all.
To define an Elliptic Curve with SageMath use
E = EllipticCurve(GF(131),[0,1,0,1,0])
print(E)
and outputs
Elliptic Curve defined by y^2 = x^3 + x^2 + x over Finite Field of size 131
In your case ( simplified Weierstrass from)
E = EllipticCurve(GF(p),[A,B])
will be enough.
To plot a curve
E.plot()
is enough
To iterate the points
for T in E.points():
print(T)
is enough.
Try online on SageMathCell.
And notice the symmetry!
Pari-GP
From a tutorial
a=ffgen(P,’a)
Es = ellinit([a^4,a^6],a);
I have an equation that I created on Desmos website
I used the code below to try and recreate it in Octave. But when I plot it, it comes out different. How can I fix the code in Octave (without changing the main equation, if possible) so it looks like the Desmos image?
x = linspace(0,1,20);
y = linspace(0,1,20);
S=[13.2];
T=1.12;
for zz=1:1:length(S)
eq1=exp(S(1,zz)*T*log(x))+exp(S(1,zz)*T*log(y));
hold on
plot(x,eq1)
zz
end
PS: I'm using Octave 4.2.2
S = 13.2;
T = 1.12;
f = #(x)exp(log(1-exp(S*T*log(x)))./(S*T));
fplot(f, [0, 1])
Desmos.com does not plot (x,eq1) but (x,y) with the constraint that x, y satisfy the given equation. So, you solve for y for each value of x, and plot the pairs (x,y).
Since log(x), log(y) exist, x and y are >0 (otherwise you would have to plot for x<0 as well).
clear; clc;
x = linspace(0,1,150);
S = 13.2;
T = 1.12;
y = zeros(size(x));
for i = 1:length(x)
y(i) = (1-exp(S*T*log(x(i))))^(1/S/T);
end
plot(x,y)
Notes:
1) I assume by log(x) you mean ln(x) (logarithm using base e).
2) I used a more dense discretization with 150 points so that the plotted curve appears smoother.
3) Mathematically, linspace(0,1,150) should not work, as log(x=0) is not defined. However for Matlab log(0) = -inf which means that exp(-inf) = 0. That's why no runtime error is thrown.
4) By the way, the provided equation can be simplified to x^(ST) + y^(ST) = 1, with the constraints that x, y > 0.
I would like to draw a curve logW= a+b*logP, where a = 12, b = -0.8
R = -0.4 of this curve. I would like to use data and look how this curve fits to data. Can I do it in gnuplot?
log(f(x)) = a+b*log(P)
a = 12, b = -0.8
fit f(x) 'data.txt' u 1:2 w p via a,b
plot f(x) 'data.txt' u 1:2
Does it sounds ok?
You are close, but there are little problems everywhere. Let's try to clean it up.
1) You must define f(x) itself, not log(f(x)). It is not clear from your statement, but I assume 'P' is the independent variable x?
f(P) = exp(a + b*log(P))
2) gnuplot commands are separated by semicolons, not commas
a = 12; b = -0.8
3) 'fit' is not a plotting style, so "with points" makes no sense as part of a fit command
fit f(x) 'data.txt' using 1:2 via a,b
4) The 'plot' command has two separate pieces: the data and the curve fit to it
plot 'data.txt' using 1:2 with points, f(x) with lines
Hey so I'm reading this article by Chris Hecker where he has an image of a Parabola surrounded by the a vector field of it's derivative:
However he never mentions how exactly he got the vector field equation, and never even states it. He does say he overlayed the vector field of the slopes in Figure 1, by drawing the solution to the slope equation, dy/dx = 2x, as a short vector at each coordinate on the grid.
How do you create a vector field of the slopes of an equation in the vector field syntax of
V = xi + yj
The Figure title would be clearer if it read:
The curve y = x^2, and the vector field dy/dx = 2x for the general case y = x^2 + C
There are three equations at work in the graph above:
y = x^2 - The equation for the parabola drawn - This is the one long solid curve
y = x^2 + C -The equation for all parabolas that fit on the vector field - C is a constant. This is the equation for all parabolas that fit on that vector field
dy/dx = 2x The equation for the slope field. - This is the slope or derivative of the both the curve drawn and all the possible curves that can be drawn with y = x^2 + C for all constant Cs.
Note that C is a constant, since the derivative of y = x^2 + C with any C is 2x. So the vector field shows how to draw all the different parabolas with different Cs.
So there are two ways to calculate the vector field:
Iterate over your desired range of x and y and calculate the slope, dy/dx- 2x independent of y in this case - at each point. This is how the author did it.
Draw a bunch of parabolas by slowly varying C in y = x^2 + C over a desired range of - let's say - x calculating y.
For a differential equation dy/dx = f(x,y) (e.g., dy/dx = 2x in this case, with f(x,y) = 2x), the vector field (F) will be F = i + f(x,y)j (so in your case, F = i + 2x j )