Chelz
Chelz

Reputation: 447

Plotting the Impulsefunction

so I'm new to Matlab and had to draw the Impulsefunction with y(n) is only 1 if n == 3, else 0. The following code works:

n = -5:5;                       
f = n;                          % allocate f
for i = 1 : length(n)           
    f(i) = dd1(n(i)-3);         
end                            

stem(n, f);                     
function y = dd1(n)            
    y = 0;                    
    if n == 0                
        y = 1;              
    end
end

But I feel like it's to complicated, so I tried the following:

n = -5:5
stem(n, fo)
function y = fo(n)
    y = 0
    if n == 3
        y=1
    end
end

This returns

Not enough input arguments.

Error in alternative>fo (line 5)
    if n == 3

Error in alternative (line 2)
stem(n, fo)

I feel like I'm missing something trivial here.

Upvotes: 0

Views: 20

Answers (1)

Max
Max

Reputation: 4045

if is no vector-wise operation but expects a single boolean (or at least a scalar that it can cast to a boolean).

But you can do this vector-wise:

lg = n == 3;

This produces a logical (MATLAB's name for boolean) array (because n is an array and not a vector), which is true where n is equal (==) to three. So you don't need a function, because you can make use of MATLAB's ability to work with vectors and arrays implicitly. (for your code it would be f = (n-3) == 3)

A last hint: if you have a state-space system (ss-object), you can use the function step to get the step-response as a plot.

Upvotes: 1

Related Questions