Why doesn’t this equation compile?

Asked

Viewed 115 times

-1

import math

def f(x):
    eq = (4-8*x)*math.cosh(2x)
    return eq

a= 0
b= 1
erro = math.pow(10,-3)

if f(a)*f(b) < 0:
    x=(a+b)/2
    while (math.fabs(f(x)) > erro):
        x=(a+b)/2
        if f(a)*f(x)<0:
            b=x
            g = (b - a)
        else:
            a=x

            print("raiz: ",a , "valor da função: ", f(x), "erro aproximado", (b-a))
else:
    print("não há raizes")

Error:

File "C:/Users/Giovane/Desktop/2018.2/calculo numerico/bisseccao.py", line 4
   eq = (4-8*x)*math.cosh(2x)
                           ^

Syntaxerror: invalid syntax

Process finished with Exit code 1

3 answers

5

According to what you have provided, the error is happening because of the math.cosh(2x). In a "written" mathematical equation, this 2x would be the equivalent of 2 times the variable x, or, in this case, the parameter x of function. However, the Python language, and I think all other programming languages, do not understand in the same way, because in programming languages the multiplication operation has to be represented with the * (ex.: 2*2 == 4, 4*4 == 16, etc..).

Summary: only replace the 2x for 2*x, and this error will no longer occur.

I hope I’ve helped!

  • right friend, solved this problem, now compiles but the paycharm goes crazy saying that there is no root in the equation, even with the correct interval, the program works with any polynomial of any degree

  • 1

    @Giovaneramos, you got the answer to your specific problem, presented in the question. The algorithm for calculating roots from bisections to be wrong is another problem, which I even believe already have a very elegant answer on the subject of a user named Victor Stafusa

  • That’s the answer I mentioned

4

Good evening Giovane, I took a look at your code, and the fact that it does not compile is really due to a syntactic error existing in your code. The error is found in the expression Math.Cosh(2x), it occurs due to the fact that you are passing the value 2x as a unique value, but the interpreter of a code does not perform multiplication operations in this way, for them to be executed, it is necessary to use the multiplication operator, the * (asterisk).

Therefore, your code should only be with this modification, being as follows: Math.Cosh(2 * x).

Since your question was in Python, I suggest that next time, put your code well-idented and in the stack overflow code region, to facilitate understanding of the problem.

  • right friend, solved this problem, now compiles but the paycharm goes crazy saying that there is no root in the equation, even with the correct interval, the program works with any polynomial of any degree

3


@Giovaneramos, your code has some problems:
1. As the staff has already mentioned, you need to update in f(x) the multiplication to math.cosh(2*x)
2. When analyzing your code, I see that there is an architecture error, because when running with these values of a and b, when calculating the first x, its value will be 0.5, but this value is already the root of the equation f(x), and as the output of the x will be zero, which is always smaller than the erro, and so you won’t even get into the while, and therefore will not print anything (because the print is inside the while)
3. Using the print end of his while, by the concept you are using, the root being the average value within the range, so the root should be x and not a, how are you printing.
4. The approximate error would not be (b-a), but math.fabs(f(x))

Therefore, the simplest way to solve points 2 and 3 would be to exchange the print into the if, and not of while, and thus ensure that it will always print the correct root, which in case is x, as below:

if f(a)*f(b) < 0:
    x = (a + b)/2
    while (math.fabs(f(x)) > erro):
        x=(a+b)/2
        if f(a)*f(x)<0:
            b=x
            g = (b - a)
        else:
            a=x
        # Desconte a linha baixo caso queira acompanhar as iterações do algoritmo até chegar na raiz
        # print("x: ", x, "erro: ", math.fabs(f(x)))
    print("raiz: ", x , "valor da função: ", f(x), "erro aproximado", math.fabs(f(x)))
else:
    print("não há raizes")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.