having function from negative infinity to infinity & having a continuous function in spite of array

5 ビュー (過去 30 日間)
hi . I have a function that I put it's code below. you see that I make an array, and I access to the elements 1 to 3000. I need to have this function from negative infinity to infinity. what should I do?? my second question is that it is an array how could I have a continuous function because for example I need to access phi(.1) but because of having array, index should be just positive integer what should I do?
m2=-15;
for i2=1:3000
if (m2>=0 && m2<1)
phi(i2)=1;
else
phi(i2)=0;
end
m2=m2+0.01;
end
if(m2>15 || m2<-15)
phi = 0;
end
% sysc = d2c(phi);
m2=-1500:1:1499;
plot(m2,phi)

採用された回答

Cam Salzberger
Cam Salzberger 2017 年 8 月 29 日
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);
Then do your plotting. I'd highly recommend checking out some of the basics of creating functions here, or going through the free MATLAB Onramp training for an overview of getting started with MATLAB.
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
  4 件のコメント
Cam Salzberger
Cam Salzberger 2017 年 8 月 31 日
編集済み: Cam Salzberger 2017 年 8 月 31 日
There are several things going on here. First, bear in mind that you are not accessing phi(0.1). "Access" implies indexing. The function allows for you to calculate phi at m2 = 0.1.
Secondly, as I said above, if you want to calculate phi starting with a vector of m2 values, you need to call the vectorized code. The first function I gave was just calculating a single value of phi for a single value of m2, and was mostly a translation of your code. The second function ("calculatePhiVectorized") was cleaned up and worked on vectors of m2 values.
Thirdly, the integral function requires a function handle as input, not a vector of values. When you enter "calculatePhiVectorized(m2)", it will output a vector. integral only works properly with function handles, so that it can call the function with whatever values it decides to. The correct function call would be:
q = integral(@calculatePhiVectorized,-inf,inf)
Which would produce 1. This is expected because the function is 1 over the interval [0 1), and 0 everywhere else, so the area under the curve is just 1.
-Cam
zahra GRJ
zahra GRJ 2017 年 9 月 2 日
thank you your answer helped me a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by