# Integral with specific range (i.e t = 0:0.1:1)

14 views (last 30 days)
Fakhri Alifin on 23 Sep 2020
Commented: Star Strider on 2 Oct 2020 at 10:57
Hello everybody,
I'm a new user of matlab and still a beginner of this software. I'm using matlab R2015a by the way.
I've made a function called intensityc (t) with this code:
%NHPP on critical component (N(t))
function lc = intensityc(t)
B = 2;
ac = 0.4;
lc = (B/ac)*((t/ac).^(B-1));
end
then I was trying to call the function to another function Lamdac (t) with this code:
%big Lamda
function Lc = Lamdac(t)
Lc = integral(@intensityc, 0 ,t);
end
I have a trouble to find the value of Lamdac(t) because the "t" is ranging from 0 up to 1. I have used 'Arrayvalued' but the code still error.
Anyone can help me? I will appreciate all suggestions given to me. Thank you.

Star Strider on 23 Sep 2020
Do the integration in a loop for each value of ‘t’:
function lc = intensityc(t)
B = 2;
ac = 0.4;
lc = (B/ac)*((t/ac).^(B-1));
end
function Lc = Lamdac(t)
for k = 1:numel(t)
Lc(k) = integral(@intensityc, 0 ,t(k));
end
end
Calling it:
t = linspace(0, 1, 10);
Out = Lamdac(t).'
produces:
Out =
0
0.077160493827160
0.308641975308642
0.694444444444444
1.234567901234568
1.929012345679012
2.777777777777777
3.780864197530863
4.938271604938270
6.249999999999999
(Another option is to use arrayfun, however it is much slower and less efficient than an explicit loop.)

Show 1 older comment
Star Strider on 24 Sep 2020
My pleasure!
My code as I posted it ran without error in R2020b, and produced the results I posted.
I do not understand what the problem is when you run it. What was the exact code you ran? How did you run it?
Note that you must run it from a script as I noted in my Answer:
t = linspace(0, 1, 10);
Out = Lamdac(t).'
You cannot run it by pressing the green Run arrow in the editor. It will not run correctly that way.
Please copy and paste the entire error message (all the red text that appears in your Command Window). That way, I may be able to understand.
Fakhri Alifin on 2 Oct 2020 at 6:14
Star Strider on 2 Oct 2020 at 10:57
My pleasure!
.

### Community Treasure Hunt

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

Start Hunting!

Translated by