why does not this equation compile?

0
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

    
asked by anonymous 07.12.2018 / 01:59

3 answers

4

@GiovaneRamos, your code has some problems:
1. As the folks already mentioned, you need to update% with% multiplication to f(x)
2. When analyzing your code, I see that there is an architecture error, because when running with these values of math.cosh(2*x) and a , when calculating the first b , its value will be 0.5, but this value already is the root of the equation x , and since the output of the f(x) function will be zero, which is always less than x , and therefore will not even enter erro , and therefore not will print nothing (since while is within print )
3. Using the while end of your print , for the concept you are using, the root is the average value within the range, so the root should be while and not x , as you are printing. Home 4. The approximate error would not be a , but (b-a)

So the simplest way to solve points 2 and 3 is to change the math.fabs(f(x)) into print , not if , and thus ensure that it always prints the correct root , which in this case is while , 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")
    
07.12.2018 / 12:21
5

According to what you have made available, the error is occurring because of 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 x parameter of the function. However, the Python language, and I think all other programming languages do not understand the same way, because in programming languages the multiplication operation has to be represented with * (ex: 2*2 == 4 , 4*4 == 16 , etc.).

Summary: Only replace 2x with 2*x , and this error will no longer occur.

I hope I have helped!

    
07.12.2018 / 02:17
4

Good evening Giovane, I took a look at your code, and the fact that it does not compile is actually due to a syntactic error in your code. The error is in the math.cosh (2x) expression, it occurs because you are passing the value 2x as a single value, but the code interpreter does not perform multiplication operations form, in order for them to be executed, it is necessary to use the multiplication operator, * (asterisk).

So, your code should be only with this modification, being as follows: math.cosh (2 * x) .

As your doubt was in Python, I suggest that next time, put your code well indented and in the code region of the stack overflow, to make it easier to understand the problem.

    
07.12.2018 / 02:12