Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

MATLAB: Why is symbolic substitution so slow and what are the faster alternatives?

Date: 2023-01-24 12:40:09

On my machine, the following code:

 

for i=1:200
    1+2+5;
end;

runs in 0.000180 seconds.

Now, this code:

 

syms x y z
f(x,y,z) = x + y+z;

for i=1:200
    f(1,2,5);
end;

is much slower (6.193909 seconds).

For my code, I need to write the derivatives of several complicated functions of 3 variables, and then plug in all the integer points in a given domain. It would help A LOT to be able to do symbolic differentiation and then plugging in, but this seems amazingly slow.

Is there any alternative other than doing everything manually (writing the full derivatives by hand)?

Answers:

You can use matlabFunction to speed up the calculation:

 

syms x y z
f(x,y,z) = x + y+z;
h= matlabFunction(f);
for i=1:200
    h(1,2,5);
end;

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com