How to improve computing speed of in matlab
    3 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have attached three file. 1. main file (circle.m) 2. ode file (circle_ode_with_nonlinear_spring.m) 3. forces (forces.m)
Add all file in same folder. just run the code using main file. ode file called using main file.
can any one tell me how to improve speed.
please help me
thanks
3 件のコメント
採用された回答
  Jan
      
      
 2017 年 8 月 2 日
        
      編集済み: Jan
      
      
 2017 年 8 月 2 日
  
      This is too much code to ask in the forum. It is not clear, which is the "main" file and how the code has to be called. A MAT file is required to run the code, but you do not provide it. Therefore we cannot run your code and in consequence we do not know, which part is slow.
But you can run your code. Use the profiler as suggested by Adam:
profile on
Then run your code and examine the logs about the time spent in each line of the code:
profile report
Now find the "bottleneck", the command or block of code, which needs the most run time. It is not worth to improve some code, which takes 2% of the processing time only: If you accelerate it by a factor of 2, the total run time is reduced by 1% only.
As soon as you have found the bottleneck, post this small piece of code only, such that the forum can concentrate on the important detail. Attach some input data to run this piece of code, e.g. created by rand() if this is valid.
Some general hints:
- Do not use global variables. See http://www.mathworks.com/matlabcentral/answers/1971
- Do not load files repeatedly. Store them in a persistent variable instead.
- Do not load a MAT file directly into the workspace, but prefer to catch the output in a variable: Data = load('File.mat'). Otehrwise the variables are created dynamically and Matlab's JIT acceleration cannot work powerful.
- Pre-allocate: Do not let arrays grow iteratively. Search for "pre-allocate" in this forum to find many explanations.
- Vectorize your code: replace e.g.:
for angle = 0:2*pi/p:(2*pi-2*pi/p)
  rpi2_C2_2 (q,1:3) = [r2*cos(angle) r2*sin(angle) 0];
  q = q + 1;
end
by
a         = (0:2*pi/p:(2*pi-2*pi/p)).';
rpi2_C2_2 = [r2*cos(a), r2*sin(a), zeros(size(a)];
Note that "angle" is a builtin Matlab function and shadowing it by a variable may cause unexpected behavior. Therefore I've used "a".
- Code blocks like this are impossible to debug:
            q27=Rv_0'*q13;
            q15=q27(1,1);
            q32=q19(2,1);
            q37=q19(3,1);
            f28=f27(1,1);
            f33=f27(2,1);
            f38=f27(3,1);
            e15=K15*(q15/(1-q15^2));
            e32=K32*(q32/(1-q32^2)); 
            e37=K37*(q37/(1-q37^2));
            e28=R28*f28;
            e33=R33*f33;
            e38=R38*f38;
            e29=e15+e28;
            e34=e32+e33;
            e39=e37+e38;
Don't do this. It is the pure horror for a programmer to find a bug in such code. Use arrays instead. Matlab is perfect for vector and matrix operations. Creating a pile of variables is not only very ugly, but slow also. But unfortunately I'm convinced that it is impossible to fix this code now, because the massive creation of variables is far too complex to keep the overview.
- Use the standard code indentation and do not gather outdated code by commenting it out. Out-commented code confuses the reader. Better use a version control system, if you do not want to loose older pieces of code. On the other hand your code contains to few useful comments, which explains the intention of the code.
- Most likely this is a waste of time:
parfor q = 1:1:3  
  e10_(q,1)=sum(e10(q,:));
end
Better:
e10_ = sum(e10, 2);
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

