Problem with integrals

3 vues (au cours des 30 derniers jours)
Jon
Jon le 1 Mai 2011
I have a integral that simplified looks like this:
quad(@(z) exp(z.^2),-2,x)
The problem here is quite obvious. You can't use quad quad a variable as a limit. So why don't I use int? Because exp(z^2) is an analytically unsolvable integral. In the task I'm trying to solve there is a second integral of the first integral and simplified it looks like this:
quad(@(x) exp(x.^2).*quad(@(z) exp(z.^2),-2,x),-2,2)
And according to my mentor there should be a way to solve this because he says that the first integration will make the other one solvable. The code above gives this error:
??? Error using ==> quad at 70
The limits of integration must be scalars.
Error in ==> @(x)exp(x.^2).*quad(@(z)exp(z.^2),-2,x)
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> test at 54
quad(@(x) exp(x.^2).*quad(@(z) exp(z.^2),-2,x),-2,2)
So I thought I should ask you if there is a way to do what I'm trying to do (with any command, it does not have to be quad) or if its not possible.

Réponse acceptée

Teja Muppirala
Teja Muppirala le 1 Mai 2011
A very good question. You need to use QUADV here instead of QUAD.
quadv(@(x) exp(x.^2).*quad(@(z) exp(z.^2),-2,x),-2,2)
QUAD expects vector inputs and outputs, and so when it goes to evaluate the inner QUAD, it sends in a vector which is why you get that error.
Notice that QUAD will error on a problem like this one here below for the same reason (but QUADV will work fine):
quad(@(x) det([x 1; 1 1]),0,1)
QUADV does it without trying to send in vectors.
Just as a sanity check, compare what you get from QUADV with what you get if you did it using TRAPZ
f = @(x) exp(x.^2).*quad(@(z) exp(z.^2),-2,x);
trapz(arrayfun(f,-2:.001:2)) * 0.001
quadv(f,-2,2)
Both answers are very similar, about 541.38.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by