plot running time complexity

22 次查看(过去 30 天)
I want to plot these plots
n = linspace(1,3);
f1 = n.^n;
f2 = n.^3;
f3 = 2.^n;
f4 = n.^2;
f5 = n;
f6 = ones(size(n));
plot(n,f1,'r');
hold on
plot(n,f2,'b');
plot(n,f3,'g');
plot(n,f4,'y');
plot(n,f5,'c');
plot(n,f6,'m');
legend('O(n^n)','O(n^3)','O(2^n)','O(n^2)','O(n)','O(1)');
But the result is differet from this one.
  2 个评论
KSSV
KSSV 2023-2-1
What problem you have with code now?
Sumera Zem
Sumera Zem 2023-2-1
The n^n and n^3 and n^2 are different from this figure.

请先登录,再进行评论。

采纳的回答

MarKf
MarKf 2023-2-1
Well, in this figure the functions behind the lines are just not the simple ones displayed. Otherwise for example all n^n and n^3 and n^2 should intersect at [1,1], as well as O(1).
Likely it's just a simplification to show O notations / program run times / space requirements, so that O(1) stays stable (not necessarily at 1), n^n at first does not increase as much as the power functions but then more steeply etc.
n = linspace(0,7);
f1 = ones(size(n));
fn = n;
f2 = n.^2;
f3 = n.^3;
fns = n.^n;
plot(n,f1,'k');
hold on
plot(n,fn,'b');
plot(n,f2,'r');
plot(n,f3,'y');
plot(n,fns,'c');
legend('O(1)','O(n)','O(n^2)','O(n^3)','O(n^n)');
axis square,ylim([0,7])

更多回答(2 个)

Mathieu NOE
Mathieu NOE 2023-2-1
you need your n start at zero and not 1 to see n^2 and n^3 curves below curve y = 1 and y = n
n = linspace(0,2);
f1 = n.^n;
f2 = n.^3;
f3 = 2.^n;
f4 = n.^2;
f5 = n;
f6 = ones(size(n));
plot(n,f1,'r',n,f2,'b',n,f3,'g',n,f4,'y',n,f5,'c',n,f6,'m');
legend('O(n^n)','O(n^3)','O(2^n)','O(n^2)','O(n)','O(1)');

Sarvesh Kale
Sarvesh Kale 2023-2-1
You can try with the following code snippet
syms n % define a symbolic variable
figure ;
legend on; % highlight which equation represents which line
hold on ; % allow multiple plots in same figure
fplot(n^n,'LineWidth',1.5); % line width property modified for proper visibility
fplot(n^3,'LineWidth',1.5);
fplot(n^2,'LineWidth',1.5);
fplot(2^n,'LineWidth',1.5);
fplot(n,'LineWidth',1.5) ;
fplot(n^0 * 100,'LineWidth',1.5);
fplot(sqrt(n),'LineWidth',1.5);
xlim([3 15]); % for proper visibility
ylim([0 1500]);% for proper visibility
I hope this answers your queries, please accept the query as answered if satisfied !

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by