Hello, good afternoon.
Im trying to plot a piecewise function using sympy.jl and plots.jl withou success. May someone help please?
let
@syms y C
# Define os pontos do gráfico. Eles são necessários
# para encontrar a área da forma de onda que iremos integrar.
p1 = point(0, 0)
p2 = point(1, 50)
p3 = point(2, 0)
p4 = point(3, -50)
p5 = point(4, 0)
@info lineexp
# y - yn = m(x -xn). Devemos usar a função linear e calcular
# o coeficiente angular(m), e o coeficiente linear (c).
# O coeficiente angular é encontrado por Δy/Δx.
# 0 < t < 1
m1 = (p2.y - p1.y)/(p2.x - p1.x)
l1 = first(solve(subs(lineexp, m=>m1, c=>0), y))
# 1 < t < 3
m2 = (p3.y - p2.y)/(p3.x - p2.x)
l2 = first(solve(subs(y - p2.y ~ m*(t - p2.x), m=>m2), y))
# 3 < t < 4
m3 = (p5.y - p4.y)/(p5.x - p4.x)
l3 = first(solve(subs(y - p4.y ~ m*(t - p4.x), m=>m3), y))
# Olhe na figura de forma de onda do exemplo
v = sympy.Piecewise(
(l1, Gt(t, 0) & Gt(1, t)),
(l2, Gt(t, 1) & Gt(3, t)),
(l3, Gt(t, 3) & Gt(4, t))
)
@info v
# Calcularemos a função de i
fi = C*diff(v)
@info fi
iresult = simplify(subs(fi, C=>200e-6))
@info iresult
firesult = lambdify(iresult, t)
time = LinRange(0, 4, 1000)
ydata = firesult.(collect(time))
plot(time, ydata)
end
MethodError: objects of type Float64 are not callable
The object of type `Float64` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Maybe you forgot to use an operator such as *, ^, %, / etc. ?
but the function is failing on line ydata = firesult.(collect(time))
MethodError: objects of type Float64 are not callable
The object of type `Float64` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Maybe you forgot to use an operator such as *, ^, %, / etc. ?
Cant understand as the data type is a function with a t parameter
They are just numbers to pass to plot (0 and 4 in your case), as any other function would be plotted. (The issue here, is the lambdify call doesn’t work well with the last condition. If you put true instead of Gt(t, 3) & Gt(4, t) then the plot should happen as desired: plot(v, 0, 4; lt=:step).
Yep. It worked. This shows that the issue was the NAN in piecewise function.
Thank you. When you add the condition (0, true), the result in the differentiated function was (0, otherwise).