double integration of function handles with two different variables
3 views (last 30 days)
Show older comments
Gianluca Angelini
on 20 Nov 2023
Commented: Dyuman Joshi
on 21 Nov 2023
hi everybody, here after i post my code, from which you can notice that I need to solve a definite integral by two different variables, after creating several functions and relations amond different expressions.
syms freq_wave wave_angle;
Spectre=@(freq_wave) AA/(freq_wave^5)*exp(-BB/(freq_wave^4))
Spectre_e = @(freq_wave,wave_angle) Spectre(freq_wave)/(1-2*freq_wave*cos(wave_angle)/g)
freq_e =@(freq_wave,wave_angle) freq_wave-(freq_wave^2*vel_nave/g)*cos(wave_angle)
a1=@(wave_angle) 60.3*(4*kyy)^2*CB^1.34*(0.87/CB)^(-(1+Fn)*cos(wave_angle))*log(B/T)^-1*((1-2*cos(wave_angle))/3)
wave_amplitude=@(freq_wave,wave_angle)(2*Spectre_e(freq_wave,wave_angle))^0.5
lambda_wave=@(freq_wave,wave_angle) (wave_amplitude(freq_wave,wave_angle)/0.085)^(4/3)
w_r=@(freq_wave,wave_angle) 2.142*(0.25*Lpp)^1/3*(Lpp/lambda_wave(freq_wave,wave_angle))^(-0.5)*(1-0.111/CB*(log(B/T)-log(2.75)))*(2-cos(wave_angle))/3*Fn^0.143
coeff_3=@(freq_wave,wave_angle) 11 .*(w_r(freq_wave,wave_angle)<1) + (-8.5) .*(w_r(freq_wave,wave_angle)>=1)
coeff_4=@(freq_wave,wave_angle) (566*(Lpp/B)^(-2.66)).*(w_r(freq_wave,wave_angle)<1) + (-566*(Lpp/B)^(-2.66)*6) .*(w_r(freq_wave,wave_angle)>=1)
if Fn<0.12
a2=0.0072+0.1676*Fn;
else
a2=Fn^1.5*exp(-3.5*Fn);
end
sigma_aw=@(freq_wave,wave_angle) w_r(freq_wave,wave_angle)^coeff_3(freq_wave,wave_angle)*exp(coeff_3(freq_wave,wave_angle)*coeff_4(freq_wave,wave_angle)^(-1)*(1-w_r(freq_wave,wave_angle)^coeff_4(freq_wave,wave_angle)))
Raw=@(freq_wave,wave_angle) (4*ro_w*g*B^2*wave_amplitude(freq_wave,wave_angle)^2/Lpp)*a1(wave_angle)*a2*sigma_aw(freq_wave,wave_angle)
integral2(Raw,0,inf,0,2*pi)
here, instead, I show you the error messages
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.
Error in Controllore_nave>@(freq_wave)AA/(freq_wave^5)*exp(-BB/(freq_wave^4))
Error in Controllore_nave>@(freq_wave,wave_angle)Spectre(freq_wave)/(1-2*freq_wave*cos(wave_angle)/g)
Error in Controllore_nave>@(freq_wave,wave_angle)(2*Spectre_e(freq_wave,wave_angle))^0.5
Error in Controllore_nave>@(freq_wave,wave_angle)(4*ro_w*g*B^2*wave_amplitude(freq_wave,wave_angle)^2/Lpp)*a1(wave_angle)*a2*sigma_aw(freq_wave,wave_angle)
Error in integral2Calc>@(y)fun(xi*ones(size(y)),y) (line 18)
@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions), ...
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 integral2Calc>@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions) (line 17)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
Error in integral2Calc>@(x)arrayfun(@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions),x,ymin(x),ymax(x)) (line 17)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
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 83)
[q,errbnd] = vadapt(@AToInfInvTransform,interval);
Error in integral2Calc>integral2i (line 20)
[q,errbnd] = integralCalc(innerintegral,xmin,xmax,opstruct.integralOptions);
Error in integral2Calc (line 7)
[q,errbnd] = integral2i(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Thanks in advance for your precious support
0 Comments
Accepted Answer
Dyuman Joshi
on 20 Nov 2023
Moved: Dyuman Joshi
on 20 Nov 2023
You need to use element-wise operations to define the integrand -
From the documentation of integral2, the definition of the 1st input argument - "Integrand, specified as a function handle, defines the function to be integrated over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x). The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
Note the underlined part. Refer to this for more info - Array vs Matrix Operations
It is also a generalized practice to define anonymous functions using element-wise operations.
Additionally, the syms line is redundant. You can remove it.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!