How to evaluate a function y = f(x) for x in an interval defined by IntervalSets.jl ?
using DomainSets
I = Interval(0,1.)
foo(x) = x^2
How to [foo(x) for x ∈ I] or map(foo,I) ?
How to evaluate a function y = f(x) for x in an interval defined by IntervalSets.jl ?
using DomainSets
I = Interval(0,1.)
foo(x) = x^2
How to [foo(x) for x ∈ I] or map(foo,I) ?
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?
Thx!
My endavor is plain and elementary. I am trying to plot or show intermediate results of MethodOfLines. For instance the functions u0(x,y,t) and v0(x,y,t) (initial conditions) in the tutorial tutorials/brusselator/ ?
Something like the method Sample, but for spatial variables x and y?
Did you try the steps on that documentation page under “Extracting Results” ?
Something like
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.)
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
But then you need to still evaluate this function on a finite number of points, e.g.
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)
Thanks you! Much appreciated.