Techniques for making matlabFunction finish executing faster

14 views (last 30 days)
Hi,
I am trying to run the following script where I am finding coefficients of monomials in a degree N polynomial, and trying to save them as a function handle using matlabFunction command. However, this command takes forever to finish, it has been running for a couple days now. Any tips that could make generating this function handle quicker ? like parallel processing, GPUs etc ?
clear all
theta = sym('theta',[4,1],'real');
P = sym('P',[1,5],'real');
Q = sym('Q',[1,5],'real');
R = sym('R',[1,5],'real');
S = sym('S',[1,5],'real');
T = sym('T',[1,5],'real');
U = sym('U',[1,5],'real');
V = sym('V',[1,5],'real');
W = sym('W',[1,5],'real');
theta_tilde = [theta;1];
p = dot(P,theta_tilde)*dot(Q,theta_tilde)*dot(R,theta_tilde)*dot(S,theta_tilde)*dot(T,theta_tilde)*dot(U,theta_tilde)*dot(V,theta_tilde)*dot(W,theta_tilde) ;
[coeff,terms] = coeffs(p,theta);
matlabFunction(coeff, 'File','coeff','Vars', {P,Q,R,S,T,U,V,W})

Answers (1)

Pranav Verma
Pranav Verma on 13 Aug 2020
Hi Jaskaran,
The 'File' option optimizes the code and this optimization may be time consuming for some symbolic expressions and functions. You should try the 'Optimize' option to disable this code optimization. You may try to edit your matlabFunction as:
matlabFunction(coeff, 'File','myFile', 'Optimize',false,'Vars', {P,Q,R,S,T,U,V,W})
A small example demostrating the same option is below:
syms x
r = x^2*(x^2 + 1);
f = matlabFunction(r,'File','myfile','Optimize',false);
For further information on matlabFunction, refer to the below documentation link: https://www.mathworks.com/help/symbolic/matlabfunction.html

Community Treasure Hunt

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

Start Hunting!