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 commentaires

KSSV
KSSV le 1 Fév 2023
What problem you have with code now?
Sumera Zem
Sumera Zem le 1 Fév 2023
The n^n and n^3 and n^2 are different from this figure.

Connectez-vous pour commenter.

 Réponse acceptée

MarKf
MarKf le 1 Fév 2023
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])

Plus de réponses (2)

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 le 1 Fév 2023

2 votes

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 !

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by