How to run an exponential function with sympy.Symbol?

Question:

I am trying to plot a Piecewise Defined Function in Python. I have figured out how to get it to plot, however, one of the formulas in the Piecewise reads: 4 e^(0.1x)sin(2 x) X E [−10 ,- ]

I tried including the Exponential function of it (e^(0.1x)) but it gives me the following error:
TypeError: Cannot convert expression to float

It works without the exponential.

I first tried with:

import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
from sympy.plotting import *
import math


x = sp.Symbol("x")
f1 = 4*sp.pi*math.exp(0.1*x)*sp.sin(2*sp.pi*x)
f2 = 0
sp.plot((f1, (x,-10*sp.pi,-sp.pi)),(f2, (x,-sp.pi,-sp.pi/2))) 

Which gives me the error. I know the problem is that the "x" which equals sp.Symbol("x") cannot be used in math.exp(). I’ve tried converting it to a float with Float(), Complex(), sp.evalf() and sp.N() but none of them work. I would very much appreciate any help

Asked By: AetherBorn

||

Answers:

You cannot mix functions from math and sympy. Look at what you did:

f1 = 4*sp.pi*math.exp(0.1*x)*sp.sin(2*sp.pi*x)

Here you used the math.exp into a symbolic expression. You need to use Sympy’s exponential function:

f1 = 4*sp.pi*sp.exp(0.1*x)*sp.sin(2*sp.pi*x)
Answered By: Davide_sd
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.