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일

0 개 추천

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일

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 2023년 2월 1일

2 개 추천

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 !

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

질문:

2023년 2월 1일

답변:

2023년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by