MATLAB Answers

The following error occurred converting from sym to double: Unable to convert expression into double array.

12 views (last 30 days)
This part is included in a function called myf2(w,k):
function f= myf2(w,k)
i = sqrt(-1);
fm = @(x) (1/sqrt(2*pi))* exp(-x.^2/2);
dfm = @(x) -x.*fm(x);
fun = @(x) dfm(x) / (x-w/k);
w0 = max(imag(w/k),0);
a = -10+w0*i;
b = 10+w0*i;
N = 100;
R = dfm(w/k);
x = linspace(a,b,N+1);
syms wn x
wn = sym(zeros(1, N)); % <------- Something is wrong here
f_int = int(fun, x, a, b);
A = vpa(f_int);
f = k^2 - A - 2*pi*i*R;
return
This is where I call the function:
for ix = 1:Nx
for iy = 1:Ny
z = x(ix) + I*y(iy);
tmp = myf2(z,k);
f(ix,iy) = abs(tmp);
end
end
After this I'm getting an error! Please help me out!

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 18 Jun 2020
In the test I did a moment ago with random inputs, the expression to be integrated over x had a denominator that became zero between the calculated a and b, leading to a singularity and so to an undefined integration result.
Angela Mehta
Angela Mehta on 18 Jun 2020
I see and understand my mistake but then how will I numerically calculate the integral(to avoid the issue of running into division by 0) and still getting an approximately close answer? Initially, I tried to use the following in myf2:
height = (b-a)/N;
s = 1/2* (fun(a)+fun(b));
A = height * s;
Walter Roberson
Walter Roberson on 18 Jun 2020
In some cases you could hope that the singularity is "removable" to permit a Cauchy Principle Value to be calculated. However in cases where the function goes continuously to infinity, the singularity has no hope of being removable unless it is the same sign of infinity on both sides of the singularity. The singularity that I encounter with random data with of the form (expression in x) / (linear expression in x) and that has infinities with different sign as the linear expression becomes 0, so the singularity cannot be removed.
In other words, in the case I encountered, no integration was possible. The "approximately close answer" was undefined.
If you post your revised code and post values for x, y, I, and k so that we can test, then we can have another look.

Sign in to comment.

Accepted Answer

David Hill
David Hill on 17 Jun 2020
Do you have to perform symbolically? Does not look like the integral exists. Why not numerically?
function f= myf2(w,k)
dfm =@(x) -x.*(1/sqrt(2*pi)).* exp(-x.^2/2);
fun =@(x) -x.*(1/sqrt(2*pi)).* exp(-x.^2/2)./ (x-w/k);
w0 = max(imag(w./k),0);
a = -10+w0*1i;
b = 10+w0*1i;
R = dfm(w./k);
A = integral(fun,a,b);
f = k^2 - A - 2*pi*1i*R;
end

  3 Comments

Angela Mehta
Angela Mehta on 17 Jun 2020
Hello, I tried to perform the integral numerically but wasn’t getting the exact solution(with negligible error). So I tried to do it symbolically and am getting the exact solutions but now am running into that error!
David Hill
David Hill on 17 Jun 2020
What error? If your exact answer has 'int' out in front, then it has not really been integrated symbolically.
Angela Mehta
Angela Mehta on 18 Jun 2020
I am actually new to Matlab so I might be misplacing my words here and there so I am sorry! When I run the code, it's gives an error of not being able to convert from sym to double.

Sign in to comment.

More Answers (0)