Speed up MatlabFunction or use alternatives

7 views (last 30 days)
Finn Busch
Finn Busch on 17 Apr 2020
Answered: Steven Lord on 28 Nov 2022
Hello,
I am currently processing a big equation (in fact a 36 - Vector with ~15.000 characters each) and am trying to get a function using MatlabFunction. The goal is to receive a function which can be evaluated as quickly as possible, as it's part of a system solved by an ODE solver. The equation itself only contains polynomials to the degree of 3 and square-roots (^3/2 e.g.) but no sin, cos, etc. . Calling MatlabFunction took longer than 18 Hours, and now I had to change the equation and I really don't want to wait for the full 18 Hours. Is setting optimize to false advisable, or will this make my evaluation time worse? Are there other options beside MatlabFunction? I am not very familiar with this kind of stuff as I never had to deal with calculations taking so long.
Any help is greatly appreciated!
  23 Comments

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 28 Nov 2022
Have you tried telling matlabFunction to skip trying to Optimize the code?
cd(tempdir)
syms a b c d positive
syms x
s = solve(a*x^3+b*x^2+c*x+d == 0, x, MaxDegree=3);
s is a pretty complicated expression, but the final answer can be simplified through the use of temporary sub-expressions. But that simplification can result in longer files (albeit with simpler expressions and shorter lines) and longer time creating the files.
tic
matlabFunction(s, File='cubicSolver1.m', Optimize=true);
toc
Elapsed time is 0.477392 seconds.
dbtype cubicSolver1.m
1 function s = cubicSolver1(a,b,c,d) 2 %cubicSolver1 3 % S = cubicSolver1(A,B,C,D) 4 5 % This function was generated by the Symbolic Math Toolbox version 9.2. 6 % 28-Nov-2022 14:57:43 7 8 t2 = b.^2; 9 t3 = b.^3; 10 t4 = 1.0./a; 11 t7 = sqrt(3.0); 12 t5 = t4.^2; 13 t6 = t4.^3; 14 t8 = (b.*t4)./3.0; 15 t9 = (c.*t4)./3.0; 16 t10 = (d.*t4)./2.0; 17 t11 = -t8; 18 t12 = -t10; 19 t13 = (b.*c.*t5)./6.0; 20 t15 = (t2.*t5)./9.0; 21 t16 = (t3.*t6)./2.7e+1; 22 t14 = -t13; 23 t17 = -t15; 24 t18 = -t16; 25 t19 = t9+t17; 26 t21 = t10+t14+t16; 27 t20 = t19.^3; 28 t22 = t21.^2; 29 t23 = t20+t22; 30 t24 = sqrt(t23); 31 t25 = t12+t13+t18+t24; 32 t26 = t25.^(1.0./3.0); 33 t27 = 1.0./t26; 34 t28 = t26./2.0; 35 t29 = -t28; 36 t30 = t19.*t27; 37 t31 = t30./2.0; 38 t32 = t26+t30; 39 t33 = t7.*t32.*5.0e-1i; 40 s = [t11+t26-t30;t11+t29+t31-t33;t11+t29+t31+t33];
tic
matlabFunction(s, File='cubicSolver2.m', Optimize=false);
toc
Elapsed time is 0.121004 seconds.
dbtype cubicSolver2.m
1 function s = cubicSolver2(a,b,c,d) 2 %cubicSolver2 3 % S = cubicSolver2(A,B,C,D) 4 5 % This function was generated by the Symbolic Math Toolbox version 9.2. 6 % 28-Nov-2022 14:57:43 7 8 et1 = (c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0); 9 et2 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0); 10 et3 = ((c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0))./2.0+sqrt(3.0).*(et1+et2).*5.0e-1i-b./(a.*3.0); 11 et4 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0).*(-1.0./2.0); 12 et5 = (c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0); 13 et6 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0); 14 et7 = ((c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0))./2.0-sqrt(3.0).*(et5+et6).*5.0e-1i-b./(a.*3.0); 15 et8 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0).*(-1.0./2.0); 16 et9 = -(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0)-b./(a.*3.0); 17 et10 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0); 18 s = [et9+et10;et7+et8;et3+et4];

Community Treasure Hunt

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

Start Hunting!