Hello Zahra,
It sounds like there may be a couple of options for you, depending on what you are hoping to do with this function. Is there a particular reason you need it defined from -inf to inf? Are you looking to plot the function? Are you looking to just get a value for the function at any point on there? Or do you need to integrate or differentiate the function?
If you just want to plot it, or if you just want to evaluate at any point (like 0.1), what you probably want to do is to turn it into a MATLAB function (as opposed to a mathematical function). Rather than computing the values immediately, a function defines a procedure that can compute any value it is called with. Just make a new file in the Editor use the function keyword to assign your new function. Once you define it, save it to the current directory.
function phi = calculatePhi(m2)
if m2 >= 0 && m2 < 1
phi = 1;
else
phi = 0;
end
m2=m2+0.01;
if m2 > 15 || m2 < -15
phi = 0;
end
end
Now this code would only work for a scalar m2 and phi, not when m2 is a vector. If you want to do that, I'd suggest some logical indexing. Also, it seems like phi will only ever be non-zero when m2 is between 0 and 1, and nowhere near a magnitude of 15, even when adjusting m2 by 0.01. So the second part seemed unnecessary.
function phi = calculatePhiVectorized(m2)
phi = zeros(size(m2));
phi(m2 >= 0 & m2 < 1) = 1;
end
Now, you could call the first one with:
m2 = -1500:1499;
phi = zeros(size(m2));
for k = 1:length(m2)
phi(k) = calculatePhi(m2(k));
end
Or call the second one with:
m2 = -1500:1499;
phi = calculatePhiVectorized(m2);
Now, if you need to do some differentiation or integration, you may want to check out how to define functions with the Symbolic Math Toolbox. This allows for calculations to be done symbolically rather than numerically, though it tends to be slower. You probably don't need this though. Hope this helps.
-Cam