Integration of function with two variables with respect to one of them

12 visualizaciones (últimos 30 días)
Hi,
I'm trying to evaluate the integral of
exp(i*k*r)*(C1*cos(P*r)+C2*sin(P*r)+C3*cosh(P*r)+C4*sinh(P*r))
with respect to "r" from 0 to 1, where P, C1, C2, C3 and C4 are constants, "i" is sqrt(-1).
I have tried integral command but is says 'k' is not defined.
Any suggestions will be appreciated.
Thanks,
Mohammad

Respuestas (3)

David Sanchez
David Sanchez el 19 de Dic. de 2013
syms P C1 C2 C3 C4 k r
int(exp(1i*k*r)*(C1*cos(P*r)+C2*sin(P*r)+C3*cosh(P*r)+C4*sinh(P*r)),r,0,1)
ans =
C2*(P/(P^2 - k^2) - (exp(k*i)*(P*cos(P) - k*sin(P)*i))/(P^2 - k^2)) - C1*((k*i)/(P^2 - k^2) - (exp(k*i)*(P*sin(P) + k*cos(P)*i))/(P^2 - k^2)) - C4*(P/(P^2 + k^2) - (exp(- P + k*i)*(P + k*i + P*exp(2*P) - k*exp(2*P)*i))/(2*(P^2 + k^2))) + C3*((k*i)/(P^2 + k^2) - (exp(- P + k*i)*(P + k*i - P*exp(2*P) + k*exp(2*P)*i))/(2*(P^2 + k^2)))
  1 comentario
Mohammad Gharaibeh
Mohammad Gharaibeh el 19 de Dic. de 2013
Thanks David. Actually the constants P, and C1-C4 they have values and these values are changing in the loop. I cant define them as symbols. Any specific way to do the integration in this case?

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 19 de Dic. de 2013
Editada: Walter Roberson el 12 de Jun. de 2017
You cannot do numeric integration when there are "free variables" in the expression.
You can use the Symbolic Toolbox
syms C1 C2 C3 C4 P r
int( exp(i*k*r)*(C1*cos(P*r)+C2*sin(P*r)+C3*cosh(P*r)+C4*sinh(P*r)), r, 0, 1)
If you simplify the results a bit you might be able to get down to
(-(C3-C4) * (P+k) * (P+i*k) * (P-k) * exp(i*k-P) - (C3+C4) * (i*k-P) * (P+k) * (P-k) * exp(P+i*k) + 2*(P^2 + k^2) * ((i * k * C1 - C2 * P) * cos(P) + sin(P) * (i * k * C2 + P * C1)) * exp(i*k) + (2*C2-2*C4) * P^3 - (2*i) * k * (C1-C3) * P^2 + 2 * k^2 * (C2+C4) * P - (2*i) * (C1+C3) * k^3) / (2*P^4-2*k^4)
  2 comentarios
Mohammad Gharaibeh
Mohammad Gharaibeh el 19 de Dic. de 2013
Thanks Walton. Actually the constants P, and C1-C4 they have values and these values are changing in the loop. I cant define them as symbols. Any specific way to do the integration in this case?
Walter Roberson
Walter Roberson el 20 de Dic. de 2013
If you already have values for those constants at the time you do the integration, and if what you want to get out of it is a function handle that you can put in just particular k and get out integration values, then:
syms k
QOUT = matlabFunction( simplify( int( exp(i*k*r)*(C1*cos(P*r)+C2*sin(P*r)+C3*cosh(P*r)+C4*sinh(P*r)), r, 0, 1) ) );
If what you want is an anonymous function that you can pass in values for everything except k and get back anonymous function that can then be evaluated given just specific k, then
QV = matlabfunction( simplify( int( 'exp(I*k*r)*(C1*cos(P*r)+C2*sin(P*r)+C3*cosh(P*r)+C4*sinh(P*r))', r, 0, 1) ), 'vars', {'k', 'C1', 'C2', 'C3', 'C4', 'P'} );
This will create a function handle that can be passed around.
Then once you have specific numeric values...
QOUT = @(k) QV(k, C1, C2, C3, C4, P)
Note that this will already be integrated.

Iniciar sesión para comentar.


Mike Hosea
Mike Hosea el 19 de Dic. de 2013
Editada: Mike Hosea el 19 de Dic. de 2013
Here's an example. While it's literally true that you can't do numerical integration with free variables, sometimes it misses the point. Eventually you will have values for all free variables, so the trick is to postpone the numerics until that time. Instead of computing the integral, think of building a function of the free variables. Here is a straightforward example of how to handle a problem like the one you describe.
% Define the function with parameters as inputs.
FUN = @(r,k,P,C1,C2,C3,C4)exp(1i*k*r).*(C1*cos(P*r) + C2*sin(P*r) + C3*cosh(P*r) + C4*sinh(P*r));
n = 10; % or whatever
QOUT = zeros(1,n); % Preallocating storage for output.
k = rand(1,n); % or whatever;
P = rand(1,n); % or whatever
C1 = rand(1,n); % or whatever
C2 = rand(1,n); % or whatever
C3 = rand(1,n); % or whatever
C4 = rand(1,n); % or whatever
for j = 1:100
% Define the integrand as a function of only one variable,
% fixing the value of each parameter.
f = @(r)FUN(r,k(j),P(j),C1(j),C2(j),C3(j),C4(j));
QOUT(j) = integral(f,0,1);
end
This doesn't define that function I was talking about, but the above is halfway there and may be all the way there per what you need. However, to go the whole way and define that function of the free variables, we start with the same comprehensive FUN function and then define a function involving INTEGRAL:
FUN = @(r,k,P,C1,C2,C3,C4)exp(1i*k*r).*(C1*cos(P*r) + C2*sin(P*r) + C3*cosh(P*r) + C4*sinh(P*r));
Q = @(k,P,C1,C2,C3,C4)integral(@(r)FUN(r,k,P,C1,C2,C3,C4),0,1)
Now you can evaluate Q for whatever scalar values of the parameters you like:
>> Q(1,2,3,4,5,6)
ans =
16.804616705794533 +12.211731444641790i
Next you might ask me to make this function work for vectors of parameter values. Well, you just need one more line of code for that.
QV = @(k,P,C1,C2,C3,C4)arrayfun(Q,k,P,C1,C2,C3,C4)
With QV defined you can do something like (and here I also illustrate what to do if one of the inputs is a scalar instead of an array like the others),
% Define some arrays for illustration purposes
n = 3;
k = rand(1,n); % or whatever;
P = pi; % or whatever
C1 = rand(1,n); % or whatever
C2 = rand(1,n); % or whatever
C3 = rand(1,n); % or whatever
C4 = rand(1,n); % or whatever
% Now evaluate QV over these arrays.
QOUT = QV(k,P*ones(size(k)),C1,C2,C3,C4)
  5 comentarios
adnan taha
adnan taha el 8 de Dic. de 2022
if i have phi or theta in my equation what happened in this situation
Torsten
Torsten el 8 de Dic. de 2022
Open a new question.
Taken out of context, nobody knows what you are asking here.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by