Error "Limits of integration must be double or single scalars." when solve equations with "fsolve" function
Show older comments
I want to solve the balance equations like following:
% momentum and force balance
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);
initial_guess = [0.5, 0.1, 2];
equations = matlabFunction(f1,f2,f3, 'Vars', {x1, y1, Uratio});
result = fsolve(@(vars) equations(vars(1), vars(2), vars(3)), initial_guess);
Then error happend after running:
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-ata.........
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))+(Uratio.*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).*(cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*4.5265984e-5+y1.^2.*4.5265984e-5)-cos(pi.*(1.1e+1./6.0)-atan(x1./y1).*2.0).*(x1.^2.*4.5265984e-5+y1.^2.*4.5265984e-5)-cos(pi./6.0-atan(x1./y1).*2.0).*(x1.^2.*1.357.........
Error in fsolvetest_3para>@(vars)equations(vars(1),vars(2),vars(3)) (line 74)
result = fsolve(@(vars) equations(vars(1), vars(2), vars(3)), initial_guess);
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Error in fsolvetest_3para (line 74)
result = fsolve(@(vars) equations(vars(1), vars(2), vars(3)), initial_guess);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Answers (1)
Harald
on 1 Aug 2023
0 votes
Hi Yuting,
since you are solving the equations numerically anyway, my recommendation would be to perform all the computations numerically from the start, i.e. write a function that computes [f1, f2, f3] from (x1, y1, Uratio). I would only make an exception from this if the symbolic computations are really simple or can be simplified significantly.
This should not only help resolve the error but it will likely also improve performance.
Best wishes,
Harald
4 Comments
Yuting
on 1 Aug 2023
Harald
on 1 Aug 2023
I would not skip them, but calculate them depending on the currently symbolic variables. You could highlight the lines between between "syms..." and "disp", then try the "Convert to Function" from the context menu to get started with such a function. You may need to switch some symbolic expressions to anonymous function handles and calls of int may need to be switched to integral.
At the moment, the formulas are just getting too long for me to even understand what exactly the problem is. This is the reason for me to suggest the workaround.
Best wishes,
Harald
Yuting
on 2 Aug 2023
Harald
on 2 Aug 2023
I would avoid using the same variable as the integration variable and the limit as it has potential for confusion.
Since the integral will depend on the upper limit, I would create an anonymous function handle for this:
pl = @(x) -12*mu_L*rho_S^4*h_m^3*Uratio^3*(Uratio*integral(@(xi) LL(xi)^2*xi,0,x)+C*integral(@(xi) LL(xi)^1.5, 0,x))/(rho_L*T0^3*K_L^3)+P0;
This assumes that LL has been created as a function handle, e.g.
LL = @(x) DL^2+(xL-x)^2;
I hope this helps.
Best wishes,
Harald
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!