Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Speed up fairly short code

Subject: Speed up fairly short code

From: Kobye

Date: 15 Jul, 2013 20:27:11

Message: 1 of 10

I have a code (script) of 462 lines. In parts, it uses the symbolic toolbox. It also makes calls to 3 other functions that I have written, that are already highly efficient.

The code is a mechanical model (theory of elasticity) that describes two plates joined by adhesive. The model is very elegant in its formulation, but currently it is way too slow. I have the option of either discarding it, or massively speeding it up. I have reason to believe that large speed increases are feasible (similar models exist and have been used for sensitivity analyses in the literature).

If anyone would like to help me find ways to speed it up, your help would be immeasurably appreciated. I will send you the code privately as I don't necessarily want it in the public domain.

Kobye

PS the main bottlenecks seem to be symbolic integrations of my Bernstein basis polynomials. I think that maybe the code could be sped up here by using the Matlab poly options, but I am totally unfamiliar with these.

Subject: Speed up fairly short code

From: TideMan

Date: 16 Jul, 2013 10:21:10

Message: 2 of 10

On Tuesday, July 16, 2013 8:27:11 AM UTC+12, Kobye wrote:
> I have a code (script) of 462 lines. In parts, it uses the symbolic toolbox. It also makes calls to 3 other functions that I have written, that are already highly efficient.
>
>
>
> The code is a mechanical model (theory of elasticity) that describes two plates joined by adhesive. The model is very elegant in its formulation, but currently it is way too slow. I have the option of either discarding it, or massively speeding it up. I have reason to believe that large speed increases are feasible (similar models exist and have been used for sensitivity analyses in the literature).
>
>
>
> If anyone would like to help me find ways to speed it up, your help would be immeasurably appreciated. I will send you the code privately as I don't necessarily want it in the public domain.
>
>
>
> Kobye
>
>
>
> PS the main bottlenecks seem to be symbolic integrations of my Bernstein basis polynomials. I think that maybe the code could be sped up here by using the Matlab poly options, but I am totally unfamiliar with these.

This:
>The model is very elegant in its formulation, but currently it is way too slow.
seems like an oxymoron.

Subject: Speed up fairly short code

From: Kobye

Date: 16 Jul, 2013 11:00:16

Message: 3 of 10

TideMan <mulgor@gmail.com> wrote in message <917984b5-dba1-4e8e-94fd-c9671b0f24e9@googlegroups.com>...
> On Tuesday, July 16, 2013 8:27:11 AM UTC+12, Kobye wrote:
> > I have a code (script) of 462 lines. In parts, it uses the symbolic toolbox. It also makes calls to 3 other functions that I have written, that are already highly efficient.
> >
> >
> >
> > The code is a mechanical model (theory of elasticity) that describes two plates joined by adhesive. The model is very elegant in its formulation, but currently it is way too slow. I have the option of either discarding it, or massively speeding it up. I have reason to believe that large speed increases are feasible (similar models exist and have been used for sensitivity analyses in the literature).
> >
> >
> >
> > If anyone would like to help me find ways to speed it up, your help would be immeasurably appreciated. I will send you the code privately as I don't necessarily want it in the public domain.
> >
> >
> >
> > Kobye
> >
> >
> >
> > PS the main bottlenecks seem to be symbolic integrations of my Bernstein basis polynomials. I think that maybe the code could be sped up here by using the Matlab poly options, but I am totally unfamiliar with these.
>
> This:
> >The model is very elegant in its formulation, but currently it is way too slow.
> seems like an oxymoron.

Why would it be an oxymoron? I said the model is elegant, I didn't say my code is elegant. Quite a strange comment to make.

Subject: Speed up fairly short code

From: Kobye

Date: 16 Jul, 2013 13:27:11

Message: 4 of 10

I have copied the code below after all, this should be easier for everyone. I have tried to speed it up myself using parfor, with some improvement, but not nearly enough.


%Warning: this script will clear and therefore erase your stored variables. If you do not
%desire this, delete the next two lines of code, but beware that the
%script may not function properly.
clear;
clc;

%Define geometry
L=84e-3; %length
W=24e-3; %width

%Define the elasticity tensor
C1=[303973359.614198 89929777.3924441 0 0 0 0 0 0;89929777.3924441 303973359.614198 0 0 0 0 0 0;0 0 107021791.110877 0 0 0 0 0;0 0 0 497.738940473395 80.7354743011537 -15.9810696297077 0 0;0 0 0 80.7354743011537 391.198476275344 -15.9810696297077 0 0;0 0 0 -15.9810696297077 -15.9810696297077 103.524825925731 0 0;0 0 0 0 0 0 20440000 0;0 0 0 0 0 0 0 20440000];

%Specify the degree of the approximating basis polynomials in x/y
m=3;
n=3;

%Construct basis polynomials
syms x y z
B11 = sym(zeros(m+1, 1));
B21 = sym(zeros(m+1, 1));
B12 = sym(zeros(n+1, 1));
B22 = sym(zeros(n+1, 1));

for i = 0:m
    A=factorial(m)/(factorial(i)*factorial(m-i))*(x/L)^i*(1-x/L)^(m-i);
    B11(i+1)=A;
end

for i = 0:n
    B=factorial(n)/(factorial(i)*factorial(n-i))*(y/W)^i*(1-y/W)^(n-i);
    B12(i+1)=B;
end

%Construct displacement vectors using the basis polynomials in x/y
zero_vector=zeros((m+1)*(n+1),1);
counter=0;
Vx1 = sym(zeros((m+1)*(n+1),1));
Vy1 = sym(zeros((m+1)*(n+1),1));
Vz1 = sym(zeros((m+1)*(n+1),1));
Vrx1 = sym(zeros((m+1)*(n+1),1));
Vry1 = sym(zeros((m+1)*(n+1),1));
for i = 0:m
    for j=0:n
        counter=counter+1;
        C=B11(i+1)*B12(j+1);
        Vx1(counter,1)=C; %Bernstein bases for x-displacement
        Vy1(counter,1)=C; %Bernstein bases for y-displacement
        Vz1(counter,1)=C; %Bernstein bases for z-displacement
        Vrx1(counter,1)=C; %Bernstein bases for x-rotation
        Vry1(counter,1)=C; %Bernstein bases for y-rotation
    end
end
Vx1=[Vx1;zero_vector;zero_vector;zero_vector;zero_vector]; %Displacement in x
Vy1=[zero_vector;Vy1;zero_vector;zero_vector;zero_vector]; %Displacement in y
Vz1=[zero_vector;zero_vector;Vz1;zero_vector;zero_vector]; %Displacement in z
Vrx1=[zero_vector;zero_vector;zero_vector;Vrx1;zero_vector]; %Rotation in x
Vry1=[zero_vector;zero_vector;zero_vector;zero_vector;Vry1]; %Rotation in y

%Define the strain tensor
Vxx1=diff(Vx1,x);
Vyy1=diff(Vy1,y);
Vxy1=diff(Vx1,y);
Vyx1=diff(Vy1,x);
Vzx1=diff(Vz1,x);
Vzy1=diff(Vz1,y);
Vrxx1=diff(Vrx1,x);
Vryy1=diff(Vry1,y);
Vrxy1=diff(Vrx1,y);
Vryx1=diff(Vry1,x);

B_epsilon_1=[Vxx1.';Vyy1.';Vxy1.'+Vyx1.']; %in-plane strains
B_kappa_1=[Vrxx1.';Vryy1.';Vrxy1.'+Vryx1.']; %curvatures
B_gamma_1=[Vzy1.'-Vry1.';Vzx1.'-Vrx1.']; %shear strains
B_1=[B_epsilon_1;B_kappa_1;B_gamma_1]; %overall strain tensor

%Calculate stiffness matrix
%**This is where the longest part of the code is**
tic
K=int(int(B_1.'*C1*B_1,x,0,L),y,0,W); %Perform area integral over geometry
K=double(K); %Convert the matrix obtained to doubles
toc
%**This is where the longest part of the code ends**


%Faster stiffness matrix calculation
if matlabpool('size') == 0
    matlabpool open 4 %Assign 4 cores to task
end
tic
K1_unintegrated=triu(B_1.'*C1*B_1);
K1_unintegrated = reshape(K1_unintegrated,1,(5*(m+1)*(n+1))^2);
Batch_length=(5*(m+1)*(n+1))^2;
addendum=0;
while mod(Batch_length,4)>0
    addendum=addendum+1;
    Batch_length=Batch_length+1;
end
Batch_in=sym(zeros(4,Batch_length/4));
Batch_in(1,:)=K1_unintegrated(1:Batch_length/4);
Batch_in(2,:)=K1_unintegrated(Batch_length/4+1:2*Batch_length/4);
Batch_in(3,:)=K1_unintegrated(2*Batch_length/4+1:3*Batch_length/4);
Batch_in(4,1:Batch_length/4-addendum)=K1_unintegrated(3*Batch_length/4+1:4*Batch_length/4-addendum);
Batch_out=sym(zeros(4,Batch_length/4));
parfor i=1:4
Batch_out(i,:)=int(int(Batch_in(i,:),x,0,L),y,0,W);
end
K1_fast =[Batch_out(1,:),Batch_out(2,:),Batch_out(3,:),Batch_out(4,1:Batch_length/4-addendum)];
K1_fast = reshape(K1_fast,5*(m+1)*(n+1),5*(m+1)*(n+1));
K1_fast = K1_fast+triu(K1_fast,1).';
K1_fast=double(K1_fast);
toc

Subject: Speed up fairly short code

From: Steven_Lord

Date: 16 Jul, 2013 14:03:00

Message: 5 of 10



"Kobye " <kobye.bodjona@gmail.com> wrote in message
news:ks1luv$i3h$1@newscl01ah.mathworks.com...
> I have a code (script) of 462 lines. In parts, it uses the symbolic
> toolbox. It also makes calls to 3 other functions that I have written,
> that are already highly efficient.
>
> The code is a mechanical model (theory of elasticity) that describes two
> plates joined by adhesive. The model is very elegant in its formulation,
> but currently it is way too slow. I have the option of either discarding
> it, or massively speeding it up. I have reason to believe that large speed
> increases are feasible (similar models exist and have been used for
> sensitivity analyses in the literature).
>
> If anyone would like to help me find ways to speed it up, your help would
> be immeasurably appreciated. I will send you the code privately as I don't
> necessarily want it in the public domain.
>
> Kobye
>
> PS the main bottlenecks seem to be symbolic integrations of my Bernstein
> basis polynomials. I think that maybe the code could be sped up here by
> using the Matlab poly options, but I am totally unfamiliar with these.

Try converting that symbolic integration into numeric integration using
matlabFunction, FEVAL, or EVALIN (as shown in the Tips section of the
documentation page below) and INTEGRAL.

http://www.mathworks.com/help/symbolic/matlabfunction.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Speed up fairly short code

From: Eric Sampson

Date: 16 Jul, 2013 15:26:09

Message: 6 of 10

"Steven_Lord" <slord@mathworks.com> wrote in message <ks3jql$4jb$1@newscl01ah.mathworks.com>...
>
>
> "Kobye " <kobye.bodjona@gmail.com> wrote in message
> news:ks1luv$i3h$1@newscl01ah.mathworks.com...
> > I have a code (script) of 462 lines. In parts, it uses the symbolic
> > toolbox. It also makes calls to 3 other functions that I have written,
> > that are already highly efficient.
> >
> > The code is a mechanical model (theory of elasticity) that describes two
> > plates joined by adhesive. The model is very elegant in its formulation,
> > but currently it is way too slow. I have the option of either discarding
> > it, or massively speeding it up. I have reason to believe that large speed
> > increases are feasible (similar models exist and have been used for
> > sensitivity analyses in the literature).
> >
> > If anyone would like to help me find ways to speed it up, your help would
> > be immeasurably appreciated. I will send you the code privately as I don't
> > necessarily want it in the public domain.
> >
> > Kobye
> >
> > PS the main bottlenecks seem to be symbolic integrations of my Bernstein
> > basis polynomials. I think that maybe the code could be sped up here by
> > using the Matlab poly options, but I am totally unfamiliar with these.
>
> Try converting that symbolic integration into numeric integration using
> matlabFunction, FEVAL, or EVALIN (as shown in the Tips section of the
> documentation page below) and INTEGRAL.
>
> http://www.mathworks.com/help/symbolic/matlabfunction.html
>
> --
> Steve Lord

Steve, do you think this type of situation could be detected by M-lint and highlighted in the Editor?

Subject: Speed up fairly short code

From: Kobye

Date: 16 Jul, 2013 17:40:10

Message: 7 of 10

Steve, I'm quite unsure about how one would proceed with this. First of all, I guess I would have to use integral2 since I'm performing a surface integral. Second of all, it doesn't seem like MATLAB likes numerically integrating a matrix per entry. I was trying the below example as a demonstrator.

a=[x^2,y^3;x*y,2*y*x^2]
 
b=matlabFunction(a)

q = integral2(b,0,1,0,1)

I get a bunch of errors. I have no doubt that I can extract each entry from the matrix individually, define it as a function, and then integrate it numerically. However, there are so many operations here that it seems at first glance it wouldn't be efficient.

Subject: Speed up fairly short code

From: Steven_Lord

Date: 17 Jul, 2013 13:46:13

Message: 8 of 10



"Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
news:ks3omh$iqc$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <ks3jql$4jb$1@newscl01ah.mathworks.com>...
>>
>>
>> "Kobye " <kobye.bodjona@gmail.com> wrote in message
>> news:ks1luv$i3h$1@newscl01ah.mathworks.com...
>> > I have a code (script) of 462 lines. In parts, it uses the symbolic
>> > toolbox. It also makes calls to 3 other functions that I have written,
>> > that are already highly efficient.
>> >
>> > The code is a mechanical model (theory of elasticity) that describes
>> > two plates joined by adhesive. The model is very elegant in its
>> > formulation, but currently it is way too slow. I have the option of
>> > either discarding it, or massively speeding it up. I have reason to
>> > believe that large speed increases are feasible (similar models exist
>> > and have been used for sensitivity analyses in the literature).
>> >
>> > If anyone would like to help me find ways to speed it up, your help
>> > would be immeasurably appreciated. I will send you the code privately
>> > as I don't necessarily want it in the public domain.
>> >
>> > Kobye
>> >
>> > PS the main bottlenecks seem to be symbolic integrations of my
>> > Bernstein basis polynomials. I think that maybe the code could be sped
>> > up here by using the Matlab poly options, but I am totally unfamiliar
>> > with these.
>>
>> Try converting that symbolic integration into numeric integration using
>> matlabFunction, FEVAL, or EVALIN (as shown in the Tips section of the
>> documentation page below) and INTEGRAL.
>>
>> http://www.mathworks.com/help/symbolic/matlabfunction.html
>>
>> --
>> Steve Lord
>
> Steve, do you think this type of situation could be detected by M-lint and
> highlighted in the Editor?

It might be detectable, but I don't think it should be reported. In some
cases, you actually want to perform the symbolic integration; in this case,
I think numeric integration would be sufficient for the OP's situation.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Speed up fairly short code

From: Steven_Lord

Date: 17 Jul, 2013 14:00:19

Message: 9 of 10



"Kobye " <kobye.bodjona@gmail.com> wrote in message
news:ks40hq$b4m$1@newscl01ah.mathworks.com...
> Steve, I'm quite unsure about how one would proceed with this. First of
> all, I guess I would have to use integral2 since I'm performing a surface
> integral. Second of all, it doesn't seem like MATLAB likes numerically
> integrating a matrix per entry. I was trying the below example as a
> demonstrator.
>
> a=[x^2,y^3;x*y,2*y*x^2]
>
> b=matlabFunction(a)
>
> q = integral2(b,0,1,0,1)
>
> I get a bunch of errors. I have no doubt that I can extract each entry
> from the matrix individually, define it as a function, and then integrate
> it numerically. However, there are so many operations here that it seems
> at first glance it wouldn't be efficient.

I wouldn't be so sure. The cost of N matrix entry extractions plus N numeric
integrations may still be less than the cost of N symbolic integrations,
particularly if your functions are complicated. For this simple example the
symbolic approach wins; for a more complicated function I expect it may be
different.

syms x y
a=[x^2,y^3;x*y,2*y*x^2];
tic
q = zeros(size(a));
for k = 1:numel(a)
    b = matlabFunction(a(k), 'vars', [x y]);
    q(k) = integral2(b, 0, 1, 0, 1);
end
toc

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Speed up fairly short code

From: Kobye

Date: 17 Jul, 2013 20:48:19

Message: 10 of 10

Thanks for the help, I will give it a go with some large matrices and will report back to this thread so that other users may benefit in the future.

"Steven_Lord" <slord@mathworks.com> wrote in message <ks681j$plj$1@newscl01ah.mathworks.com>...
>
>
> "Kobye " <kobye.bodjona@gmail.com> wrote in message
> news:ks40hq$b4m$1@newscl01ah.mathworks.com...
> > Steve, I'm quite unsure about how one would proceed with this. First of
> > all, I guess I would have to use integral2 since I'm performing a surface
> > integral. Second of all, it doesn't seem like MATLAB likes numerically
> > integrating a matrix per entry. I was trying the below example as a
> > demonstrator.
> >
> > a=[x^2,y^3;x*y,2*y*x^2]
> >
> > b=matlabFunction(a)
> >
> > q = integral2(b,0,1,0,1)
> >
> > I get a bunch of errors. I have no doubt that I can extract each entry
> > from the matrix individually, define it as a function, and then integrate
> > it numerically. However, there are so many operations here that it seems
> > at first glance it wouldn't be efficient.
>
> I wouldn't be so sure. The cost of N matrix entry extractions plus N numeric
> integrations may still be less than the cost of N symbolic integrations,
> particularly if your functions are complicated. For this simple example the
> symbolic approach wins; for a more complicated function I expect it may be
> different.
>
> syms x y
> a=[x^2,y^3;x*y,2*y*x^2];
> tic
> q = zeros(size(a));
> for k = 1:numel(a)
> b = matlabFunction(a(k), 'vars', [x y]);
> q(k) = integral2(b, 0, 1, 0, 1);
> end
> toc
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us