Using integral function for a function which is defined with an integral
Show older comments
I want to use the integral function in Matlab 2012a to integrate the function
phi = @(x) integral(@(t) exp(-t.^2./2),-inf,x);
but I get an error when the integrate function passes vector-valued x to
integral(@(t) exp(-t.^2./2),-inf,x);
I tried to define a function
function out = innerIntegralMultiple(x)
inner = @(t) exp(-t.^2./2);
if(isscalar(x))
out = integral(inner,-inf,x);
return;
end
out = zeros(length(x),1);
for i=1:length(x)
out = integral(inner,-inf,x(i));
end
end
and define phi as
phi = @(x) innerIntegralMultiple(x)
but I get errors about the number of arguments the function returns when I try this definition.
Is there a way to do what I want?
Answers (1)
Mike Hosea
on 5 Jun 2012
>> phi = @(r)arrayfun(@(x)integral(@(t)exp(-t.^2./2),-inf,x),r);
>> phi(-3:3)
ans =
Columns 1 through 5
0.003383692573953 0.057026123992892 0.397689745423351 1.253314137315500 2.108938529207654
Columns 6 through 7
2.449602150637674 2.503244582076109
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!