0

I want to graph \$h[n] = \frac{1}{n+1} \ n\in [0,29] \$

I tried the next code, but won't compile:

n=linspace(0,29,30);               
h =1/(1+n);                                   
figure                                                  
stem (n,h);  
Trygve Laugstøl
  • 1,410
  • 2
  • 19
  • 28

1 Answers1

4

You need to add a dot in front of the divide symbol (this means array divide as opposed to matrix divide):

n=linspace(0,29,30);
h =1./(1+n);
figure
stem (n,h);

Resulting Graph:

Matlab Example

To find out more, type help . - you will get something like this (this is only part of the result, as it's rather long...):

>> help .
  Operators and special characters.

  Arithmetic operators.
    plus       - Plus                               +    
    uplus      - Unary plus                         +    
    minus      - Minus                              -    
    uminus     - Unary minus                        -    
    mtimes     - Matrix multiply                    *    
    times      - Array multiply                    .*    
    mpower     - Matrix power                       ^    
    power      - Array power                       .^    
    mldivide   - Backslash or left matrix divide    \    
    mrdivide   - Slash or right matrix divide       /    
    ldivide    - Left array divide                 .\    
    rdivide    - Right array divide                ./    
Oli Glaser
  • 55,140
  • 3
  • 76
  • 148
  • 1
    I would like to add that n=0:30; is cleaner and easier. In practice, this error is solved by adding dots util it works. Eventually, you will know the cases in which it is needed. – Serge Jan 05 '13 at 16:36
  • 1
    @Serge - agreed, a simple integer array might be better. Matlab gives you plenty of hints on the errors too. – Oli Glaser Jan 05 '13 at 17:48