0

I'm trying to solve the equation f(x) = x-sin(x) -n*t -m0

In this equation, n and m0 are attributes, defined in my class. Further, t is a constant integer in the equation, but it has to change each time.

I've solved the equation so i get a 'new equation'. I've imported scipy.optimize

def f(x, self):
    return (x - math.sin(x) -self.M0 - self.n*t)

def test(self,t):
    return fsolve(self.f, 1, args=(t))

Any corrections and suggestions to make it work?

KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
Saybia
  • 3
  • 1
  • 1
  • 2
  • 3
    Can you give us a complete example, including what about this "doesn't work"? – robert May 18 '12 at 12:30
  • 1
    I don't think I've ever seen `f(x, self)` before.. the first argument will get the instance, regardless of what you call it. – DSM May 18 '12 at 12:33
  • it is with fsolve from scipy.optimize – Saybia May 18 '12 at 12:36
  • the main problem is that it al has to be done in a class. that is why i need to define the equation in f(x, self) – Saybia May 18 '12 at 12:37
  • The first error i need to get rid of is TypeError: func() takes exactly 2 arguments (3 given) so it has probably something to do with the args=(t) because before i've added that, it didn't give that error – Saybia May 18 '12 at 12:39

3 Answers3

4

I can see at least two problems: you've mixed up the order of arguments to f, and you're not giving f access to t. Something like this should work:

import math
from scipy.optimize import fsolve

class Fred(object):
    M0 = 5.0
    n = 5

    def f(self, x, t):
        return (x - math.sin(x) -self.M0 - self.n*t)

    def test(self, t):
        return fsolve(self.f, 1, args=(t))

[note that I was lazy and made M0 and n class members]

which gives:

>>> fred = Fred()
>>> fred.test(10)
array([ 54.25204733])
>>> import numpy
>>> [fred.f(x, 10) for x in numpy.linspace(54, 55, 10)]
[-0.44121095114838482, -0.24158955381855662, -0.049951288133726734,
 0.13271070588400136, 0.30551399241764443, 0.46769772292130796, 
 0.61863201965219616, 0.75782574394219182, 0.88493255340251409, 
 0.99975517335862207]
DSM
  • 342,061
  • 65
  • 592
  • 494
1

You need to define f() like so:

  def f(self, x, t):
    return (x - math.sin(x) - self.M0 - self.n * t)

In other words:

  1. self comes first (it always does);
  2. then comes the current value of x;
  3. then come the arguments you supply to fsolve().
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You're using a root finding algorithm of some kind. There are several in common use, so it'd be helpful to know which one.

You need to know three things:

  1. The algorithm you're using
  2. The equation you're finding the roots for
  3. The initial guess and range over which you're looking

You need to know that some combinations may not have any roots.

Visualizing the functions of interest can be helpful. You have two: a linear function and a sinusiod. If you were to plot the two, which sets of constants would give you intersections? The intersection is the root you're looking for.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • the main problem is that it al has to be done in a class. that is why i need to define the equation in f(x, self). – Saybia May 18 '12 at 12:36