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 comentarios

Andrew Newell
Andrew Newell el 17 de Feb. de 2011
What interval do you want to integrate it over?
Azar Bakili
Azar Bakili el 18 de Feb. de 2011
It is a definite integral from zero to infinity, for this reason I started with quadgk.

Iniciar sesión para comentar.

 Respuesta aceptada

Matt Tearle
Matt Tearle el 18 de Feb. de 2011
Or, if it's actually a function of x with two parameters, and integrated over a fixed interval, then a variation of Andrew's answer will do it
f = @(x,a,b) sin(a*x+b);
x1 = 0;
x2 = pi;
[a,b] = meshgrid(-1:0.1:1);
z = zeros(size(a));
for k = 1:numel(a)
g = @(x) f(x,a(k),b(k));
z(k) = quad(g,x1,x2);
end
surf(a,b,z)

5 comentarios

Azar Bakili
Azar Bakili el 18 de Feb. de 2011
Thank you very much, it was really helpful...
Andrew Newell
Andrew Newell el 18 de Feb. de 2011
You won the "guess what the problem really is" lottery!
Matt Tearle
Matt Tearle el 18 de Feb. de 2011
Hah, sorry. I feel a bit cheap ripping off your solution (although if it's any consolation, I was basically thinking the same approach as you anyway). I upvoted your second answer so it's more visible (given that it's the best answer for this specific question, in the end)
Andrew Newell
Andrew Newell el 18 de Feb. de 2011
Don't worry, I don't mind. I think a lot of us get tired waiting to find out what the problem really is and just start offering solutions. We could call it extra content!
Andrew Newell
Andrew Newell el 18 de Feb. de 2011
I should add that I didn't think you were ripping off my solution - I assumed that you were just thinking along the same lines as me. And your answer really is the best choice because it is more general. So I voted yours up!

Iniciar sesión para comentar.

Más respuestas (3)

Andrew Newell
Andrew Newell el 18 de Feb. de 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
John D'Errico el 17 de Feb. de 2011

0 votos

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
Andrew Newell el 18 de Feb. de 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)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by