How to create a graph time vs value n

12 ビュー (過去 30 日間)
Emilia
Emilia 2020 年 12 月 3 日
コメント済み: Emilia 2020 年 12 月 3 日
Hello,
I have a function that we enter a value n. Next I designed a matrix here and it measures time (tic,toc) until vector returns result.
Now I want to produce a graph for 4<=n<=50 vs time, How can this created?
Thanks for the helpers :)
function [A,VectorOut,B]= pp(n)
tic
f=4*ones(1,n^2);
m=diag(f);
x = zeros(1,n^2);
x_new = x;
j=0;
while j~=n^2
[m(j+1,j+2)]=-1;
[m(j+1,j+4)]=-1;
[m(j+2,j+1)]=-1;
[m(j+4,j+1)]=-1;
j=j+1 ;
end
t=m(1:n^2,1:n^2);
v=zeros(1,n^2);
v(1)=1;
v(n^2)=1;
A=t;
B=v';
for i = 1:n^2
x_new(i) = (B(i) - sum(A(i,1:i-1).*x_new(1:i-1)) - sum(A(i,i+1:n).*x(i+1:n)))/A(i,i);
end
VectorOut=x_new'
toc
end

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 12 月 3 日
編集済み: Ameer Hamza 2020 年 12 月 3 日
I think it might be better if you move the tic ... toc lines outside the function. For example, remove these lines and then run the following code
n = 4:50;
ts = zeros(size(n));
for i = 1:numel(n)
tic
[A,VectorOut,B]= pp(n(i));
ts(i) = toc;
end
plot(n, ts)
Alternatively, you will need to return the value of 't' too. For example,
function [A,VectorOut,B,t]= pp(n)
tic
f=4*ones(1,n^2);
.. ..
.. ..
.. ..
.. ..
.. ..
VectorOut=x_new'
t = toc
end
and then run the code
n = 4:50;
ts = zeros(size(n));
for i = 1:numel(n)
[A,VectorOut,B,ts(i)]= pp(n(i));
end
plot(n, ts)
  1 件のコメント
Emilia
Emilia 2020 年 12 月 3 日
Thank you :)

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


Deepak Gupta
Deepak Gupta 2020 年 12 月 3 日
編集済み: Deepak Gupta 2020 年 12 月 3 日
Hi Emilia,
Placed of toc in the function will just display the elapsed time there instead you need to return the elapsed time from this function, to the script where you will be called this function. i.e. in your function you can write.
totalTime = toc;
And then return it.
In your script you need to save these times with respect to n values. i.e.
n = 4:50;
totalTime = zeros(1, size(n));
for n
[~, ~, ~, nTime] = pp(n);
totalTime(n-3) = nTime;
end
Now you can plot time vs n.
plot(totalTime, n)
Hope this helps,
Cheers,
Deepak
  1 件のコメント
Emilia
Emilia 2020 年 12 月 3 日
Thank you :)

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

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by