Error: Limits of integration must be double or single scalars. While solving equations numerically with symbolic integral limits

33 views (last 30 days)
Problem Description:
The equation system to be solved {f1=0, f2=0, f3=0} contains variables pl and pr. The expressions for pl and pr involve integrals with variables as their limits (indicated by bold parts in the code). After processing through matlabFunction to create anonymous functions, "int" is replaced with "integral". However, "integral" does not support variables as limits, resulting in an error:
Error using integral
Limits of integration must be double or single scalars.
Potentially useful reference:
This is a similar problem I found, but I can't understand it...
Problem Code:
++++++++++++++++++++++++++
alph=pi*15/180;
L=0.116;
T_ice=18;
addweight=0.212;
T0=15;
dL=0.02;
tilt=0;
heightofweight=12.5;
forcefromcable=0;
mu_L=0.001;
rho_S=920*0.95;
rho_L=1000;
Cp_s=2049.41;
h_m=334000+Cp_s*T_ice;
K_L=0.57;
P_e=102770;
g1=addweight*9.8;
g2=0.38*9.8;
heightofmasscenter=0.07;
heightofcable=0.13;
syms x1 y1 x Uratio
d = pi*L*sin(alph)/2;
M = g1*(dL+heightofweight*tan(tilt))*cos(tilt)+g2*heightofmasscenter*sin(tilt)-forcefromcable*heightofcable*sin(tilt);
G = g1+g2;
beta = atan(x1/y1);
xL = sqrt(x1^2+y1^2)*cos(pi-alph-beta);
DL = sqrt(x1^2+y1^2)*sin(pi-alph-beta);
xR = sqrt(x1^2+y1^2)*cos(beta-alph);
DR = sqrt(x1^2+y1^2)*sin(beta-alph);
LL = DL^2+(xL-x)^2;
RR = DR^2+(xR-x)^2;
C = Uratio*(int(x*RR^2,x,0,L)-int(x*LL^2,x,0,-L))/(int(LL^1.5,x,0,-L)-int(RR^1.5,x,0,L));
P0 = 12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*int(LL^2*x,x,0,-L)+C*int(LL^1.5,x,0,-L))/(rho_L*T0^3*K_L^3)+P_e;
pl = -12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*int(LL^2*x,0,x)+C*int(LL^1.5, 0,x))/(rho_L*T0^3*K_L^3)+P0;
pr = -12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*int(RR^2*x,0,x)+C*int(RR^1.5, 0,x))/(rho_L*T0^3*K_L^3)+P0;
% Perform definite integrals on pl and pr to obtain the target equation system
f1 = int(pl*x,x,-L,0)+int(pr*x,x,0,L)+M/d;
f2 = int(pl,x,-L,0)*sin(alph+tilt)+int(pr,x,0,L)*sin(alph-tilt)-G/d-2*L*P_e*sin(alph)*cos(tilt);
f3 = int(pl,x,-L,0)*cos(alph+tilt)-int(pr,x,0,L)*cos(alph-tilt)+2*L*P_e*sin(alph)*sin(tilt);
% Proceed with solving
% Initial guess for variables
initial_guess = [0.5, 0.1, 2];
% Define solving function
equations = matlabFunction(f1,f2,f3, 'Vars', {x1, y1, Uratio});
% Use fsolve to solve the equation system
result = fsolve(@(vars) equations(vars(1), vars(2), vars(3)), initial_guess);
Error using integral
Limits of integration must be double or single scalars.

Error in symengine>@(x)-x.*(Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi./1.2e+1-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi./1.2e+1-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*5.715599983683794e+20-1.0)

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 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);

Error in symengine>@(x1,y1,Uratio)deal(integral(@(x)-x.*(Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi./1.2e+1-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi./1.2e+1-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*5.715599983683794e+20-1.0),0.0,2.9e+1./2.5e+2)+integral(@(x)-x.*(Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*5.715599983683794e+20-1.0),-2.9e+1./2.5e+2,0.0)+8.810850563679611e-1,integral(@(x)Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi./1.2e+1-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi./1.2e+1-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*(-5.715599983683794e+20)+1.0,0.0,2.9e+1./2.5e+2).*2.588190451025207e-1+integral(@(x)Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*(-5.715599983683794e+20)+1.0,-2.9e+1./2.5e+2,0.0).*2.588190451025207e-1-6.293948740487748e+3,integral(@(x)Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi./1.2e+1-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi./1.2e+1-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi./1.2e+1-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*(-5.715599983683794e+20)+1.0,0.0,2.9e+1./2.5e+2).*(-9.659258262890683e-1)+integral(@(x)Uratio.^3.*(Uratio.*(x.^4.*(-cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2./4.0+y1.^2./4.0)+cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2.*(3.0./4.0)+y1.^2.*(3.0./4.0))+x1.^2+y1.^2)+(x.^2.*(x1.^2+y1.^2).^2)./2.0+x.^6./6.0-x.^3.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*(x1.^2+y1.^2).^(3.0./2.0).*(4.0./3.0)-x.^5.*cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2).*(4.0./5.0))+integral(@(x)(sin(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).^2.*(x1.^2+y1.^2)+(x-cos(pi.*(1.1e+1./1.2e+1)-atan(x1./y1)).*sqrt(x1.^2+y1.^2)).^2).^(3.0./2.0),0.0,x)).*(-5.715599983683794e+20)+1.0,-2.9e+1./2.5e+2,0.0).*9.659258262890683e-1)

Error in solution>@(vars)equations(vars(1),vars(2),vars(3)) (line 52)
result = fsolve(@(vars) equations(vars(1), vars(2), vars(3)), initial_guess);

Error in fsolve (line 267)
fuser = feval(funfcn{3},x,varargin{:});

Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
++++++++++++++++++++++++++
  15 Comments
Yuting
Yuting on 7 Aug 2023
@Walter Roberson Thank you for your help!! I added every variables to every anonymous function as you suggested, but the error "Not enough input arguments." still exists.
beta = @(x1,y1) atan(x1/y1);
xL = @(x1,y1) sqrt(x1^2+y1^2)*cos(pi-alph-beta);
DL = @(x1,y1) sqrt(x1^2+y1^2)*sin(pi-alph-beta);
xR = @(x1,y1) sqrt(x1^2+y1^2)*cos(beta-alph);
DR = @(x1,y1) sqrt(x1^2+y1^2)*sin(beta-alph);
% 积分常数项
LL = @(x,x1,y1) DL^2+(xL-x)^2;
RR = @(x,x1,y1) DR^2+(xR-x)^2;
C = @(Uratio,x,x1,y1) Uratio*(integral(@(x)x*RR(x,x1,y1)^2,0,L)-integral(@(x)x*LL(x,x1,y1)^2,0,-L))/(integral(LL(x,x1,y1)^1.5,0,-L)-integral(RR(x,x1,y1)^1.5,0,L));
P0 = @(Uratio,x,x1,y1) 12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*integral(@(x)LL(x,x1,y1)^2*x,0,-L)+C(Uratio,x,x1,y1)*integral(LL(x,x1,y1)^1.5,0,-L))/(rho_L*T0^3*K_L^3)+P_e;
pl = @(Uratio,x,x1,y1) -12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*LL(x,x1,y1)^2*x+C(Uratio,x,x1,y1)*LL(x,x1,y1)^1.5)/(rho_L*T0^3*K_L^3)+P0(Uratio,x,x1,y1);
pr = @(Uratio,x,x1,y1) -12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*RR(x,x1,y1)^2*x+C(Uratio,x,x1,y1)*RR(x,x1,y1)^1.5)/(rho_L*T0^3*K_L^3)+P0(Uratio,x,x1,y1);
% momentum balance
f1 = @(x1,y1,Uratio,x) integral2(@(xi,x)pl(Uratio,xi,x1,y1)*x,0,x,-L,0)+integral2(@(xi,x)pr(Uratio,xi,x1,y1)*x,0,x,0,L)+M/d;
% Force balance分解为竖+横,左+右
f2 = @(x1,y1,Uratio,x) integral2(@(xi,x)pl(Uratio,xi,x1,y1),0,x,-L,0)*sin(alph+tilt)+integral2(@(xi,x)pr(Uratio,xi,x1,y1),0,x,0,L)*sin(alph-tilt)-G/d-2*L*P_e*sin(alph)*cos(tilt);
f3 = @(x1,y1,Uratio,x) integral2(@(xi,x)pl(Uratio,xi,x1,y1),0,x,-L,0)*cos(alph+tilt)-integral2(@(xi,x)pr(Uratio,xi,x1,y1),0,x,0,L)*cos(alph-tilt)+2*L*P_e*sin(alph)*sin(tilt);
disp('balance equations done')
fun = {@(vars) f1(vars(1), vars(2), vars(3)); @(vars) f2(vars(1), vars(2), vars(3)); @(vars) f3(vars(1), vars(2), vars(3))};

Sign in to comment.

Accepted Answer

Torsten
Torsten on 7 Aug 2023
I guess this is what you mean.
initial_guess = [0.5, 0.1, 2];
% Use fsolve to solve the equation system
result = fsolve(@(vars) fun(vars(1), vars(2), vars(3)), initial_guess, optimset('MaxFunEvals',10000,'MaxIter',10000));
function res = fun(x1,y1,Uratio)
alph=pi*15/180;
L=0.116;
T_ice=18;
addweight=0.212;
T0=15;
dL=0.02;
tilt=0;
heightofweight=12.5;
forcefromcable=0;
mu_L=0.001;
rho_S=920*0.95;
rho_L=1000;
Cp_s=2049.41;
h_m=334000+Cp_s*T_ice;
K_L=0.57;
P_e=102770;
g1=addweight*9.8;
g2=0.38*9.8;
heightofmasscenter=0.07;
heightofcable=0.13;
d = pi*L*sin(alph)/2;
M = g1*(dL+heightofweight*tan(tilt))*cos(tilt)+g2*heightofmasscenter*sin(tilt)-forcefromcable*heightofcable*sin(tilt);
G = g1+g2;
beta = atan(x1/y1);
xL = sqrt(x1^2+y1^2)*cos(pi-alph-beta);
DL = sqrt(x1^2+y1^2)*sin(pi-alph-beta);
xR = sqrt(x1^2+y1^2)*cos(beta-alph);
DR = sqrt(x1^2+y1^2)*sin(beta-alph);
LL = @(x)DL^2+(xL-x).^2;
RR = @(x)DR^2+(xR-x).^2;
C = Uratio*(integral(@(x)x.*RR(x).^2,0,L)-integral(@(x)x.*LL(x).^2,0,-L))/(integral(@(x)LL(x).^1.5,0,-L)-integral(@(x)RR(x).^1.5,0,L));
P0 = 12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*integral(@(x)LL(x).^2.*x,0,-L)+C*integral(@(x)LL(x).^1.5,0,-L))/(rho_L*T0^3*K_L^3)+P_e;
pl = @(y)(-12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*integral(@(x)LL(x).^2.*x,0,y)+C*integral(@(x)LL(x).^1.5, 0,y))/(rho_L*T0^3*K_L^3)+P0);
pr = @(y)(-12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*integral(@(x)RR(x).^2.*x,0,y)+C*integral(@(x)RR(x).^1.5, 0,y))/(rho_L*T0^3*K_L^3)+P0);
% Perform definite integrals on pl and pr to obtain the target equation system
f1 = integral(@(y)pl(y).*y,-L,0,'ArrayValued',1)+integral(@(y)pr(y).*y,0,L,'ArrayValued',1)+M/d;
f2 = integral(@(y)pl(y),-L,0,'ArrayValued',1)*sin(alph+tilt)+integral(@(y)pr(y),0,L,'ArrayValued',1)*sin(alph-tilt)-G/d-2*L*P_e*sin(alph)*cos(tilt);
f3 = integral(@(y)pl(y),-L,0,'ArrayValued',1)*cos(alph+tilt)-integral(@(y)pr(y),0,L,'ArrayValued',1)*cos(alph-tilt)+2*L*P_e*sin(alph)*sin(tilt);
res = [f1;f2;f3];
end
  7 Comments
Torsten
Torsten on 11 Aug 2023
Sorry - lsqnonlin must be used in the old formulation:
result = lsqnonlin(@(vars) fun(vars(1), vars(2), vars(3)), initial_guess, [],[], optimset('MaxFunEvals',10000,'MaxIter',10000));
and
res = [f1;f2;f3];

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Aug 2023
integral() can never accept symbolic integration limits.
If you are trying to build up a symbolic algorithm that is later to be used with matlabFunction(), then code the integral() as either int() or vpaintegral()
Note: integral() does not accept expressions such as pl*x . The first parameter to integral() must be a function handle.
  4 Comments
Walter Roberson
Walter Roberson on 6 Aug 2023
Note by the way you have
equations = matlabFunction(f1,f2,f3, 'Vars', {x1, y1, Uratio});
but that should be
equations = matlabFunction([f1,f2,f3], 'Vars', {x1, y1, Uratio});
Also I suggest you consider using
equations = matlabFunction([f1,f2,f3], 'Vars', {[x1, y1, Uratio]});
as that would allow you to
result = fsolve(equations, initial_guess);
Yuting
Yuting on 6 Aug 2023
Thanks a lot for your reply!!
The update of "equations = matlabFunction([f1,f2,f3], 'Vars', {[x1, y1, Uratio]})" works!
But I'm still confused with the integral() stuff...
I wrote something like this:
matlabFunction(expression + integral(@(x) second_expression, 0, x), 'File', 'outer_integral_function', 'ArrayValued', true);
I hope this is correct and I don't know what to do next at all... Sorry for my poor matlab skill...

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!