Double Integral using Integral2 Error

I am trying to write code for the attached equation in matlab but I am getting errors.
My code is as under;
phi=0.0:0.01:90;
P=sqrt((pi^2*Ef*df^3*rounot*S*(1+n))/(4)+(pi^2*Ef*df^3*Gd)/(2));
p_phi=sin(phi);
p_z=2/Lf;
m1=P*exp(f*phi); %multiplier,inside the integral part of eq#8.
m2=(4*Vf)/(pi*df^2); %multiplier, outside the integral part of eq#8.
myfun_eq8=@(z,phi) m1.*p_phi.*p_z;
I= integral2(myfun_eq8,0,1,0,pi/2);
sigma_b=m2*I;
I am getting the following error, can someone help me correct it.
Error using integral2Calc>integral2t/tensor (line 241) Integrand output size does not match the input size.
Error in integral2Calc>integral2t (line 55) [Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9) [q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106) Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in myeqsetlin (line 160) I= integral2(myfun_eq8,0,1,0,pi/2);

6 件のコメント

Andrew Newell
Andrew Newell 2017 年 4 月 17 日
I can't reproduce your problem with the code you provided. First, rounot was undefined, so I tried setting it to 0.01. Then I got the error
Error using *
Inner matrix dimensions must agree.
for the line
m1=P*exp(f*phi);
jack carter
jack carter 2017 年 4 月 18 日
Sorry I forgot to mention, the value of rounot is 3.0. I have run my code again but I am getting the same set of errors which I have mentioned above.
Andrew Newell
Andrew Newell 2017 年 4 月 18 日
There must be something different from what you are showing above because S (and therefore P) has length 3001 while while phi has length 9001, so when you get to the line I mentioned above there is an error.
Andrew Newell
Andrew Newell 2017 年 4 月 18 日
Actually, S is not mentioned at all in the statement of the problem, except at the bottom where its values are provided.
Andrew Newell
Andrew Newell 2017 年 4 月 18 日
I notice that p(z) is a constant, so integrating p(z)dz gives simply cos(phi). Thus, you really have a one-dimensional integral to solve numerically.
jack carter
jack carter 2017 年 4 月 18 日
Thanks Andrew for your reply. I was writing a series of equations in a function file to run a simulation after completion. That is why there seem to had variation in yours and mine results. I wrote delta as S in my script.
I re-ran the above mentioned script separately and I have received the same error as you mentioned in your first reply for m1. If I define as a range of numeric values (S=0:0.01:30), the function still does not work. But if I select S=1; then I get the same set of integral errors.
Your simplification of the equation i.e. p(z) as a constant is helpful

サインインしてコメントする。

 採用された回答

Andrew Newell
Andrew Newell 2017 年 4 月 18 日

1 投票

Based on the above discussion, the critical part of code you need is as follows:
myfun_eq8=@(phi) exp(f*phi).*cos(phi).*sin(phi);
I= integral(myfun_eq8,0,pi/2);
All the other terms can stay outside of the integral.

1 件のコメント

jack carter
jack carter 2017 年 4 月 19 日
Thank you Andrew for your help

サインインしてコメントする。

その他の回答 (2 件)

David Goodmanson
David Goodmanson 2017 年 4 月 17 日
編集済み: David Goodmanson 2017 年 4 月 18 日

1 投票

Hi jack, I believe this is occurring because your definition of myfun_eqn8 uses the two variables z,phi, but the integrand m1.*p_phi.*p_z is not explicitly a function of either of those variables. Matlab does not know how to create the integrand for arbitrary z,phi. You need to define m1, p_phi,p_z explicitly in myfun_eqn8.
Once this gets working the first and third lines of code will probably become superfluous anyway, but right now you are taking the sine of angles in degrees. You can convert to radians or use the sind function there.

5 件のコメント

jack carter
jack carter 2017 年 4 月 18 日
Thank you for your answer. Could you please write the code of defining m1,p_phi,p_z explicitly? I could not follow you from here.
David Goodmanson
David Goodmanson 2017 年 4 月 18 日
Hi jack, Before going forward, is it true that p(z) is simply the constant 2/Lf? If so, then as Andrew noted above you can do the z integration at once and get cos(phi). P(delta) does not depend on phi so you can take that out of the integral and arrive at
(function of a bunch of constants and delta) * Integral sin(phi)*cos(phi)*exp(f*phi)dphi.
That seems too simple. Is it true than nothing in the integrand depends on z?
jack carter
jack carter 2017 年 4 月 18 日
Lf is a constant value, in our case Lf=6. Since p(z) = 2/Lf therefore it should be a numerical value only. But for this numeric value there is a certain range i.e. -2/Lf < z < 2/Lf. So, p(z) is a range of numeric value from (-2/Lf) to (2/Lf) and there is no other function included in the definition of p(z).
David Goodmanson
David Goodmanson 2017 年 4 月 18 日
編集済み: David Goodmanson 2017 年 4 月 18 日
ok then it does appear to reduce to a one-dimensional integral in the variable phi, and the answer for that only depends on the constant f.
jack carter
jack carter 2017 年 4 月 19 日
Thank you David for your help

サインインしてコメントする。

Shahid Hasnain
Shahid Hasnain 2018 年 11 月 15 日

0 投票

I have following expression with x=[1 2 3 4 5], y=[1 2 3 4 5],
L = 100; % Length of the box
p=1;
q=1;
Ui = @(x,y) sin(pi*x).*sin(pi*y); % An initial distribution for u, i.e. the u(x,y,0)
Vi = @(x,y) sin(pi*x).*sin(pi*y); % An initial distribution for v, i.e. the v(x,y,0)
Wi = Ui(x,y) + Vi(x,y); % Initial condition for w, i.e. w(x,y,0)
% Computation of the b-coefficients
if p < Ntrunc
if q < Ntrunc
my_integrand = @(x,y) 4*Wi.*sin(p.*x).*cos(q.*y)/L^2;
bd(p,q) = integral2(my_integrand(x,y),0,L,0,L);
q = q+1;
end
p = p+1;
end
I got following error,
Error using integral2Calc>integral2t/tensor (line 231)
Input function must return 'double' or 'single' values. Found 'function_handle'.
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Correction will be highly appreciated.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 4 月 17 日

回答済み:

2018 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by