Winston_Churchill
Bazinga!
+521|6956|Toronto | Canada

Code:

from scipy.optimize import *
from numpy import *
from pylab import *

#import period and length into separate arrays using loadtxt
lengtharray, periodarray = loadtxt("C:\Documents and Settings\student\Desktop\programisawesome.txt", dtype='float', unpack=True)

#calculate the square of the period and its error
Tsquare=periodarray**2
Tsquareerror=2*periodarray*0.05

#define gravitational constant in terms of measured Tsquare and length
g = 9.8



#define the initial parameters
p0=0.0, 4*pi*2/g


#define the residuals using the parameters, T squared and the length
def residuals (p,Tsquare,lengtharray):
    return Tsquare-peval(lengtharray,p)

#define linear fit function
def peval(lengtharray, p):
    return p[0]+p[1]*lengtharray

plsq = leastsq(residuals,p0,args=(Tsquare,lengtharray),maxfev=2000)

print plsq[0]

#use the errorbar function to draw error bars on the graph
errorbar(lengtharray,Tsquare,Tsquareerror,fmt='r+')

#plot the graph of Tsquare vs length on the graph          
plot (lengtharray, Tsquare)
#title of graph
title ('Length vs. Period Squared')
#label for x axis
xlabel ('Length (m)')
#label for y axis
ylabel ('Period Squared (s^2)')
show ()
The lab is this - http://www.physics.utoronto.ca/~phy225h … o_code.pdf  Basically trying to linearize a relationship between measurements but I cant get our leastsq function to work.  Any ideas?  When I try to print plsq it gives me invalid syntax, ive tried it as plsq and plsq[0]
jsnipy
...
+3,276|6740|...

what's the len of plsq? print len(plsq)

very ostentatious lab btw

edit: also what's in "programisawesome.txt"
Winston_Churchill
Bazinga!
+521|6956|Toronto | Canada

says invalid syntax for len when i do that

its values for length and period in columns, we know that part works properly since ive printed it out before
Winston_Churchill
Bazinga!
+521|6956|Toronto | Canada

Code:

from scipy.optimize import *
from numpy import *
from pylab import *

#import period and length into separate arrays using loadtxt
lengtharray = [.355,.27,.155,.084,.04]
periodarray = [1.192338,1.037447,.82259,.64556,.56032]
#lengtharray, periodarray = loadtxt("C:\Users\~\Desktop\Python\trythisone.txt", dtype='float', unpack=True)

#calculate the square of the period and its error
Tsquare = zeros(5)
Tsquareerror = zeros(5)
i=0
while i<5:
    Tsquare[i]=periodarray[i]**2
    Tsquareerror[i]=2*periodarray[i]*0.05
    print Tsquare[i]
    
    i=i+1
    

#define gravitational constant in terms of measured Tsquare and length
g = 9.8

print lengtharray
print periodarray


#define the initial parameters
p0=0.0, 4*pi*2/g


#define the residuals using the parameters, T squared and the length
def residuals (p,Tsquare,lengtharray):
    return Tsquare-peval(lengtharray,p)

#define linear fit function
def peval(lengtharray, p):
    return p[0]+p[1]*lengtharray

plsq = leastsq(residuals,p0,args=(Tsquare,lengtharray),maxfev=2000)

print plsq[0]

#use the errorbar function to draw error bars on the graph
errorbar(lengtharray,Tsquare,Tsquareerror,fmt='r+')

#plot the graph of Tsquare vs length on the graph          
plot (lengtharray, Tsquare)
#title of graph
title ('Length vs. Period Squared')
#label for x axis
xlabel ('Length (m)')
#label for y axis
ylabel ('Period Squared (s^2)')
show ()
new attempt, now i get the error

Code:

Traceback (most recent call last):
  File "C:\Users\~\Desktop\Python\newsweetprogram(this is nicklass).py", line 41, in <module>
    plsq = leastsq(residuals,p0,args=(Tsquare,lengtharray),maxfev=2000)
  File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 276, in leastsq
    m = _check_func('leastsq', 'func', func, x0, args, n)[0]
  File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 13, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File "C:\Users\~\Desktop\Python\newsweetprogram(this is nicklass).py", line 35, in residuals
    return Tsquare-peval(lengtharray,p)
ValueError: operands could not be broadcast together with shapes (5) (10)
so confused
HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5701|Bolingbrook, Illinois

Code:

from scipy.optimize import *
from numpy import *
from pylab import *

#import period and length into separate arrays using loadtxt
lengtharray = [.355,.27,.155,.084,.04]
periodarray = [1.192338,1.037447,.82259,.64556,.56032]
#lengtharray, periodarray = loadtxt("C:\Users\~\Desktop\Python\trythisone.txt", dtype='float', unpack=True)

#calculate the square of the period and its error
Tsquare = zeros(5)
Tsquareerror = zeros(5)
i=0
while i<5:
    Tsquare[i]=periodarray[i]**2
    Tsquareerror[i]=2*periodarray[i]*0.05
    print Tsquare[i]
    
    i=i+1
To save you some future trouble, since you're opening a file with an unknown amount of elements in the arrays:

Code:

arrayLength = len(lengthArray)
tSquare = zeros(arrayLength)
tSquareError = zeros(arrayLength)
i = 0
while i < arrayLength:
    tSquare[i] = periodArray[i] ** 2
    tSquareError[i] = 2 * periodArray[i] * 0.05
    print tSquare[i]
    i += 1

Code:

p0=0.0, 4*pi*2/g
What are you trying to do here?  I see that you use the variable p later in the program.  Are you trying to create a list/tuple here?:

Code:

p = (0.0, 4 * pi * 2 / g)

Code:

#define the residuals using the parameters, T squared and the length
def residuals (p,Tsquare,lengtharray):
    return Tsquare-peval(lengtharray,p)
Why is tSquare a function parameter if you're not going to use it?

Code:

#define linear fit function
def peval(lengtharray, p):
    return p[0]+p[1]*lengtharray
This won't work because p doesn't have any elements.  See above for the proper way to initialize p.

Code:

plsq = leastsq(residuals,p0,args=(Tsquare,lengtharray),maxfev=2000)

print plsq[0]
In your arguments, make sure you put parenthesis after residuals, residuals().  Unless you're trying to pass the object as an argument, which I doubt.

Anyway, I would help more, but I have no idea what leastsq() and Tsquare-peval() do.  Post the function definitions of those and I can help out on the arguments and return statement.
Winston_Churchill
Bazinga!
+521|6956|Toronto | Canada

Thanks, we actually handed it in today and got our TA to help us, among others our biggest problem was that we needed to print out plsq instead of lengtharray at the end.

leastsq() is a least square function and peval() is a linear fitting function.
HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5701|Bolingbrook, Illinois

Winston_Churchill wrote:

Thanks, we actually handed it in today and got our TA to help us, among others our biggest problem was that we needed to print out plsq instead of lengtharray at the end.

leastsq() is a least square function and peval() is a linear fitting function.
but to actually help, it would be helpful for me to see the function definition, as in the actual code of the function so i could help you with how the arguments should be formatted and how the return value is formatted.
Winston_Churchill
Bazinga!
+521|6956|Toronto | Canada

i have no idea what it is myself, its online though www.scipy.org
HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5701|Bolingbrook, Illinois
Oh, my bad.  I should've looked at your imports.

it makes a lot more sense after reading this:

http://docs.scipy.org/doc/scipy/referen … astsq.html

well either, i guess it's too late now

Board footer

Privacy Policy - © 2024 Jeff Minard