Inner product of functions on an interval with Trapz.jl

x = range(0.,π,length = 1000)
u1 = sin.(x)
u2 = cos.(x)
ip = trapz(x,prod(hcat(u1,u2),dims=2))

AssertionError: Integration axis over yis incompatible withx. Make sure their length match!

Can’t figure out why it’s not compatible!

Works using NumericalIntegration with exact same syntax (defaults to trapezoid rule), changing trapz to integrate.

Trapz.jl works with input vector instead of 1000×1 Matrix:


However, it would be simpler to broadcast the product of two arrays:

trapz(x, u1 .* u2)

Note that gaussian quadrature will likely be significantly faster.

1 Like