# How to evaluate a function y = f(x) for x in Interval defined by Domainset?

**URL:** https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300
**Category:** Specific Domains
**Tags:** question
**Created:** [December 2, 2025, 5:35pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300 "2025-12-02T17:35:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)
#### Post date: [December 2, 2025, 5:35pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300/1 "2025-12-02T17:35:03Z")

</div>

How to evaluate a function y = f(x) for x in an interval defined by [IntervalSets.jl](https://juliamath.github.io/IntervalSets.jl/stable/) ?

```julia-auto
using DomainSets

I = Interval(0,1.)

foo(x) = x^2

```

How to `[foo(x) for x ∈ I]` or `map(foo,I)` ?

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [December 2, 2025, 5:45pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300/2 "2025-12-02T17:45:09Z")

</div>

Could you share some of the context of this problem?

I’m not sure what you mean exactly by evaluating a function in an interval. Technically there are infinitely many values in an interval (of reals), so something like `[foo(x) for x ∈ I]` would not really work? With data types on a computer like `Int` or `Float64` there are only a finite number of numbers in the interval of course, but these numbers are most likely still far too large to be used in practice. `Interval` doesn’t seem to be iterable, probably for that exact reason.

But perhaps you just want to have “samples” from the function evaluated at some more reasonable number points within the interval? Or just at the end points?

---

<div class="post-metadata">

### Author: ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)
#### Post date: [December 2, 2025, 5:55pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300/3 "2025-12-02T17:55:56Z")

</div>

Thx!

My endavor is plain and elementary. I am trying to plot or show intermediate results of [MethodOfLines](https://docs.sciml.ai/MethodOfLines/stable/). For instance the functions u0(x,y,t) and v0(x,y,t) (initial conditions) in the tutorial [tutorials/brusselator/](https://docs.sciml.ai/MethodOfLines/stable/tutorials/brusselator/) ?

Something like the method [Sample](https://docs.sciml.ai/ModelingToolkit/dev/API/model_building/#ModelingToolkit.Sample-API-model_building), but for spatial variables x and y?

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [December 3, 2025, 7:38am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300/4 "2025-12-03T07:38:30Z")

</div>

Did you try the steps on that documentation page under “Extracting Results” ?

> **[Getting Started · MethodOfLines.jl](https://docs.sciml.ai/MethodOfLines/stable/tutorials/brusselator/#Extracting-results)**
>
> Documentation for MethodOfLines.jl.

Something like

```julia
discrete_x = sol[x]
discrete_y = sol[y]
discrete_t = sol[t]

solu = sol[u(x, y, t)]
solv = sol[v(x, y, t)]

```

and then (might differ depending on what plotting package you use; or if you want to have a 2D plot etc.)

```julia
plot(discrete_x, solu[eachindex(discrete_x), firstindex(discrete_y), firstindex(discrete_t)])

```

It’s a bit verbose, but all that is happening here is that the ODE solutions will already return the solution evaluated at certain points and put it in arrays (`discrete_x`, `solu` etc.). You just need to choose which values you want to plot.

You can also get functions out of the solution object as detailed here: [Solution Interface - PDESolutions · MethodOfLines.jl](https://docs.sciml.ai/MethodOfLines/stable/solutions/#sol)

But then you need to still evaluate this function on a finite number of points, e.g.

```julia
t_value = 0.0
y_value = 0.5
x_range = 0.0:0.01:1.0
u_values = u_function.(t_value, x_range, y_value)

plot(x_range, u_values)

```

---

<div class="post-metadata">

### Author: ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)
#### Post date: [December 3, 2025, 7:57am UTC](https://discourse.julialang.org/t/how-to-evaluate-a-function-y-f-x-for-x-in-interval-defined-by-domainset/134300/5 "2025-12-03T07:57:21Z")

</div>

Thanks you! Much appreciated.
