I am trying to implement this computation in Matlab, but I don't know how.

I want to compute the following:

$$p(k,j) = \frac{\sum\limits_{i=j+1}^{k} x_{i-1}}{\sum\limits_{i=j+1}^{k}(y_{i-1} - y_i)}$$ where $j = 0,\ldots,k-1$. For example for $k = 2$, $j = 0,1$. Thus,

$$ p(2,0) = \frac{\sum\limits_{i=1}^{2}x_{i-1}z_{i-1}}{\sum\limits_{i=1}^2 (y_{i-1}-y_i)} \quad \text{and} \quad p(2,1) = \frac{\sum\limits_{i=2}^{2}x_{i-1}z_{i-1}}{\sum\limits_{i=2}^2 (y_{i-1}-y_i)} $$

For $k=3$, $j=0,1,2$ and $$p(3,0) = \frac{\sum\limits_{i=1}^{3}x_{i-1}z_{i-1}}{\sum\limits_{i=1}^3 (y_{i-1}-y_i)}\,,p(3,1) =\frac{\sum\limits_{i=2}^{3}x_{i-1}z_{i-1}}{\sum\limits_{i=2}^3 (y_{i-1}-y_i)} \,, p(3,2) =\frac{\sum\limits_{i=3}^{3}x_{i-1}z_{i-1}}{\sum\limits_{i=3}^3 (y_{i-1}-y_i)} ~.$$

I have the values of $x$ and $y$ but I only have the initial value of $z$, $z_0$. The next value of $z$ is found iteratively: $$z_i = z_{i-1} - 1/p(k,j). $$

  • $\begingroup$ What does initial value of $z$ mean? Only $z_0$? $\endgroup$ – Proved Maroon Z Jun 26 '17 at 2:56
  • $\begingroup$ Yes, only $z_0$. The next value of z depends on the $p(k,j)$. $\endgroup$ – L.bronze Jun 26 '17 at 3:06
  • $\begingroup$ So what is the question? $\endgroup$ – Jonas Dahlbæk Jun 26 '17 at 7:55
  • 1
    $\begingroup$ You could notice that ${\sum\limits_{i=j+1}^{k}(y_{i-1} - y_i)}=y_j-y_k$ $\endgroup$ – Claude Leibovici Jun 26 '17 at 10:01
  • 1
    $\begingroup$ If the question had to do with what you've worked out in the question post, it's on topic here. Questions about how to implement in Matlab, other software, etc. then your question is off topic. $\endgroup$ – amWhy Jun 26 '17 at 12:22

Matlab has one-based array indexing: x(1), x(2) and so on. You need to take it into account. If p(k,j) is two-dimensional array, then you need to change indexing too.

Let: x_(i+1) = x(i), y_(i+1) = y(i), z_(i+1) = z(i), p_(k+1,j+1) = p(k,j)

So, $p(k,j)$ is calulated, using element-wise multiplication .*, difference and vector sum operations, with this code:

p_(k+1,j+1) = sum(x_(j+1:k).*z_(j+1:k))/sum(y_(j+1:k)-y_(j+2:k+1))

One more hint, MATLAB has built-in function diff, calculating difference of neighbour elements. diff(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]. So we can write:

p_(k+1,j+1) = sum(x_(j+1:k).*z_(j+1:k))/sum(-diff(y_(j+1:k+1)))
| cite | improve this answer | |
  • $\begingroup$ Thanks for your response. Would I need a for loop? $\endgroup$ – L.bronze Jun 26 '17 at 11:41
  • $\begingroup$ Yes you need, because your variable $z_i$ is calculated iterative way. I do not know equation for $z_i$, probably you need a nested loop for $j$ and $k$. $\endgroup$ – Proved Maroon Z Jun 26 '17 at 14:57
  • $\begingroup$ Thanks. I gave the equation for $z_i$ as $z_i = z_{i-1} - 1/p(k,j)$. $\endgroup$ – L.bronze Jun 26 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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