Thread Subject: Speeding up Matlab

Subject: Speeding up Matlab

From: Hugh

Date: 22 Oct, 2009 14:01:04

Message: 1 of 4

Hi there,

I have the following code which seeks to find the optimal solution to a rpoblem. Using a linear search over a hyperbox. The problem is I need a reasonably good granularity 1000+ on each parameter but becasue the scale of the problem rises exponentially with the granularity used this soon makes the problem prohibitively expensive in terms of computational time. Is there an in built matlab function I an use which will speed this process up?

function [Result] =PrimePermOp(tau,Y)
tic;
tau=tau.';
Y=Y.';

nObs=length(Y);

z=1;

for(j=1:10)

L5=j/200+0.01;

for(k=1:10)

L4=k/200+0.01;

for(l=1:10)

L3=l/200+0.01;

for(m=1:10)

L2=m/200+0.01;

for(n=1:10)

L1=n/200+0.01;

G= [ones(nObs,1) L1*(1-exp(-tau./L1))./(tau) (1-exp(-((tau-L2).^2)/L3))./(((tau-L2).^2)/L3) (1-exp(-((tau-L4).^2)/L5))./(((tau-L4).^2)/L5) ];
%c0(1)+c0(2)*c0(5)*(1-exp(-xdata./c0(5)))./(xdata) + c0(3)*(1-exp(-((xdata-c0(6)).^2)/c0(7)))./(((xdata-c0(6)).^2)/c0(7))+ c0(4)*(1-exp(-((xdata-c0(8)).^2)/c0(9)))./(((xdata-c0(8)).^2)/c0(9));

alpha =G\Y;
u=Y-G*alpha;
stderr=sqrt(diag((u'*u)/(length(Y)-4)*pinv(G'*G)));
Sum_u2 = sum(u.^2);
error2=(Y-G*alpha).^2;
error2 = sum(error2);
Res(z,:) = [Sum_u2 alpha' L1 L2 L3 L4 L5 error2];
z=z+1;
end
end
end
end
end

optim=find(Res(:,11)==min(Res(:,11)));
%Result=Res(optim,2:end)
Result=toc;

Subject: Speeding up Matlab

From: Bogdan Cristea

Date: 22 Oct, 2009 14:35:14

Message: 2 of 4

"Hugh " <h_a_patience@hotmail.com> wrote in message <hbpoj0$5ld$1@fred.mathworks.com>...
> Hi there,
>
> I have the following code which seeks to find the optimal solution to a rpoblem. Using a linear search over a hyperbox. The problem is I need a reasonably good granularity 1000+ on each parameter but becasue the scale of the problem rises exponentially with the granularity used this soon makes the problem prohibitively expensive in terms of computational time. Is there an in built matlab function I an use which will speed this process up?
>
> function [Result] =PrimePermOp(tau,Y)
> tic;
> tau=tau.';
> Y=Y.';
>
> nObs=length(Y);
>
> z=1;
>
> for(j=1:10)
>
> L5=j/200+0.01;
>
> for(k=1:10)
>
> L4=k/200+0.01;
>
> for(l=1:10)
>
> L3=l/200+0.01;
>
> for(m=1:10)
>
> L2=m/200+0.01;
>
> for(n=1:10)
>
> L1=n/200+0.01;
>
> G= [ones(nObs,1) L1*(1-exp(-tau./L1))./(tau) (1-exp(-((tau-L2).^2)/L3))./(((tau-L2).^2)/L3) (1-exp(-((tau-L4).^2)/L5))./(((tau-L4).^2)/L5) ];
> %c0(1)+c0(2)*c0(5)*(1-exp(-xdata./c0(5)))./(xdata) + c0(3)*(1-exp(-((xdata-c0(6)).^2)/c0(7)))./(((xdata-c0(6)).^2)/c0(7))+ c0(4)*(1-exp(-((xdata-c0(8)).^2)/c0(9)))./(((xdata-c0(8)).^2)/c0(9));
>
> alpha =G\Y;
> u=Y-G*alpha;
> stderr=sqrt(diag((u'*u)/(length(Y)-4)*pinv(G'*G)));
> Sum_u2 = sum(u.^2);
> error2=(Y-G*alpha).^2;
> error2 = sum(error2);
> Res(z,:) = [Sum_u2 alpha' L1 L2 L3 L4 L5 error2];
> z=z+1;
> end
> end
> end
> end
> end
>
> optim=find(Res(:,11)==min(Res(:,11)));
> %Result=Res(optim,2:end)
> Result=toc;

I don't know if there is a built in function doing the same computation, however I would advise to write your own C/C++ routine and interface this routine with Matlab. You could also use Matlab profiler to find what is the most computationally expensive part of your program and implement in C only that part.

Subject: Speeding up Matlab

From: Hugh

Date: 23 Oct, 2009 08:39:03

Message: 3 of 4

"Bogdan Cristea" <cristeab@gmail.com> wrote in message <hbpqj2$iad$1@fred.mathworks.com>...
> "Hugh " <h_a_patience@hotmail.com> wrote in message <hbpoj0$5ld$1@fred.mathworks.com>...
> > Hi there,
> >
> > I have the following code which seeks to find the optimal solution to a rpoblem. Using a linear search over a hyperbox. The problem is I need a reasonably good granularity 1000+ on each parameter but becasue the scale of the problem rises exponentially with the granularity used this soon makes the problem prohibitively expensive in terms of computational time. Is there an in built matlab function I an use which will speed this process up?
> >
> > function [Result] =PrimePermOp(tau,Y)
> > tic;
> > tau=tau.';
> > Y=Y.';
> >
> > nObs=length(Y);
> >
> > z=1;
> >
> > for(j=1:10)
> >
> > L5=j/200+0.01;
> >
> > for(k=1:10)
> >
> > L4=k/200+0.01;
> >
> > for(l=1:10)
> >
> > L3=l/200+0.01;
> >
> > for(m=1:10)
> >
> > L2=m/200+0.01;
> >
> > for(n=1:10)
> >
> > L1=n/200+0.01;
> >
> > G= [ones(nObs,1) L1*(1-exp(-tau./L1))./(tau) (1-exp(-((tau-L2).^2)/L3))./(((tau-L2).^2)/L3) (1-exp(-((tau-L4).^2)/L5))./(((tau-L4).^2)/L5) ];
> > %c0(1)+c0(2)*c0(5)*(1-exp(-xdata./c0(5)))./(xdata) + c0(3)*(1-exp(-((xdata-c0(6)).^2)/c0(7)))./(((xdata-c0(6)).^2)/c0(7))+ c0(4)*(1-exp(-((xdata-c0(8)).^2)/c0(9)))./(((xdata-c0(8)).^2)/c0(9));
> >
> > alpha =G\Y;
> > u=Y-G*alpha;
> > stderr=sqrt(diag((u'*u)/(length(Y)-4)*pinv(G'*G)));
> > Sum_u2 = sum(u.^2);
> > error2=(Y-G*alpha).^2;
> > error2 = sum(error2);
> > Res(z,:) = [Sum_u2 alpha' L1 L2 L3 L4 L5 error2];
> > z=z+1;
> > end
> > end
> > end
> > end
> > end
> >
> > optim=find(Res(:,11)==min(Res(:,11)));
> > %Result=Res(optim,2:end)
> > Result=toc;
>
> I don't know if there is a built in function doing the same computation, however I would advise to write your own C/C++ routine and interface this routine with Matlab. You could also use Matlab profiler to find what is the most computationally expensive part of your program and implement in C only that part.

Thanks I can probably code this up in C++, but how can I interface this with Matlab? I've never done this before it is relatively easy?

+ can I use the free express edition iof visual C++ to do the development. (I realise it's limited in the type of files it cam produce)

Subject: Speeding up Matlab

From: tristram.scott@ntlworld.com (Tristram Scott)

Date: 23 Oct, 2009 10:50:25

Message: 4 of 4

Hugh <h_a_patience@hotmail.com> wrote:
> Hi there,
>
> I have the following code which seeks to find the optimal solution to a
> rpoblem. Using a linear search over a hyperbox. The problem is I need a
> reasonably good granularity 1000+ on each parameter but becasue the scale
> of the problem rises exponentially with the granularity used this soon
> makes the problem prohibitively expensive in terms of computational time.
> Is there an in built matlab function I an use which will speed this process
> up?

[snip]

So, if I read this correctly, you are solving for the minimum of your
function in five-dimensional space by complete enumeration on a grid.

What would be a useful set of input parameters to you function, so we might
attempt to reproduce your results?

I am guessing that your function has many local minima, or you would just
use a simple Newton search or similar. Is that correct?

If you want to solve this using generic techniques, it would seem to fit
into the realms of non-linear programming. The MATLAB optimisation toolbox
should cover that in one if not more ways. Alternatively, use a third
party solver e.g. MINOS or CPLEX.

If you want to code it up yourself, I would suggest using a course grid to
find the region of the global minimum, and then to use a Newton search in
the region near the solution from the course grid search.

There are many texts on math programming. One of the good ones is
Optimization Theory for Large Systems by Leon Lasdon.

--
Dr Tristram J. Scott
Energy Consultant

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com