Numerical integration while keeping the parameters
이전 댓글 표시
I have a function which is like f(a,b,x). I want to numerically integrate it with respect to x, for example using quad function and plot the result of integration with respect to parameters a and b. Can someone share how can I achieve this?
댓글 수: 2
Andrew Newell
2011년 2월 17일
What interval do you want to integrate it over?
Azar Bakili
2011년 2월 18일
채택된 답변
추가 답변 (3개)
Andrew Newell
2011년 2월 18일
Now that we know that you are integrating a function f(a,b,x) from zero to infinity, you can do something like this:
f = @(p1,p2,y) exp(-p1.*y.^2-p2.*y);
a = 0.1:.1:1;
b = 0.1:.1:1;
[a,b] = meshgrid(a,b);
g = 0*a;
for i = 1:numel(a)
fab = @(x) f(a(i),b(i),x);
g(i) = quadgk(fab,0,Inf);
end
surf(a,b,g)
John D'Errico
2011년 2월 17일
0 개 추천
A numerical integration (i.e., quad) does not allow you to keep some of the parameters symbolic. It would not be a numerical integration if it did would it? And since adaptive routines like quad are designed to use the shape of the function to decide where to put the points, they cannot leave parameters unknown.
So if you really want to keep those parameters unknown, then use the symbolic toolbox for the integration.
Of course, nothing stops you from setting the values of parameters a and b to some fixed values, then doing the integral. Use an anonymous function to do this. Then you could loop over the values of those parameters, and build a plot as you desire.
Andrew Newell
2011년 2월 18일
I am going to assume that the function you want to integrate is really just a function of x, e.g., g(x), and f(a,b) is the integral of g(x) over the interval [a,b]. Then a code like this would do what you want:
g = @(x) exp(x);
a = -1:.1:1;
b = -1:.1:1;
[a,b] = meshgrid(a,b);
f = 0*a;
for i = 1:numel(a)
f(i) = quadl(g,a(i),b(i));
end
surf(a,b,f)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!