integrate function

Hi!!!
I would like to ask if anyone knows what's the appropriate way to calculate the integral of this function
function out = Gr(u,x,Kt,i)
out= exp(-(u.^2)).*frustration_function2(u,x,Kt,i);
end
The integrating variable is u, and the limits are [1e-4,1e4]
Thanks!!

Réponses (2)

Andrei Bobrov
Andrei Bobrov le 25 Août 2011

0 votes

variant
x = ...;
Kt = ...;
i = ...;
quad(@(u)exp(-(u.^2)).*frustration_function2(u,x,Kt,i),
1e-4,1e4)

7 commentaires

kostas
kostas le 25 Août 2011
The problem is that the frustration_function2(u,x,Kt,i) is also an integral of another function and matlab gives error..
the problem simplified is something like that:
function out =d(x,Kt,i)
out=quad(@(u) u+x+Kt+i,0,1);
end
function out=intd(Kt,i)
out=quad(@(x) exp(x).*d(x,Kt,i),0,1);
end
Matlab gives error when i try to calculate function intd(Kt,i)
Walter Roberson
Walter Roberson le 25 Août 2011
What error does it give?
Are you taking in to account that for quad(), "The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x." ?
kostas
kostas le 25 Août 2011
the error in the example functions above is
??? Error using ==> plus
Matrix dimensions must agree.
the problem as i think is in the calculation of an integral which contains another integral...
Walter Roberson
Walter Roberson le 25 Août 2011
As I noted above, quad() will pass in vectors for the first argument and expect a vector result. quad() does not, however, define how long the vector will be; it could even be different lengths on different calls.
Your anonymous function in intd will be passed a vector, which it will pass in as "x" to d(). d() will then call quad, which will pass a vector in to the anonymous function there, dummy variable "u". But you have no reason to expect that the length of the vector quad created for "u" will be the same as the length of the vector quad created in the outer call for "x".
You would also run in to problems if Kt or i are not scalars.
You are going to have to recode d, such as to
d = @(x,Kt,i) arrayfun(@(X) quad(@u) u+X+Kt+i,0,1), x);
kostas
kostas le 25 Août 2011
all the variables are scalars not vectors and i want quad to give just one result..How can i have it?
Andrei Bobrov
Andrei Bobrov le 25 Août 2011
Hi Walter! Your last comment - the correct answer.
Bellow example, with use Maple Toolbox.
>> intd=@(Kt,i1)quad(@(x)exp(x).*arrayfun(@(X)quad(@(u)u+X+Kt+i1,0,1),x),0,1)
intd =
@(Kt,i1)quad(@(x)exp(x).*arrayfun(@(X)quad(@(u)u+X+Kt+i1,0,1),x),0,1)
>> intd(3,3)
ans =
12.169
>> syms Kt i1 x u
int(exp(x)*int(u+x+Kt+i1,u,0,1),x,0,1)
ans =
1/2 - Kt - i1 + 1/2 exp(1) + exp(1) Kt + exp(1) i1
>> double(subs(ans,[Kt,i1],[3,3]))
ans =
12.169
>>
Walter Roberson
Walter Roberson le 25 Août 2011
Ah good... it was around 4 AM when I wrote that, so I'm glad to see that I didn't do much worse than miss a "(" in the inner anonymous function!

Connectez-vous pour commenter.

kostas
kostas le 25 Août 2011

0 votes

i mean that the result of the quad need to be scalar

1 commentaire

Walter Roberson
Walter Roberson le 25 Août 2011
The results of the quad cannot be scalar. My sentence "The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x." was a direct quote from the reference documentation for quad. quad does not just do one evaluation at a time: it selects a number of x and requests that the fun do the evaluation with respect to each of them and return the vector of corresponding answers.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Question posée :

le 25 Août 2011

Community Treasure Hunt

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

Start Hunting!

Translated by