# Impulse calculation

I have written this MATLAB function to plot the impulse response. But it doesn't give me the desired output (impulse function). I am new to MATLAB. Can anyone tell me where i went wrong with the code?

   function [] = impulse()
t=-10:1:10;
x=zeros(length(t));
for(i=-10:1:10)
if(i==0)
x=1;
end
plot(t,x);
xlabel('t');
ylabel('delta(t)');
title('impulse response');
axis([-10,10,-1,2])
end


In your code there is no end statement for the loop.There should be an end statement after the end for the if statement.Also x=1 inside the if condition converts the x vector into a vector of length 1.It should be changed as x(i+11)=1.Then the code works to give you a plot of a delta function

If you write zeros(length(t)) you are creating a matrix with length(t) rows and length(t) columns. Instead, you should write zeros(1, length(t)) in order to have an array. Also you don't need a for loop. Try this:

   t = -10:1:10;
x = zeros(1, length(t));
x(t==0) = 1;


However, an end is missing in your code, because both if and for need it, and you should write x(i) in order to insert 1 in that specific position.

• Any chance to elaborate, instead of simply pasting the code?
– jojek
Jul 8, 2015 at 11:30
• This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
– jojek
Jul 8, 2015 at 11:30
• I edited my answer. Jul 8, 2015 at 12:04