integral with vectoric varying coeficient

조회 수: 1 (최근 30일)
fima v
fima v 2020년 5월 6일
댓글: Ameer Hamza 2020년 5월 7일
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);

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 6일
편집: Ameer Hamza 2020년 5월 6일
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);
  댓글 수: 5
Ameer Hamza
Ameer Hamza 2020년 5월 7일
편집: Ameer Hamza 2020년 5월 7일
I have defined coef_fun as a function handle using interp1 so that it can be continually evaluated at all the points. If you just use interp1 alone, then you will again get a finite vector. The function handle allows the integral to evaluate interp1 at an arbitrary point.
Following code shows the difference between interp1 and polyfit
x = 0:0.1:10;
coef_vec = gensig('square', 2, 10, 0.1) + 0.1*rand(size(x)).';
coef_fun = @(xq) interp1(x, coef_vec, xq);
xq = linspace(0, 10, 200);
p = polyfit(x, coef_vec, 10);
figure;
plot(x, coef_vec, '+-', xq, polyval(p, xq), '-o')
title('Polyfit')
figure;
plot(x, coef_vec, '+-', xq, coef_fun(xq), '-o')
title('interp1')
Ameer Hamza
Ameer Hamza 2020년 5월 7일
Try this
data = load('Default Dataset4.csv');
x = data(:,1);
y = data(:,2);
[x, index] = unique(x);
coef_fun = @(xq) interp1(x, y(index), xq);
xq = linspace(3.5,23,100000);
plot(xq, coef_fun(xq))
title('interp1')

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2020년 5월 6일
Why not do it after computing the integral since it is a constant.
fun = @(x)coef_vec*exp(-x.^2).*log(x).^2;
q = integral(fun,1,10);
q=q*linspace(1,10,100);
  댓글 수: 1
fima v
fima v 2020년 5월 6일
because i want the coeffecient to be tied to the interval
so it needs to be during the integration.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by