0
B        x(cm)
24.5    4.2
25.5    4.5
26.5    5.0
27.5    5.4
28.5    5.9
29.5    6.6
30.5    7.2
31.5    7.9
32.5    8.6
33.5    9.3
34.5    10.0
35.5    10.5
36.5    10.9
37.5    11.1
38.5    11.1
39.5    10.8
40.5    10.3
41.5    9.8
42.5    9.2
43.5    8.4
44.5    7.7
45.5    7.1
46.5    6.4
47.5    5.9
48.5    5.4
49.5    5.0
50.5    4.6
51.5    4.2

This is my data. And y(x) = a/(b**2 + x**2)**3/2 is the equation to which I want to fit the above data but the problem I am facing is that value of b is coming negative. So I want to know how will I change the origin of the graph to get the right result

2 Answers 2

1

A few things:

  1. are you sure the function is f(x) = a/(b**2 + x**2)**3/2 and not f(x) = a/(b**2 + x**2)**(3/2), mind the parentheses around (3/2).
  2. gnuplot has integer division (a common pitfall for unexpected results), hence, (3/2) will be evaluated to 1 instead of the expected 1.5.
  3. why not letting gnuplot find the offset? Just introduce a variable c which will account the x-offset and let it fit.
  4. depending on your model, i.e. if the exponent is variable, you could also add a variable d for the exponent and let it find by the gnuplot fitting algorithm.
  5. sometimes it's better if you help the fitting with good starting values.

Then you have to judge whether the fitted values are making sense or not, e.g. b<0 or d=0.794 ...

Code:

### fitting with finding x-offset automatically
reset session

$Data <<EOD
B        x(cm)
24.5    4.2
25.5    4.5
26.5    5.0
27.5    5.4
28.5    5.9
29.5    6.6
30.5    7.2
31.5    7.9
32.5    8.6
33.5    9.3
34.5    10.0
35.5    10.5
36.5    10.9
37.5    11.1
38.5    11.1
39.5    10.8
40.5    10.3
41.5    9.8
42.5    9.2
43.5    8.4
44.5    7.7
45.5    7.1
46.5    6.4
47.5    5.9
48.5    5.4
49.5    5.0
50.5    4.6
51.5    4.2
EOD

f1(x) = a1/(b1**2 + (x-c1)**2)**(3/2)
f2(x) = a2/(b2**2 + (x-c2)**2)**(3./2)
f3(x) = a3/(b3**2 + (x-c3)**2)**d3

set fit quiet nolog
fit f1(x) $Data u 1:2 via a1,b1,c1
fit f2(x) $Data u 1:2 via a2,b2,c2
a3=11; b3=1; c3=40; d3=1.5             # sometimes it's better to help the fitting with some good starting values

fit f3(x) $Data u 1:2 via a3,b3,c3,d3


print sprintf("% 9s% 9s% 9s% 9s","a","b","c","d")
print sprintf("%9.3g %9.3g %9.3g",a1,b1,c1)
print sprintf("%9.3g %9.3g %9.3g",a2,b2,c2)
print sprintf("%9.3g %9.3g %9.3g %9.3g",a3,b3,c3,d3)

plot $Data u 1:2 w p pt 7,\
     f1(x) w l lc "red",\
     f2(x) w l lc "web-green", \
     f3(x) w l lc "web-blue"
### end of code

Result:

        a        b        c        d
 1.17e+03      10.3      37.9
 2.73e+04     -13.6      37.9
      343      8.66      37.9     0.794

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure, but I think the equation that you're trying to fit to may be inappropriate for the data. Perhaps you could rewrite your equation such that it's clearer.

Here's an example using the quadratic equation y(x) = a*x**2 + b*x + c to fit:

test.dat

24.5 4.2
25.5 4.5
26.5 5.0
27.5 5.4
28.5 5.9 
29.5 6.6 
30.5 7.2 
31.5 7.9 
32.5 8.6 
33.5 9.3 
34.5 10.0 
35.5 10.5 
36.5 10.9 
37.5 11.1 
38.5 11.1 
39.5 10.8 
40.5 10.3 
41.5 9.8 
42.5 9.2 
43.5 8.4 
44.5 7.7 
45.5 7.1 
46.5 6.4 
47.5 5.9 
48.5 5.4 
49.5 5.0 
50.5 4.6 
51.5 4.2 

quad_fit.gp

set term pos col
set out 'xy_fit.ps'

set title 'Quadratic Regression Example Scatterplot'
set ylabel 'Y'
set xlabel 'X'
set style line 1 ps 1.5 pt 7 lc 'red'
set style line 2 lw 1.5 lc 'blue'

set grid

f(x) = a*(x**2) + b*x + c
fit f(x) 'test.dat' using 1:2 via a, b, c

p 'test.dat' ls 1 t 'Datapoints', f(x) ls 2 t 'Quadratic Regression'
set out

Running gnuplot quad_fit.gp produces: gnuplot output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.