How to solve the following four integration in MATLAB

4 次查看(过去 30 天)
Hello all, I am trying to solve the following expression involving four integrations in MATLAB but note getting it correctly.
This is how I had tried to code:
integrand = @(y, h, z1, z2) (1 / gamma(m_0)) * ...
gammainc( ((u_1 ./ ...
(a_1 * zeta_1 * y .* g_1_abs_sq .* h .* P_h1_2 - ...
u_1 * a_2 * zeta_1 * y .* g_1_abs_sq .* h .*P_h1_2 - ...
u_1 * a_1 * zeta_1 * g_1_abs_sq .* h .* z1 - ...
u_1 * a_1 * zeta_1 *P_h1_2 * z2 .* g_1_abs_sq .* h - ...
u_1 * a_2 * zeta_1 * g_1_abs_sq .* h .* z1 - ...
u_1 * a_2 * zeta_1 * P_h1_2 * z2 .* g_1_abs_sq .* h)) / ...
(Omega_0 / m_0)),m_0) .* ...
(1/gamma(m_0))*((m_0/Omega_0)^m_0)*y^(m_0-1)*exp(-m_0*y/Omega_0).* 1/(2*pi*A_1).*lambda_1*exp(-lambda_1*z1).*lambda_2*exp(-lambda_2*z2);
outer_integral = @(z2) arrayfun(@(z2_val) integral(@(y,h,z1) integrand(y,h,z1,z2_val ), 0, y_max(z2_val)), 0:1000);
% Perform the integration
op = integral(outer_integral, 0, 1000);
I am not getting why I am getting such errors:
Not enough input arguments.
Error in Analytical_2>@(z1,z2)(P_h1_2*(a_1-u_1*a_2))./(u_1*a_1*z1+u_1*a_1*P_h1_2*z2+u_1*a_2*z1+u_1*a_2*P_h1_2*z2) (line 96)
(u_1 * a_1 * z1 + u_1 * a_1 * P_h1_2 * z2 + ...
Error in Analytical_2>@(z2_val)integral(@(y,h,z1)integrand(y,h,z1,z2_val),0,y_max(z2_val)) (line 132)
outer_integral = @(z2) arrayfun(@(z2_val) integral(@(y,h,z1) integrand(y,h,z1,z2_val ), 0, y_max(z2_val)), 0:1000);
Error in Analytical_2>@(z2)arrayfun(@(z2_val)integral(@(y,h,z1)integrand(y,h,z1,z2_val),0,y_max(z2_val)),0:1000) (line 132)
outer_integral = @(z2) arrayfun(@(z2_val) integral(@(y,h,z1) integrand(y,h,z1,z2_val ), 0, y_max(z2_val)), 0:1000);
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
Error in Analytical_2 (line 136)
op = integral(outer_integral, 0, 1000);
Please note that I had intentionally not given other part of code which is used to produce the values of elements that are used in this integrand. Any help in this regard will be highly appreciated.
  4 个评论
chaaru datta
chaaru datta 2025-1-4
Thank u sir for ur response, but I am not getting why MATLAB cant solve such 4 integrals.
Torsten
Torsten 2025-1-4
编辑:Torsten 2025-1-4
In MATLAB, there exist functions that can directly be applied to 1d, 2d and 3d integrals (namely integral, integral2 and integral3). If you have a 4d integral, the existing MATLAB routines can be combined (e.g. 3d with 1d, 2d with 2d, 2d with two times 1d and four times 1d) to solve the higher-dimensional integral. And that's exactly what "integralN" does.
If you want to use your personal method to solve your integral (e.g. by taking "integral" four times), you can do so. But as you see from your code, you don't succeed. I recommended "integralN" because it simplifies the set-up of such calculations.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2025-1-4
integral expects a function handle to a function that accepts a single input parameter. It normally passes a vector of values to the function handle, and expects a vector of results of the same size. The size of the vector that it passes in is variable.
You are first passing integral() a function handle to a function that accepts a single parameter, which is good. Your code does
outer_integral = @(z2) arrayfun(@(z2_val) integral(@(y,h,z1) integrand(y,h,z1,z2_val ), 0, y_max(z2_val)), 0:1000);
The arrayfun() part of it is good. But inside the arrayfun() you are passing integral() a function handle of a function that expects up to three input parameters. integral() cannot handle functions that expect three input parameters. integral3 on the other hand is able to accepts function handles that expect three input parameters. Thus, proper code would more likely be
outer_integral = @(z2) arrayfun(@(z2_val) integral3(@(y,h,z1) integrand(y,h,z1,z2_val ), 0, y_max(z2_val)), 0:1000);
  17 个评论
Torsten
Torsten 2025-1-7
编辑:Torsten 2025-1-7
My query is based on the style of expression (A), how to write MATLAB code for expression (B) ?
Why are you getting confused ?
I gave you the answer previously (some * are replaced by .* and / are replaced by ./ ):
integrand = @(y, h, z1, z2) (1 ./ gamma(m_0)) .* ...
gammainc( ((u_1 ./ ...
(a_1 .* zeta_1 .* y .* g_1_abs_sq .* h .* P_h1_2 - ...
u_1 .* a_2 .* zeta_1 .* y .* g_1_abs_sq .* h .* P_h1_2 - ...
u_1 .* a_1 .* zeta_1 .* g_1_abs_sq .* h .* z1 - ...
u_1 .* a_1 .* zeta_1 .* P_h1_2 .* z2 .* g_1_abs_sq .* h - ...
u_1 .* a_2 .* zeta_1 .* g_1_abs_sq .* h .* z1 - ...
u_1 .* a_2 .* zeta_1 .* P_h1_2 .* z2 .* g_1_abs_sq .* h)) ./ ...
(Omega_0 ./ m_0)),m_0) .* ...
(1./gamma(m_0)).*((m_0./Omega_0).^m_0).*y.^(m_0-1).*exp(-m_0.*y./Omega_0).* 1./(2.*pi.*A_1).*lambda_1.*exp(-lambda_1.*z1).*lambda_2.*exp(-lambda_2.*z2);
y_max = @(z1,z2) P_h1_2 .* (a_1 - u_1 .* a_2) ./ ...
(u_1 .* a_1 .* z1 + u_1 .* a_1 .* P_h1_2 .* z2 + ...
u_1 .* a_2 .* z1 + u_1 .* a_2 .* P_h1_2 .* z2);
op = integral(@(h)integral3(@(z1,z2,y)integrand(y,h,z1,z2),0, inf, 0, inf, 0, y_max),0, Inf, 'ArrayValued',1)
% or
%outer_integral = @(h) arrayfun(@(h_val) integral3(@(z1,z2,y) integrand(y,h_val,z1,z2 ), 0, inf, 0, inf, 0, y_max), h);
%op = integral(outer_integral, 0, inf);
I assumed z1 = Z1 and z2 = Z2 in your mathematical description.
But as written, it will not work because h = 0 gives a division by zero because the denominator of the argument to the gammainc function becomes 0.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by