Ask Your Question

# curve fitting coefficients.

asked 2016-01-04 19:36:40 +0100

This post is a wiki. Anyone with karma >750 is welcome to improve it.

get the model coefficients, but I can not use them to plot the graph. How to solve?

see the code:

dados = [(0, -0.183440428023042),
(0.200000000000000, -0.131101157495126),
(0.400000000000000, 0.0268875670852843),
(0.800000000000000, 0.110532679260319),
(1.00000000000000, 0.253944632998395),
(1.20000000000000, 0.257190123748649),
(1.40000000000000, 0.531888837111346),
(1.60000000000000, 0.579048247883555),
(2.00000000000000, 0.935180993484717),
(2.20000000000000, 0.916600344376623),
(2.60000000000000, 1.13328608090532),
(2.80000000000000, 1.26893326843583),
(3.00000000000000, 1.10202945535186),
(3.40000000000000, 1.13391615491257)]

point(dados,color = "red",size=20,legend_label="pontos coletados")

modelo(x) = a*x+b;modelo

a,b = find_fit(dados, modelo);a;b

modelo(x) = a*x+b

point(dados,color = "red",size=20,legend_label="pontos coletados") + plot(modelo(x),(x,0,3))
Traceback (click to the left of this block for traceback)
...
ValueError: Variable 'x' not found

edit retag close merge delete

## Comments

What are 'a' and 'b'? Did you define them as variables?

( 2016-01-04 20:42:43 +0100 )edit

## 1 Answer

Sort by ยป oldest newest most voted

You should do

sage: a,b = var('a', 'b')
sage: modelo(x) = a*x+b
sage: print modelo
x |--> a*x + b
sage: d = find_fit(dados, modelo, solution_dict=True)
sage: print d
{b: -0.1757321718542446, a: 0.4599622663189693}
sage: modelo2 = modelo.subs(d)
sage: print modelo2
x |--> 0.4599622663189693*x - 0.1757321718542446
sage: point(dados,color = "red",size=20,legend_label="pontos coletados") + plot(modelo2,(x,0,3))


In your code, the variable does not get substituted as expected. You can see it by printing what is modelo.

more

## Comments

hello vdelecroix Thank you for your help ;). Now it works correctly.

( 2016-01-04 23:00:43 +0100 )edit

@jmarcellopereira De nada! If you are satisfied with my answer, you can select it with the check box that appear on its left ("mark this answer as correct"). That way this question will appeared as "solved" in the list of questions.

( 2016-01-05 02:28:51 +0100 )edit

## marked ;)

( 2016-01-05 03:49:14 +0100 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2016-01-04 19:36:40 +0100

Seen: 732 times

Last updated: Jan 04 '16