# Inner product of functions on an interval with Trapz.jl

**URL:** <https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505>\
**Category:** General Usage\
**Created:** [July 29, 2021, 4:39pm UTC](https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505 "2021-07-29T16:39:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [July 29, 2021, 4:39pm UTC](https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505/1 "2021-07-29T16:39:53Z")

</div>

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

```

`AssertionError: Integration axis over `y`is incompatible with`x`. Make sure their length match!`

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

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [July 29, 2021, 4:43pm UTC](https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505/2 "2021-07-29T16:43:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 29, 2021, 5:07pm UTC](https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505/3 "2021-07-29T17:07:26Z")

</div>

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

```julia
trapz(x,prod(hcat(u1,u2),dims=2)[:])

```

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

```julia
trapz(x, u1 .* u2)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 29, 2021, 7:49pm UTC](https://discourse.julialang.org/t/inner-product-of-functions-on-an-interval-with-trapz-jl/65505/4 "2021-07-29T19:49:19Z")

</div>

Note that gaussian quadrature will likely be significantly faster.
