Error if lambdify target function is zero

inkie
Thu, 01 May 2025 01:31:57 +0000
General Usage

Hi guys, im implementing the fourier functions in julia + sympy but when i have an odd function my bn is not working as expected since bn(1) for example is not working giving this error: MethodError: no method matching (::SymPyCore.var"#260#261")(::Int64)

this is my code i could just remove the bn value but i was thinking if there is a workaround for this

begin
	x,y = symbols("x y", real=true)
	n = symbols("n", integer=true, positive=true)

	a0_sym = (1/(2*PI)) * (integrate(Sym(1),(x,-PI/2, PI/2)))
	an_sym = (1/PI) *(integrate(cos(n*x),(x,-PI/2,PI/2))) |>simplify
	bn_sym = (1/PI)*(integrate(sin(n*x), (x, -PI/2, PI/2))) |> simplify

	a0 = a0_sym.evalf()
	an = lambdify(an_sym)
	bn = lambdify(bn_sym)

	# Rango de x
	xs = range(-π, π, length=500)

	function fourier(x; N=5)
		y = a0
		for n in 1:N
			y += an(n)*cos(n*x) + bn(n)*sin(n*x)
		end
		return y
	end

	bn(1)
end

3 posts - 2 participants