Increasing of speed in numerical calculation
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Richard Brown
il 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
il 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.
Risposte (6)
per isakson
il 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
- writing code to a file
- making a function_handle
- 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.
0 Commenti
per isakson
il 2 Mag 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?).
1 Commento
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!