0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ What is the definition and ourpose of you free variable X? If f is the function you are trying to construct, what is your reason for introducing g? This does not seem to be a well posed question. $\endgroup$
    – m_goldberg
    Commented Sep 6, 2020 at 2:47
  • $\begingroup$ You seem to be using two separate Mathematica accounts, which keeps you from editing your own question without approval. I recommend that you use one account only. $\endgroup$
    – bbgodfrey
    Commented Sep 6, 2020 at 3:47

2 Answers 2

1
$\begingroup$

Here's a simple way: use an interpolating function (with order 0).

rhoArr = {{-1, 0.3}, {-0.5, 0.2}, {0, 0.1}, {0.5, 0.2}, {1, 0.3}}; 
f = Interpolation[rhoArr, InterpolationOrder -> 0];
Plot[f[x], {x, -2, 2}]

enter image description here

You can use the function f just like any other function. In particular you can integrate it. Here is the integral from -1 to 1:

Integrate[f[x], {x, -1, 1}]
0.4
$\endgroup$
0
$\begingroup$

Solution

First we define the lists for the definition set and the value set, with the requirements, that the value set is distributed symmetrically around $x=0$ and that the integral over the function is unity.

NN = 3; (*number of intervals per quadrant*)
XL = N[Range[-1, 0, 1/NN]];
XL = XL[[1 ;; Length[XL] - 1]];
XR = N[Range[0, 1, 1/NN]];
XR = XR[[2 ;; Length[XR]]];
X = Join [XL, XR]];
YL = RandomReal[1/(NN), NN];
YR = Reverse [YL];
Y = Join [YL, YR];
HalfStep = N[1/(2*NN)];
RhoArr = Transpose[{X, Y}];

Now we define the desired function

g[x_] := Piecewise[
Table[{Y[[k]], 
Which[k == 1, X[[k]] <= x <= X[[k]] + HalfStep, 
k == Length[X], X[[k]] - HalfStep <= x <= X[[k]], 
True, X[[k]] - HalfStep <= x <= X[[k]] + HalfStep]}, 
{k, 1, Length@X}]];

and furthermore

Zer = (1 - NIntegrate[g[x], {x, -1, 1}])/(2*HalfStep)(*For the integral to be 1*);
X = Insert[X, 0, NN + 1]; 
Y = Insert[Y, Zer, NN + 1];
RhoArr = Transpose[{X, Y}];

NIntegrate[g[x], {x, -1, 1}]]
Plot[g[x], {x, -1, 1}, PlotRange -> {0, 0.5}]

So everything works.

Thanks to all involved.

Old response - Question 2

First: I am the one who asked the question. Unfortunately my guest account, with which I wrote this post, and the current real account, which is the one from the guest account, are not connected, although the email matches and I just added a password to the guest account. Therefore I can neither edit my question nor write a comment or ask another question about it. I am sorry for this mess, it is the first time ever that I am in a forum. If anyone can help me clean up this mess, this would be helpful.

To answer m_Goldberg's question above and provide some clarity: Here is the code where the $X$ and $Y$ lists are defined, which in turn define the 2-d list RhoArr; the definitions aim to create a 2-d list with the contents

{definition set, value set}

where the value set is to be distributed symmetrically around $x=0$.

NN = 3;
XL = N[Range[-1, 0, 1/NN]];
XL = XL[[1 ;; Length[XL] - 1]];
XR = N[Range[0, 1, 1/NN]];
XR = XR[[2 ;; Length[XR]]];
X = Join [XL, XR]];
YL = RandomReal[1/(2*NN), NN];
YR = Reverse[YL];
Y = Join[YL, YR];
Zer = 1 - Total [Y];
X = Insert[X, 0, NN + 1];
Y = Insert[Y, Zer, NN + 1];
RhoArr = Transpose[{X, Y}]
HalfStep = N[1/(2*NN)];

The problem is actually simple, at least in other programming languages, I just don't know how to solve it in Mathematica.

A short and precise summary of the problem: I have a 2-d list (RhoArr) and want to define a step function from it, which has an input value $x$ (see the mathematically written function $f(x)$ in the original post), which then can be plotted via $x$ and integrated via $x$. My above solution using the functions $f(x)$ and $g(x)$ is obviously bad, because it can only be plotted, but not integrated. The plot looks like this

enter image description here

However, if you integrate this function, the error messages from the original question will appear.

In general: such a mess won't happen to me again and I apologize for that! I will also try to formulate my problems more clearly and completely. All that remains is my justification that I have never done something like this before.

$\endgroup$
2
  • $\begingroup$ Look at the function Piecewise -- this is one way to get a function that looks like your f above. $\endgroup$
    – bill s
    Commented Sep 6, 2020 at 15:48
  • $\begingroup$ This is an ingenious advice. The solution went very quickly. The problem is that I do not know the methods of Mathematica. Thanks a lot!!! I searched for "step functions". Question: I have solved the problem now, shall I write the answer in general, together with the code that one can see how to solve it? $\endgroup$
    – Kennard
    Commented Sep 6, 2020 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.