Plot graphing of an algebraic expression in python

Asked

Viewed 167 times

1

I’m trying to plot a chart of an integral I solved, but it’s giving error :

from sympy import *
from sympy.plotting import *
%matplotlib inline

r=Symbol('r')
e=Symbol('e')
r=(1,10,100)

k=((rho)/(E*r**2))*(-e**(-r)*r**(2)-2e**(-r)*r-2e**(-r)+2)
k(r)
plot.title('comportamento do campo')
plot.xlabel('raio da esfera')
plot.ylabel('campo eletrico')
plot(r,k(r))

the error that gives is:

 File "<ipython-input-65-70ed324ef8d6>", line 1
    k=((rho)/(E*r**2))*(-e**(-r)*r**(2)-2e**(-r)*r-2e**(-r)+2)
                                         ^
SyntaxError: invalid syntax
  • Classic error, Operator = XOR = Sets each bit to 1 if only one of two bits is 1, you are not working with bits, why are you using that operator? The error next is consequence of that there.

  • Camila, your e is the Euler constant? and I think you should use the specific Plot of the sympyto plot your graph, since the expression is analytical. otherwise, your expression is using the ^ instead of the ** for power, then the correct expression would be k = (r/E*r**2)*-np.e**-r*r**2*-2*np.e**-r*r-2*np.e**-r+2 the graph will still have some problems to plot, but I can help you if you give more details of these variables

  • I solved an integral, po/Er 2 integral of r 2.e -r, then the result gave this expression I put, I wanted to plot her chart

  • I edited the code for the modifications I made

1 answer

1


It was not entirely clear to me his expression. I made some hypotheses in the implementation below that, I believe, do not compromise the example.

First of all, if e is the Euller constant, the ideal is for you to use the function exp sympy native. Second, it is necessary to clarify the operator * in all multiplications. The program was not understanding expressions like 2e, for example. For this case the correct would be 2*e.

It is unclear if you want to plot a univariate function. If this is the case, you need to assign values to E and pho. This last variable has not been declared and then replaces it with the symbol p:

from sympy import *
from sympy.plotting import plot
from sympy.abc import p,E #note que as importações de símbolos podem ser feitas todas de uma vez

k=((p)/(E*r**2))*(-exp(-r)*r**(2)-2*exp(-r)*r-2*exp(-r)+2)
k

inserir a descrição da imagem aqui

Note that I changed the imports and did not set the desired range of r, this should be done only at the time of plotting the graph.

Doing the constant substitutions, we have:

p1=plot(k.subs({E:10,p:15}), (r,1,100), 
        title='comportamento do campo', xlabel='raio da esfera',
        ylabel='campo eletrico')
p1

inserir a descrição da imagem aqui

  • Thank you so much

  • 1

    Ok! If the answer met you, mark the question as solved. It is the practice of the site and I get some points tbm rs

  • as I mark as resolved? I’m new on the site, I’m not finding

  • 1

    has a V of low score of my answer. See this example: https://i.stack.Imgur.com/uqJeW.png

Browser other questions tagged

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