2
$\begingroup$

I'm facing with this error when I'm trying to solve the equation :

Attempted to access x(101); index out of bounds because numel(x)=100.

actually the original equation is![![enter image description here

so I used "i+1"instead of"i" to avoid following error:

Attempted to access y(0); index must be a positive integer or logical.

n=[1:100];
yy=zeros(1,99);
xx=zeros(1,99);
y=[1 yy];
x=(.5).^n;
for i=n

  y(i)=.5*y(i-1)+2*x(i)+4*x(i-1)
end

error resolved by initializing i by 2

the improved code is :

yy=zeros(1,99);
xx=zeros(1,99);
y=[1 yy];
x=(.5).^n;
 for n=2:100

 y(n)=.5*y(n-1)+2*x(n)+4*x(n-1);
end

but the results of **y(n)**is different from what I get on paper.

$\endgroup$
  • $\begingroup$ How can I deal with it? $\endgroup$ – mahyar p Oct 19 '17 at 8:06
  • 1
    $\begingroup$ Every filter (FIR / IIR ) which accesses the information of samples from the past needs a certain delay to give a valid output (You cannot obviously access information which does not exist). You need to handle this boundary conditions in your code (in your case the first sample). You can mirror for example the first samples by appending them to the start of your x but you need to adjust the output correctly. As you already have y(1) you can also start your loop at the index 2 (for i=2:100). Side Note: Dont use i as an index as it also represents complex numbers $\endgroup$ – Irreducible Oct 19 '17 at 8:17
  • $\begingroup$ by initializing i from 2 , the error vanished..but the answer still different from what I solved manually... $\endgroup$ – mahyar p Oct 19 '17 at 11:53
  • $\begingroup$ I guess I couldn't introduce y(0)=1 properly . $\endgroup$ – mahyar p Oct 19 '17 at 12:10
  • $\begingroup$ I think you are getting your x vector in the equation over and over again. Can you calculate instantaneous X(I mean x(n)) seperately? $\endgroup$ – Furkan Küçük Oct 20 '17 at 12:27
1
$\begingroup$

Sorry for answering like this but I cannot comment since my reputation is not sufficient, but yet I want to help.

It is a filter and you can do it way easier, and more quickly than just building an algorithm(in Matlab ofcourse). It will take relatively a long post to cover all the concepts in order to make a sufficient understanding about filter designs.

However, I can help you about which topics you should cover.

Please take a look at FIR/IIR filters (Your recursive equation is basically an IIR filter) and Z-Transform topics.

If you need a quicker way to learn about IIR filter this lecture will give you some information.

$\endgroup$
  • $\begingroup$ Thanks, but i'm not supposed to do it using Z transforms or sth...I'll appreciate any helps to develop this code or other alternatives .I've heard about filter function but I don't know how I can introduce** y(0) **in it. $\endgroup$ – mahyar p Oct 19 '17 at 12:05
1
$\begingroup$

When solving a LCCDE $$ \sum_{k=0}^{N-1} a_k y[n-k] =\sum_{k=0}^{M-1} b_k x[n-k] $$

with $a_0 =1$, by recursion for $n\geq0$ like $$ y[n] = -\sum_{k=1}^{N-1} a_k y[n-k] + \sum_{k=0}^{M-1} b_k x[n-k] $$

you must also know the initial values of signals $y[n]$ and $x[n]$ for those set of indices $-\max(N,M)\leq n \leq -1$.

Most often those initial values are taken to be zero for LTI, causal systems so that $$y[0] = b_0 x[0] $$

You must include this initial condition calculation into account when solving the LCCDE via the recursion equation by a computer using some loop processing.

$\endgroup$
  • $\begingroup$ yep,but the way I'm defining my y(0) =1 seems wrong! $\endgroup$ – mahyar p Oct 19 '17 at 12:14
  • $\begingroup$ I defined it like that: y=[1 yy] means the initial value of y[n] is 1 , right? $\endgroup$ – mahyar p Oct 19 '17 at 12:32
  • $\begingroup$ When you want to refer to an index like $n=-1$, the way to realize it is to shift everything right by the amount necessary to make the minimum index to be 0 and Matlab indices begin from $k=1$ for index 0. So for example if you want to refer to $y[-3]$ or $x[-4]$ in a computation, then you should shift right by $3$ or $4$ respectively, hence when you refer to $y_s[0]$ it actually means $y[-3]$ etc. BTW as the other comment stated don't use i or j for the loop variabels in Matlab. it may cause unnecessary conflict with the imaginary number unit. $\endgroup$ – Fat32 Oct 19 '17 at 13:39

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.