Error in integral2 function
3 views (last 30 days)
Show older comments
a=0.4;
d=1;
syms x y
s=((x+sqrt(0.25-y*y)-0.5)/(sqrt(1-(4*y*y))));
F1=(sqrt((tan(pi*s/2))/(pi*s/2)))*(0.752+2.02*s+0.37*(1-sin(3.14*s/2)).^3)/cos(3.14*s/2);
% F2=(sqrt((tan(3.14*s/2)/(3.14*s/2))*(0.923+0.199*(1-sin(3.14*s/2))^4))/cos(3.14*s/2));
% F12=(1.122-.561*s+0.085*s^2+0.180*s^3)/sqrt(1-s);
fun=@(x,y)(32/pi)*(x+sqrt(0.25-y.^2)-0.5)*(F1).^2;
xmin=0;
xmax=@(y) d^2*(sqrt(0.25-y.^2)-(d/2));
ymin=-(sqrt(0.4))+(0.4);
ymax=(sqrt(0.4))-(0.4);
g=integral2(fun,ymin,ymax,xmin,xmax);
ERRORS
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);
Error in F11 (line 19)
g=integral2(fun,ymin,ymax,xmin,@(y) xmax);
0 Comments
Answers (2)
Walter Roberson
on 26 Sep 2019
I wonder which MATLAB version you are using. I would have expected instead
Error using integral2Calc>integral2t/tensor (line 231)
Input function must return 'double' or 'single' values. Found 'sym'.
integral2() cannot handle symbolic expressions such as are caused by your reference to F1 in fun.
You should be using
fun = matlabFunction((32/pi)*(x+sqrt(0.25-y.^2)-0.5)*(F1).^2, 'vars', [x,y])
Steven Lord
on 27 Sep 2019
Let's look at part of your code.
syms x y
s=((x+sqrt(0.25-y*y)-0.5)/(sqrt(1-(4*y*y))));
Because x and y are sym objects, s is also a sym object. When you add, subtract, multiply, divide, and/or power numeric values and sym objects the result is a sym. sqrt on a sym also returns a sym.
F1=(sqrt((tan(pi*s/2))/(pi*s/2)))*(0.752+2.02*s+0.37*(1-sin(3.14*s/2)).^3)/cos(3.14*s/2);
Because s is a sym object, F1 is also a sym object. The trig functions are also sym in, sym out.
% F2=(sqrt((tan(3.14*s/2)/(3.14*s/2))*(0.923+0.199*(1-sin(3.14*s/2))^4))/cos(3.14*s/2));
% F12=(1.122-.561*s+0.085*s^2+0.180*s^3)/sqrt(1-s);
fun=@(x,y)(32/pi)*(x+sqrt(0.25-y.^2)-0.5)*(F1).^2;
When fun is called, x and y will be assigned numeric values (by integral2.) However, F1 is still a sym object. So fun returns a sym object.
Now you call integral2 with fun as its integrand input.
g=integral2(fun,ymin,ymax,xmin,xmax);
Note that what you showed in the error message is not the same as the code you posted; the error message came from using @(y) xmax as the fifth input, not from your integrand input. When integral2 evaluated your integrand it would have thrown an error because integral2 requires the integrand function to return a double or single array. But as you wrote it validating the limits functions caused a slightly different version of that error first.
If you intended for fun to substitute the numeric values for x and y into the sym F1 to obtain a numeric result, you'll need to call subs inside fun. You may also need to call double on the substituted value.
fun=@(x,y)(32/pi)*(x+sqrt(0.25-y.^2)-0.5)*double(subs(F1)).^2;
But there's still one problem. integral2 calls fun with arrays x and y, so your function needs to be able to handle arrays. Correcting this is easy, a one-character fix to use element-wise multiplication (.*) instead of matrix multiplication (*).
fun=@(x,y)(32/pi)*(x+sqrt(0.25-y.^2)-0.5).*double(subs(F1)).^2;
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!