Questions with integral2 , Double, Syms and Dot calucations.

Dear friends, I got 2 questions here. 1. If I have y = a*x^2 + 5. What function can make it into y = a.*x.^2 +5. As you seen, dot was inserted.
2. It's easy, but kinda diffcult to describe, but please have patience with me. Thank you so much. First, let me make a very simple example of my problem. If I want to calucate Y = F(x=1)+ 2.^2, and I know F(x=1) = a+b,(a and b are syms). This means Y = a + b + 4. Problem is here, Matlab give me error if I write down as.
F = function( .... ); <==== output of function is F(X=1), and = *F(x=1)* = a+b
Y = integral2( F + 2.^2, .. , .. ,..)
However, if I just copy the output of F as
Y = integral2( a+b + 2.^2, .. , .. ,..)
Now it works!!!
Ok. Please allow me to talk about my code here. I am trying to find a double interation by using integral2. One part of my equcation(which is Y) is from another int output(which is F). Matlab will give ERROR for code below:
clear all;
a=4;
la1=1/(pi*500^2); la2= la1*5;
p1=25; p2=p1/25;
sgma2=10^(-11);
index=1;
g=2./a;
syms r u1 u2
powe= -2 ;
seta= 10^powe;
xNor = ( (u2./u1).^(a./2) + 1 ).^(2./a);
x = (xNor).^(0.5) * seta^(-1/a);
fun1 = r./(1+ r.^a );
out1 = int(fun1, x, Inf) ; %== This is my F in my example
q=pi.*(la1.*p1.^(2./a)+la2.*p2.^(2./a));
yi = @(u2,u1) exp(-u2.*(1+2.*...
( out1 )./... %=== out1 is the problem here.
( (( (u2./u1).^(a./2) + 1 ).^(2./a)).*seta.^(-2./a)))).*...
exp(-sgma2.*q.^(-a./2).* seta.*u2.^(a./2)./...
((( (u2./u1).^(a./2) + 1 ).^(2./a)).^(a./2)) );
maxF =@(u2) u2;
out2 = integral2(yi,0,Inf,0 ,maxF) % == this is Y in my previous example.
However, since I know the out1 = pi/4 - atan(10*(u2^2/u1^2 + 1)^(1/2))/2 (no dot,1/2, not 1./2). Instead of writing down out1, I will just type the equaction and add dot in the
yi = @(u2,u1) exp(-u2.*(1+2.*...
( pi./4 - atan(10.*(u2.^2./u1.^2 + 1).^(1./2))./2 )./... %===not "out1"
( (( (u2./u1).^(a./2) + 1 ).^(2./a)).*seta.^(-2./a)))).*...
exp(-sgma2.*q.^(-a./2).* seta.*u2.^(a./2)./...
((( (u2./u1).^(a./2) + 1 ).^(2./a)).^(a./2)) );
Now the code is working!!!! The final output is = 0.9957. Dear friends, I already spend a long time on this, but I still can not find out the problem. Could you please take a deeper look for me. Please copy the code to you matlab and test. Thank you so much.
Below is the error given by matlab, if I just use "out1" in yi = @(u2,u1) ......
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'sym'.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in
integral2Calc>@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions)
(line 18)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
Error in
integral2Calc>@(x)arrayfun(@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions),x,ymin(x),ymax(x))
(line 18)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 84)
[q,errbnd] = vadapt(@AToInfInvTransform,interval);
Error in integral2Calc>integral2i (line 21)
[q,errbnd] = integralCalc(innerintegral,xmin,xmax,opstruct.integralOptions);
Error in integral2Calc (line 8)
[q,errbnd] = integral2i(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 107)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in ref7_equ11n2 (line 129)
out2 = integral2(yi,0,Inf,0 ,maxF)

 Accepted Answer

The problem is that out1 is a symbolic "thing", not a MATLAB "thing". That's what the error message is really telling you. You need to use
out1fcn = matlabFunction(out1);
Then, in your integrand, instead of typing just "out1", type
out1fcn(u1,u2)
Like so:
out1 = int(fun1, x, Inf) ; %== This is my F in my example
out1fcn = matlabFunction(out1); % Convert symbolic object to a MATLAB function.
q=pi.*(la1.*p1.^(2./a)+la2.*p2.^(2./a));
yi = @(u2,u1) exp(-u2.*(1+2.*...
( out1fcn(u1,u2) )./... %=== out1 is the problem here.
( (( (u2./u1).^(a./2) + 1 ).^(2./a)).*seta.^(-2./a)))).*...
exp(-sgma2.*q.^(-a./2).* seta.*u2.^(a./2)./...
((( (u2./u1).^(a./2) + 1 ).^(2./a)).^(a./2)) );
maxF =@(u2) u2;
out2 = integral2(yi,0,Inf,0 ,maxF) % == this is Y in my previous example.

6 Comments

OMG! Dear Mike. Thank you so much! It's working now.
But Could you tell me how did you know this? I mean, what is matlab thing and symbolic thing? If I truly understand it, I wont make the same mistake.
Thanks again!
Everything in MATLAB has a "class". You can query the class using the "class" function:
>> class(1)
ans =
double
>> x = 1
x =
1
>> x = 1;
>> class(x)
ans =
double
>> x = single(1);
>> class(x)
ans =
single
>> x = intmax;
>> class(x)
ans =
int32
>> x = 'Hello, world.'
x =
Hello, world.
>> class(x)
ans =
char
>> syms u1
>> class(u1)
ans =
sym
The error message said (pay close attention to the first error message)
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'sym'.
This means that when the INTEGRAL2 tried to evaluate the function z = f(x,y), class(z) did not return 'double' or 'single', rather 'sym'. But how did that happen? Well, class(out1) was 'sym', and there are "promotion rules" for how classes combine:
>> class( single(1) + double(1) )
ans =
single
>> class( int32(1) + double(1) )
ans =
int32
>> class( u1 + double(1) )
ans =
sym
So that one 'sym' value in the function ended up converting the entire output to 'sym'.
Dear mike. Thank you so much for your help. Please allow me to ask 2 more thing here. "promotion rules" for classes combine is very helpful.
1.So, how about the relationship between symbolic and matlabfunction? I mean, I know the data type of symbolic is symbolic, the date type of x=2 is double. but what is the type of matlabfunction? The reason I want to know this is because
Actually, when I see the error
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'sym'.
I already know that I should give it as 'double' or 'single', not 'sym'. But what I tried is 'double(out1)' NOT 'matlabFunction(out1)'
2 .Another question is that in the 2nd line
yi = @(u2,u1) exp(-u2.*(1+2.*...
( out1fcn(u1,u2) )./...
Code above is working, but if I change 2nd line into
yi = @(u2,u1) exp(-u2.*(1+2.*...
( matlabFunction(out1) )./...
This is not working. Error is still showing me something with function handle. So, Could you please let me know what is difference here? What is different between out1fcn(u1,u2) and matlabFunction(out1)?
Error are
Undefined function 'times' for input arguments of type 'function_handle'.
Error in
@(u2,u1)exp(-u2.*(1+2.*(matlabFunction(out1))./((((u2./u1).^(a./2)+1).^(2./a)).*seta.^(-2./a)))).*exp(-sgma2.*q.^(-a./2).*seta.*u2.^(a./2)./((((u2./u1).^(a./2)+1).^(2./a)).^(a./2)))
Error in integral2Calc>@(y)fun(xi*ones(size(y)),y) (line 19)
@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions), ...
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in
integral2Calc>@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions)
(line 18)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
Error in
integral2Calc>@(x)arrayfun(@(xi,y1i,y2i)integralCalc(@(y)fun(xi*ones(size(y)),y),y1i,y2i,opstruct.integralOptions),x,ymin(x),ymax(x))
(line 18)
innerintegral = @(x)arrayfun(@(xi,y1i,y2i)integralCalc( ...
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 84)
[q,errbnd] = vadapt(@AToInfInvTransform,interval);
Error in integral2Calc>integral2i (line 21)
[q,errbnd] = integralCalc(innerintegral,xmin,xmax,opstruct.integralOptions);
Error in integral2Calc (line 8)
[q,errbnd] = integral2i(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 107)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in ref7_equ11n2 (line 129)
out2 = integral2(yi,0,Inf,0 ,maxF)
Thank you so much. Mike
Well, out1 is a 'sym' type. It is a symbolic expression. matlabFunction is not any kind of data, rather a function that you use to convert a symbolic expression to a MATLAB function. The input of matlabFunction is a 'sym' type, and the output is a 'function_handle' type. If you type
f = @(x)x.^2 + 1
then the variable f holds a 'function_handle' type. Arithmetic is not defined on function handles. You cannot, for example, do this
g = @(x)x.^3
h = f + g
with the intention of creating the function x.^3 + x.^2 + 1, and you certainly cannot expect h to be, say, 13 just because you might have defined x = 2 ahead of time. That last statement will issue an error because class(f) is 'function_handle' and class(g) is 'function_handle', and the operation + is not defined on function handles. The correct way to do this in MATLAB is
h = @(x)f(x) + g(x)
Since class(f) is 'function_handle', f(x) is interpreted to mean "evaluate the function handle f at the value x." The result will be whatever f(x) is. This will turn out to be 'double' in this case if class(x) is 'double'.
When you use the matlabFunction function on your symbolic expression, you get a function handle back. To evaluate that function, you need to supply the arguments.
Thank you very much, Mike:)
I have the same problem with sun. Thank you so much to solve this, Mike;)

Sign in to comment.

More Answers (0)

Asked:

sun
on 27 Dec 2014

Commented:

on 4 May 2018

Community Treasure Hunt

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

Start Hunting!