Increasing of speed in numerical calculation

4 views (last 30 days)
Kirsten
Kirsten on 29 Apr 2012
Hello,
I'm writing my master thesis about the implementation of a Monte Carlo approach (numerical calculation) in Matlab R2011b. The program needs to much computation time so I want to increase the speed.
My program generates random values for different input variables and evaluates a calculation model to get the values of the output variables (eg a+b=c).This calculation must be carried out approximately 10^6 times in order to provide reasonable results.
I was thinking of converting the random values from double into string followed by replacing the strings of the variables in the model with the strings of the values (by using 'strrep'). Finally the equation should be calculated by the command 'eval'. This procedure has to be repeated for 10^6 times, so it takes unfortunatelly very long time.
My idea was using mex functions for the implemenation of the loops to reduce the computation time. My problem is the transfer and the calculation of vectors and matrices in the mex-function.
Does anyone has a clue of programming with mex-functions or is there possibly another way to increase the speed of large for-loops?
Thank you very much!
  2 Comments
Richard Brown
Richard Brown on 29 Apr 2012
Why do you need to go to strings and back? Why can't you evaluate the expression directly with the randomly generated values?
Jan
Jan on 30 Apr 2012
Cross posting confuses the ones, who want to assist. Please add a link to your question, if you post a question in other forums also.

Sign in to comment.

Answers (6)

Kirsten
Kirsten on 29 Apr 2012
Thanks for your answer!
The equation is only available as a string so I need a command to run it. Do you have any other ideas of what I could use?

bym
bym on 29 Apr 2012
use
str2func()
to convert your string to a function

per isakson
per isakson on 29 Apr 2012
I tried to edit my answer and clicked the wrong button and it was gone.
--- New answer ---
Here is a small demo of
  1. writing code to a file
  2. making a function_handle
  3. executing the code
During development of the code it is useful to leave the file on the HD so that it can be inspected. The string, frmt, may be large.
frmt = 'function output = %s( input )\noutput = %u%sinput;\nend';
function_name = 'on_the_fly';
tic
fid = fopen( [function_name,'.m'], 'w+' );
fprintf( fid, frmt, function_name, 3, '*' );
fclose( fid );
toc
fh = str2func( function_name );
disp( fh( 17 ) )
Running this script displays:
Elapsed time is 0.005069 seconds.
51
The m-file looks like this
function output = on_the_fly( input )
output = 3*input;
end
The alternative is to use EVAL.
Evaluate the alternatives with the profiler.

Kirsten
Kirsten on 30 Apr 2012
I'm using the str2func() command and it works! Thanks! I have only one more question.. How can I use the str2func with a variable number of input values?
  2 Comments
per isakson
per isakson on 30 Apr 2012
Use my_foo(varargin) and fh=str2func(my_foo)
per isakson
per isakson on 30 Apr 2012
How do you get the your "equation" into the function?

Sign in to comment.


Kirsten
Kirsten on 30 Apr 2012
My code for the equation:
Variables{1}='x';
Variables{2}='y';
Variables{3}='z';
equation = 'x.*y.*z';
x = rand(1e6,1);
y = rand(1e6,1);
z = rand(1e6,1);
str = ['@(' Variables{1} ',' Variables{2} ',' Variables{3} ')' equation]
f = str2func(str)
The user should input the number and the symbols of the variables as well as the equation. How can I use the varargin command for this example?
thanks :)

per isakson
per isakson on 2 May 2012
Something like this
f( varargin{:} )
where
varargin == {x,y,z}
assuming the value of varargin is set in a call of the function (which function?).

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!