How to write trigonometric polynomial in python?

How to write a+b*cos x in python.?
i want to write as expression

>>> from math import cos
>>> a=2
>>> b=1
>>> x=3.14
>>> a + b*cos(x)

Hope this is what you’re looking for :slight_smile:

i am looking for ,how to write trignometric polynomial in python.

Assume you have a math function like this:
f(a,b,x) = a + b cos(x)

You could write a python function for this:

def f(a, b, x):
  return a + b*cos(x)

Or in short-hand:

f = lambda a,b,x: a + b*cos(x)

Now, f(2, 1, 3.14) should give you the answer for 2 + 1cosπ as 1.

I want expression only not the value.
I am approximating a continuous infinite differententiable by trignometric polynomial.
let say if f(x)=x then trigonometric polynomial should be in a+bcosx+csinx.

so i want experession for curve.i dont want value at a point.

If you want to write the math expressions for display purposes, you could use Latex on Jupyter notebooks.

Otherwise, if want to plot the graphs for expression, you should compute the values like @GokulNC explained above for the set of points in your input expression.

Suppose if you want to draw a curve for y = sin(x) for x = 0,1,2…10


will give you the list of y values for the given x.

For more complex expressions, you can use sympy.

How to use latex and sympy in jupyter?

In sympy, you declare the symbols first like this:

import sympy
from sympy import Symbol
x = Symbol('x')
a = Symbol('a')
b = Symbol('b')

Then for your ease of use, you can define a python function like this:

def f(a, b, x):
  return a + b*sympy.cos(x)

Then you can easily convert it to a symbol expression by:

>>> f(a,b,x)
a + b*cos(x)

For evaluation:

>>> f(2,1,3.14)

For more details, check SymPy Tutorial.
There are multiple ways to achieve the above, that was just an example.

For LaTeX on Jupyter Notebook, check this: