How to manually write the code for a matlab delta function

In summary, the code that you wrote calculates the Dirac delta function. However, the Mathworks website has a more accurate unit impulse function that you can use.
  • #1
O.J.
199
0
that is 0 everywhere and 1 at 0. the code I wrote was this:
n = -20:1:20;
if n==0
imp = 1
else
imp = 0;
end
>> stem (n, imp)
? Error using ==> stem at 40
X must be same length as Y.
but i got that error.

Using vectors and matrices is useless cause the delta function cannot be defined at 0 r negative values... can you please guide me through this?
 
Physics news on Phys.org
  • #2
O.J. said:
that is 0 everywhere and 1 at 0. the code I wrote was this:
n = -20:1:20;
if n==0
imp = 1
else
imp = 0;
end
>> stem (n, imp)
? Error using ==> stem at 40
X must be same length as Y.
but i got that error.

Using vectors and matrices is useless cause the delta function cannot be defined at 0 r negative values... can you please guide me through this?
Why not make imp a vector as well?
 
  • #3
how to, please a sample code pelase I am not proficient with MATLAB and the way it works but I am trying my best. I just wana see a sample code to get the hang of it...
 
  • #4
O.J. said:
how to, please a sample code pelase I am not proficient with MATLAB and the way it works but I am trying my best. I just wana see a sample code to get the hang of it...

I googled MATLAB dirac delta, and got lots of hits. Including one at Mathworks.com

http://www.mathworks.com/access/hel...=1T4GGLL_enUS301US302&q=matlab+delta+function

Does that help? It looks like the Mathworks.com website has lots of resources to help you learn MATLAB.
 
  • #5
i can't use the dirac function that comes with MATLAB because I need a modified diract function that is 0 everywhere and ONE at 0. the one that comes with MATLAB is INF at 0.
 
  • #6
O.J. said:
i can't use the dirac function that comes with MATLAB because I need a modified diract function that is 0 everywhere and ONE at 0. the one that comes with MATLAB is INF at 0.

And why do you think that is? Their Dirac delta function is mathematically correct.
 
  • #7
why or why not they implemented it that way isn't my concern. for dirac to apply to the discrete case it has to have a magnitude of 1 at zero
 
  • #9
still doesn't help. that's like the unit step function. I am looking for a function that's 0 everywhere and 1 at 0. is it hard to manually code such a thing?
 
  • #10
O.J. said:
still doesn't help. that's like the unit step function. I am looking for a function that's 0 everywhere and 1 at 0. is it hard to manually code such a thing?
I suggest that you look again at the link I gave. Yes, the Heaviside function isn't precisely what you need, but it should give you an idea of how to code it yourself.

Now, I know that it is strictly against the rules to give out complete solutions, but I am of the school that programming is best learned by example. So, I'm going to give you one possible way of defining a unit impulse at the origin.

For example, you could define a new function using a MATLAB file called impulse.m. The contents of such a file would be something like,
Code:
function [value] = impulse(x)
     % This function represents a unit impulse at the origin.
     % impulse(x) = 1 for x = 0 and 0 otherwise

     if x = 0
           value = 1;
     else
           value = 0;
     end
For more information on defining custom functions, see the official MATLAB documentation (http://www.mathworks.com/access/hel...ccess/helpdesk/help/techdoc/ref/function.htm" rather good lecture notes.

I would like to emphasise once again that this is not the Dirac Delta function, but a unit impulse at the origin.
 
Last edited by a moderator:
  • #11
Matlab function:
function Y = dirac(X)
%DIRAC Delta function.
% DIRAC(X) is zero for all X, except X == 0 where it is infinite.
% DIRAC(X) is not a function in the strict sense, but rather a
% distribution with int(dirac(x-a)*f(x),-inf,inf) = f(a) and
% diff(heaviside(x),x) = dirac(x).
% See also HEAVISIDE.

% Copyright 1993-2003 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2009/03/09 20:41:28 $

Y = zeros(size(X));
Y(X == 0) = Inf;

Do:

function Y = diracOne(X)

Y = zeros(size(X));

Y(X == 0) = 1;

So easy.

I have helped

I forgot one detail. Call the function as follows:

t = 1:10;
diracOne = f (t-2);

And you will have:

f = [0 1 0 0 0 0 0 0 0 0 0];

Good studies
 
Last edited:

1. What is a delta function?

A delta function is a mathematical concept used in signal processing and calculus. It is defined as a function that is zero everywhere except at one point, where it has an infinite value. In Matlab, it is represented by the dirac() function.

2. How do I write the code for a delta function in Matlab?

The code for a delta function in Matlab is simply dirac(t), where t is the variable or vector that the function is applied to. This will return a value of 0 for all values of t except at t = 0, where it will return an infinite value.

3. How can I plot a delta function in Matlab?

In order to plot a delta function in Matlab, you can use the plot() function along with the dirac() function. For example, to plot a delta function with t values ranging from -5 to 5, you can use the code plot(-5:0.1:5, dirac(-5:0.1:5)).

4. Can I use a delta function in an equation in Matlab?

Yes, you can use a delta function in an equation in Matlab. You can simply use the dirac() function within your equation along with the appropriate variable or vector. For example, f(x) = x*dirac(x) will return a value of 0 for all values of x except at x = 0, where it will return a value of 0.

5. Are there any alternative ways to represent a delta function in Matlab?

Yes, there are alternative ways to represent a delta function in Matlab. One way is to use the delta() function, which is similar to the dirac() function but has a different definition. Another way is to use a Kronecker delta, which can be represented by an array of zeros with a value of 1 at the desired location.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
765
  • Engineering and Comp Sci Homework Help
Replies
1
Views
916
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
909
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
820
Back
Top