MATLAB: Error: “First input argument must be a function handle.” when taking an integral

errorintegral

I am new to matlab and I am trying to figure out out to take an integral of the "fun" for every alue of "n". "First input argument must be a function handle." is the error that occurs.
Anything helps, thanks.
syms a b n
n = [0.60 0.62 0.64 0.66 0.68 0.70];
a=5.05;
b=2;
fun=@(w)(1./(a.*w.^n-b.*w));
for i=1:length(n)
t11(i)=integral(fun(i),0.5,10)
end
[SL: Formatted code as code not text]

Best Answer

Let's step through your code.
syms a b n
This line is completely unnecessary since you immediately overwrite those variables on the next three lines of code.
n = [0.60 0.62 0.64 0.66 0.68 0.70];
a=5.05;
b=2;
fun=@(w)(1./(a.*w.^n-b.*w));
for i=1:length(n)
t11(i)=integral(fun(i),0.5,10)
end
fun(i) returns a number, not a function handle, so the error message you received is correct. You have two options off the top of my head:
1) Tell integral that your function is array valued.
fun=@(w)(1./(a.*w.^n-b.*w));
T = integral(fun, 0.5, 10, 'ArrayValued', true);
2) Have your function handle accept both w and the index into n. In your integral call, use an anonymous function "adapter" to present the appeance of a 1-input function handle to integral.
fun = @(w, ind) 1./(a.*w.^n(ind)-b.*w);
T3 = integral(@(w) fun(w, 3), 0.5, 10)
% Check the two results
abs(T3-T(3)) % Should be small