MATLAB: Integral with vectoric varying coeficient

coeffloopvector

Hello , i have a basic function
exp(-x.^2).*log(x).^2
which i integrate in a certain interval.
i want to multiply my basic function with a vectoric coefficient called coef_vec that varies with the interval.
so if the integral is at 5 my basic function whould be multiplied with coef_vec(5).
i know that its some how turning the integral into a loop.
Is it possible in matlab?
Thanks.
coef_vec=linspace(1,10,100)
fun = @(x)coef_vec*exp(-x.^2).*log(x).^2;
q = integral(fun,1,10);

Best Answer

You need to consider that value of x is not always an integer, it can be a floating-point too. In that case, coef_vec(x) will fail. You need to interpolate your vector to give value on that range. Try this code
coef_vec = linspace(1,10,100);
coef_fun = @(x) interp1(1:numel(coef_vec), coef_vec, x);
fun = @(x) coef_fun(x).*exp(-x.^2).*log(x).^2;
q = integral(fun,1,10);