plot running time complexity
Show older comments
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.

Accepted Answer
More Answers (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
on 1 Feb 2023
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 !
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
