-1

I'm trying to write a small program in python that involves(among other things) Newton method, but i'm encountering several problems, that are probably pretty basic, but since I'm new at programming, i cant overcome..

First i defined the function and it's derivative:

import math
def f(x,e,m):
        return x-e*math.sin(x)-m

def df(x,e):
        return 1-e*math.cos(x)


def newtons_method(E0,m,e,q):#q is the error
    while abs(f(E0,e,m))>q:
        E=E0-f(E0,e,m)/df(E0,e)
        E0=E
    return (E0)


def trueanomaly(e,E):
     ta=2*math.arctan(math.sqrt((1+e)/(1-e))*math.tan(E))
     return (ta)


def keplerianfunction(T,P,e,K,y,w):
    for t in frange (0,100,0.5):
        m=(2*math.pi*((t-T)/P))
        E0=m+e*math.sin(m)+((e**2)/2)*math.sin(2*m)   
        newtons_method(E0,m,e,0.001)
        trueanomaly(e,E0)
        rv=y+K*(e*math.cos(w)+math.cos(w+ta))
                return (ta)","(rv)

def frange(start, stop, step):
        i = start
        while i < stop:
                yield i
                i += step

The question is that this keeps giving me errors, indentation errors and stuff, especially in the keplerianfunction ... Can someone help me? what am I doing wrong here?

Thank you in advance!

  • Can you show the exact error you are getting? – RottenCandy Dec 09 '17 at 16:52
  • SyntaxError: inconsistent use of tabs and spaces in indentation – Hilberto Silva Dec 09 '17 at 16:55
  • 1
    In your `keplerianfunction`, the indentation problem is pretty obvious: your final `return` should be on the same level as the rest, not on the level of your opening bracket. The format you are trying to return is confusing as well, a string between two tuppled variables doesn't make much sense... – Arthur Spoon Dec 09 '17 at 16:57
  • 1
    This type of error occurs when you have improper indentation, you might have a mixture of tabs and spaces. Convert all indentation to either one of those and try again. – RottenCandy Dec 09 '17 at 16:57
  • @HilbertoSilva if you can put that error and any others in the question it would be helpful. As for that particular error, it means that your file is mixing tabs and spaces, rather than using only tabs or only spaces. The Python way is to use only spaces, so I would recommend to transform all tabs to spaces (there should be a tool to do that in your editing software). – Arthur Spoon Dec 09 '17 at 16:58
  • @ArthurSpoon thank you very much, it was indeed that the problem, but meanwhile another one appeared, as i run the function it return the errror ta is not defined. right before the return in the keplerianfunction, doesn't the funtion picks automatically the ta value from the trueanomaly function? thank you – Hilberto Silva Dec 09 '17 at 17:09
  • @RottenCandy thank you! It truly was a mix of spaces and tabs problem! – Hilberto Silva Dec 09 '17 at 17:12
  • @HilbertoSilva your function `trueanomaly` is returning a value that you need to store in a variable then, e.g. `ta = trueanomaly (e, E0)`, and you need to do that for any function that you're using. The variables declared within a function only exist in the function, not outside it, and so you cannot access `ta` outside of your `trueanomaly` function. It's the same for all the other functions that you are calling, btw. – Arthur Spoon Dec 09 '17 at 17:17
  • @ArthurSpoon thank you again! Finally i got it to work. Last question: the outcome pairs (ta,rv), i need to use them to plot a graph... Any idea how i can put them into a .dat file or something like that? – Hilberto Silva Dec 09 '17 at 17:30

1 Answers1

0

Many things are wrong with this code, and I don't know what the desired behaviour is so can't guarantee that this will help, but I'm going to try and help you debug (although it looks like you mostly need to re-read your Python coursebook...).

First, in most languages if not all, there is a thing called the scope: a variable, function, or any other object, exists only within a certain scope. In particular, variables exist only in the scope of the function that they are defined in. This means that, to use the result of a function, you first need to return that result (which you are doing), and when you call that function you need to store that result into a variable, for example ta = trueanomaly(e, E0).

Then, you don't really need to use brackets when returning values, even if you want to return multiple values. If you do want to return multiple values though, you just need to separate them with a comma, but not with a string character of a comma: write return ta, rv instead of return ta","rv.

Finally, you seem to be iterating over a range of values, yet you don't return the whole range of values but either the first value (if your return is in the for loop), or the last one (if your return is under the for loop). Instead, you may want to store all the ta and rv values into one/two lists, and return that/those lists in the end, for example:

def keplerianfunction(T,P,e,K,y,w):
    # Initialise two empty lists
    tas = []
    rvs = []
    for t in frange (0,100,0.5):
        m = 2*math.pi*((t-T)/P)
        E0 = m+e*math.sin(m)+((e**2)/2)*math.sin(2*m)   
        E0 = newtons_method(E0,m,e,0.001)
        ta = trueanomaly(e,E0)
        rv = y+K*(e*math.cos(w)+math.cos(w+ta))
        # At each step save the value for ta and rv into the appropriate list
        tas.append(ta)
        rvs.append(rv)
    # And finally return the lists
    return (tas,rvs)

Also, why using a new frange function when range has the exact same behaviour and is probably more efficient?

Arthur Spoon
  • 432
  • 2
  • 18
  • Thank you again! I used the frange because range would only make unitary steps and i needed float steps. But yes, it seems to be working perfectly now, thank you... Now i only need to use the data to plot... Any idea how to turn into a data or txt file? – Hilberto Silva Dec 09 '17 at 18:11
  • Writing the data to a file would be pretty easy to look up online. I would recommend looking up the `csv` package, or just Google whatever file format you want to have. – Arthur Spoon Dec 09 '17 at 18:34