0
$\begingroup$

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 som 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);
$\endgroup$

1 Answer 1

0
$\begingroup$

If you have a closed-form expression for the weighting function, you can use it directly. Here, the weight is linear, so you can replace coef_vec by xand be ok.

fun = @(x)x.*exp(-x.^2).*log(x).^2;
q = integral(fun,1,10);

If not, an option is to define coef_vec on a larger interval, interpolate it with a suitable closed-form interpolating function, and use this under the integral.

Or, simply, evaluate the integral with a function like trapz. Here is a comparison, with close results

coef_vec=linspace(1,10,100);
fun0 = @(x)exp(-x.^2).*log(x).^2;
fun1 = @(x)x.*exp(-x.^2).*log(x).^2;
q1 = integral(fun1,1,10);

q2 = trapz(coef_vec,coef_vec.*fun0(coef_vec));
disp([q1 q2])

>>    0.0245    0.0245
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.