MATLAB: Am i getting the error “Matrix dimensions must agree” here

MATLABmatlab functionmatrix manipulation

m =1;
Ts_Psi = 1;
Psi_Degrees = 60;
Psi_Radians = deg2rad(Psi_Degrees);
Phi_Degrees = 60;
Phi_Radians = deg2rad(Phi_Degrees);
n = 1.5;
G_Psi = (n^2)/(sin(Psi_Radians)^2);
k_Dash = ((m+1)*0.01/(2*pi)) * Ts_Psi * G_Psi;
k = ((m+1)*0.01/(2*pi)) * Ts_Psi * G_Psi * (cos(Phi_Radians)^m) * cos(Psi_Radians);
Pt = 4.5;
lambda = 0.01;
h =10;
syms x;
syms w;
D = 1:1:10;
f =@(x,w) (1 - exp((1j*w*k_Dash*Pt.*(x.^2))./((x.^2+h^2).^2)));
cf=@(w)exp(-lambda*(integral(@(x)f(x,w),0,Inf)));
int_temp = @(w,x)((1./w).*imag(cf(w).*exp(-1j.*w.*x)));
fi = @(x)( (1/2) - (1/pi).*integral(@(w)int_temp(w,x),0,Inf));
fi(0.1)

Best Answer

Change this line to
fi = @(x)( (1/2) - (1/pi).*integral(@(w)int_temp(w,x),0,Inf,'ArrayValued',1));
and the error will be resolved. However, due to the integration limit being infinity, the integral starts to fail. To correct it, I recommend you to replace Inf with a large number, e.g., 10000. The output will be fine. If you increase the limit from 10000, you will see that the output will approach a finite value, so using finite limits will indeed give the correct result. For example run the following code with, which have finite limits
m =1;
Ts_Psi = 1;
Psi_Degrees = 60;
Psi_Radians = deg2rad(Psi_Degrees);
Phi_Degrees = 60;
Phi_Radians = deg2rad(Phi_Degrees);
n = 1.5;
G_Psi = (n^2)/(sin(Psi_Radians)^2);
k_Dash = ((m+1)*0.01/(2*pi)) * Ts_Psi * G_Psi;
k = ((m+1)*0.01/(2*pi)) * Ts_Psi * G_Psi * (cos(Phi_Radians)^m) * cos(Psi_Radians);
Pt = 4.5;
lambda = 0.01;
h =10;
D = 1:1:10;
f =@(x,w) (1 - exp((1j*w*k_Dash*Pt.*(x.^2))./((x.^2+h^2).^2)));
cf=@(w)exp(-lambda*(integral(@(x)f(x,w),0,Inf)));
int_temp = @(w,x)((1./w).*imag(cf(w).*exp(-1j.*w.*x)));
fi = @(x)( (1/2) - (1/pi).*integral(@(w)int_temp(w,x),0,Inf,'ArrayValued',1));
fi(0.1)
Result
ans =
0.9998
Related Question