I have an integral expression which I defined on Matlab using
x = 0:1/1000:1;
g = @(x) (exp(-1./x.^2).*heaviside(x)).*(exp(-1./(1-x).^2).*heaviside(1-x));
t = 0:1/1000:1;
f = zeros(size(t));
for i = 1:length(t)
f(i) = integral(g,0,t(i));
end
I can plot it, for example, using plot(t,f)
, but for other purposes I would like to attach a function handle to f
, i.e. something like f = @(t) zeros(size(t))
. I have not been able to figure it out thus far. f = @(t) integral(@(x)g(x),0,t)
is also not sufficient.
Sorry, I can't comment yet. But does this work?
funcHand= @(t) integral(g,0,t);
You don't have to define x in your code above, since the input to integral
is a function handle.
Then to check it's the same:
f2 = zeros(size(t));
for i = 1:length(t)
f2(i) = funcHand(t(i));
end
Whoops, the other answer said all the above (just replaced the for loop with arrayfun. I didn't see it while writing the answer.
Edit
If you want to build-in the for loop, try:
funcHand= @(t) arrayfun(@(u) integral(g, 0, u),t);
And test:
plot(funcHand(t))