PDA

View Full Version : How to manually write the code for a matlab delta function...


O.J.
Apr24-09, 10:33 AM
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?

Hootenanny
Apr24-09, 10:48 AM
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?

O.J.
Apr24-09, 03:46 PM
how to, please a sample code pelase im not proficient with matlab and the way it works but im trying my best. I just wana see a sample code to get the hang of it...

berkeman
Apr24-09, 04:07 PM
how to, please a sample code pelase im not proficient with matlab and the way it works but im 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/helpdesk/help/toolbox/symbolic/index.html?/access/helpdesk/help/toolbox/symbolic/dirac.html&http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=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.

O.J.
Apr25-09, 01:55 AM
i cant 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.

berkeman
Apr25-09, 02:05 AM
i cant 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.

O.J.
Apr25-09, 02:28 AM
why or why not they implemented it that way isnt my concern. for dirac to apply to the discrete case it has to have a magnitude of 1 at zero

Hootenanny
Apr25-09, 03:35 AM
why or why not they implemented it that way isnt my concern. for dirac to apply to the discrete case it has to have a magnitude of 1 at zero
So you don't really want the Dirac delta function, do you? Try the Heaviside function, http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/index.html?/access/helpdesk/help/toolbox/symbolic/heaviside.html

O.J.
Apr25-09, 03:43 AM
still doesnt help. thats like the unit step function. im looking for a function thats 0 everywhere and 1 at 0. is it hard to manually code such a thing?

Hootenanny
Apr25-09, 04:26 AM
still doesnt help. thats like the unit step function. im looking for a function thats 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 learnt 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,

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 (here (http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/function.htm)), or these (http://www4.comp.polyu.edu.hk/~csygliu/COMP319/Lab%204.pdf) 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.

glp40hs
May17-10, 09:00 PM
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