Star
Star

Reputation: 2299

Numerical integration in Matlab: matrix dimensions must agree?

I am using Matlab to do the following numerical integration:

LB=0.3;
p_old=0.26;
C_TT_old=0.03;
r=0.63;
m=5.02;

integrand=@(d)(((r*m).^(r+1)*(r+1))/((d+r*m).^(r+2)))*(m.^2*C_TT_old*(1+(2*d*(1+r))/(m))-p_old*d+r*m*(log(d/(r*m)+1))*(p_old/r+p_old-2*C_TT_old*m*(1+r)))/((d+m)*(d+m-1)/2); 
C_old=integral(integrand,LB,Inf);

However, Matlab gives me the following error

Error using  / 
Matrix dimensions must agree.

Error in
@(d)(((r*m).^(r+1)*(r+1))/((d+r*m).^(r+2)))*(m.^2*C_TT_old*(1+(2*d*(1+r))/(m))-p_old*d+r*m*(log(d/(r*m)+1))*(p_old/r+p_old-2*C_TT_old*m*(1+r)))/((d+m)*(d+m-1)/2)


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 integral (line 88)
Q = integralCalc(fun,a,b,opstruct);

Could you help me to fix the code? I do not understand the source of the error as everything is scalar in my code.

Upvotes: 0

Views: 85

Answers (1)

Mendi Barel
Mendi Barel

Reputation: 3677

You must write the integrand in a way that it will deal with argument 'd' as vector. matlab will calculate the inegrand with vector, not with scalar. So the soultion is to use '.' notation in './' and '.*' and '.^' when you use 'd':

integrand=@(d)(((r*m)^(r+1)*(r+1))./((d+r*m).^(r+2))).*(m^2*C_TT_old*(1+(2*d.*(1+r))/m)-p_old*d+r*m*(log(d/(r*m)+1))*(p_old/r+p_old-2*C_TT_old*m*(1+r)))./((d+m).*(d+m-1)/2);

Upvotes: 1

Related Questions