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

109 views (last 30 days)
Mohammad Gharaibeh on 19 Dec 2013
Commented: Walter Roberson on 12 Jun 2017
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,

David Sanchez on 19 Dec 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)))
Mohammad Gharaibeh on 19 Dec 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?

Walter Roberson on 19 Dec 2013
Edited: Walter Roberson on 12 Jun 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 CommentsShowHide 1 older comment
Walter Roberson on 20 Dec 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.

Mike Hosea on 19 Dec 2013
Edited: Mike Hosea on 19 Dec 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)
Walter Roberson on 12 Jun 2017
Pro B: I replied in the Question you posted on this topic.