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!