Is it possible in Mathematica to output a two-dimensional list of any length as a step function without defining it manually, but which can be integrated at the same time? Example: I have the list
RhoArr={{-1,0.3},{-0.5,0.2},{0,0.1},{0.5,0.2},{1,0.3},...}
and I want to define it with a function $f(x)$, so we get the following output:
$$f(-1\leq x <-0.75)=0.3,\\f(-0.75\leq x <-0.25)=0.2,\\f(-0.25\leq x <0.25)=0.1,\\...$$
I have tried to proceed analogously to other programming languages, but Mathematica gives me difficulties with the integration. For example, I get the correct function values from the code (see below), the plot of the function is also perfect, but during integration I get the error (for testing I had used different numbers than above)
Part::partw: Part 1 of {} does not exist.
Part::pspec: Part specification {}[[1,1]] is neither a machine-sized integer nor a list of machine-sized integers.
NIntegrate::inumr: The integrand {{-1.,0.0326126},{-0.666667,0.0744685},{-0.333333,0.147045},{0,0.491747},{0.333333,0.147045},{0.666667,0.0744685},{1.,0.0326126}}[[{}[[1,1]],2]] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>
Here is my procedure (this is the last variant I tried)
f[x_] := Which[x > 0, Select[X, (0 <= Abs[# - x] <= HalfStep ) &, 1], x <= 0, Select[X, (0<Abs[#- x] <= HalfStep) && # <= 0 &, 1], True, -1000][[1]];
g[x_] := RhoArr[[Position[X, f[x]][[1, 1]], 2]];
Halfstep is the half step size with which the data points are separated, so you can define when an $x$-value belongs to which $y$-value and Rhoarr is the two-dimensional list. I am still very new to Mathematica, but after days of searching in vain for a solution, I would like to ask here. Thank you very much in advance.
X
? Iff
is the function you are trying to construct, what is your reason for introducingg
? This does not seem to be a well posed question. $\endgroup$