Numerical integration while keeping the parameters
Afficher commentaires plus anciens
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 commentaires
Andrew Newell
le 17 Fév 2011
What interval do you want to integrate it over?
Azar Bakili
le 18 Fév 2011
Réponse acceptée
Plus de réponses (3)
Andrew Newell
le 18 Fév 2011
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
le 17 Fév 2011
0 votes
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
le 18 Fév 2011
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)
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!