how to decrease the time consumption of the code

7 ビュー (過去 30 日間)
Abirami
Abirami 2014 年 9 月 4 日
編集済み: Abirami 2014 年 9 月 4 日
Hello I've already got a lot of help in developring the encryption technique from matlab answers. Now i have another problem. I need to reduce the time consumption of the folowing part of the code.Here i generate two chaotic sequences using Chen's hyperchaotic system. I've defined a function called Chen1.m and i've given both the codes below.This part of the code is taking almost 15 minutes.Please help.Thanks in advance.
%the values of a, b, c, d, k here
a = 36;
b = 3;
c = 28;
d = 16;
k = 0.2;
%vector v0, which is a 4x1 vector of initial conditions
v0 = [0.3 -0.4 1.2 1];
fun = @(t,v) chen1(t,v,a,b,c,d,k);
[t, v] = ode45(fun, [0 1.54], v0);
x = v(:,1);
x(257)=[];
y = v(:,2);
y(257)=[];
[lx,fx]=sort(x);
[ly,fy]=sort(y);
S=fx;
T=(fy)';
%%matrix generated for scrambling
[Tgrid, Sgrid] = meshgrid( T, S );
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
The function is defined as follows
function vdot = chen(t, v, a, b, c, d, k)
x = v(1); y = v(2); z = v(3); q = v(4);
xdot = a*(y-x);
ydot = -(x*z)+(d*x)+(c*y)-q;
zdot = (x*y)-(b*z);
qdot = x+k;
vdot = [xdot; ydot; zdot; qdot];
end
  1 件のコメント
Adam
Adam 2014 年 9 月 4 日
First thing you should do is run the profiler on it:
profile on
functionCall(...)
profile off
profile viewer
That will tell you where the bottlenecks are. One of the worst things you can do when trying to speed up code is to start focusing on really speeding up a piece of code that only takes 1% of the total running time anyway.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2014 年 9 月 4 日
Don't use cell arrays to hold B,E, F, and G. Cell arrays are slow and unnecessary for holding arrays of scalars. Once you've repaired the damage with
B=cell2mat(B)
E=cell2mat(E)
you should also replace this code segment
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
with this
F=B(S,T);
G=E(S,T);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by