1

I had the task to plot the computational time cost of 2 algorithms in function of the size of the matrices they solved.

What I did so far was made a method that did every algorithm x amount of times and stored the times.

In the end, I've got a matrix like such:

T =

1.248008000000027e-003    9.360059999994519e-004
7.488048000004710e-003    1.456009333332986e-002
4.992032000000109e-002    2.808017999999492e-002
1.809611600000039e-001    1.489809550000018e-001
5.740836800000352e-001    5.865637599999672e-001
4.748670439999978e+000    4.714350220000005e+000

With the first row being the computational cost of the 2 algorithms for a matrix of size 20x20, the second row the cost of the 2 algorithms for a matrix of size 40x40, then for 80x80, 160x160, 320x320 and 640x640.

The reason the 2 columns are nearly the same is because I've yet to write the second algorithm and just used the first algorithm 2 times.

What I now need to do is plot the cost of the 2 algorithms, in the same figure, in function of the increasing matrix size. I'm however stuck on the plot syntax and I keep failing in getting a pretty figure. Could anybody help?

1
  • Can you show us the code that you have tried and failed with? Commented Apr 22, 2013 at 14:06

3 Answers 3

1

How about

T = [
    1.248008000000027e-003    9.360059999994519e-004
    7.488048000004710e-003    1.456009333332986e-002
    4.992032000000109e-002    2.808017999999492e-002
    1.809611600000039e-001    1.489809550000018e-001
    5.740836800000352e-001    5.865637599999672e-001
    4.748670439999978e+000    4.714350220000005e+000];


figure, hold on

 % column 1
plot(1:size(T,1), T(:,1), 'r.-');

% column 2
plot(1:size(T,1), T(:,2), 'b.-');

% nicer labels at the X-tick locations
set(gca, 'xticklabel', {...
    '20×20',...
    '40×40',...
    '80×80',...
    '160×160',...
    '320×320',...
    '640×640'}...
);

% finish plot
grid on
ylabel('Execution time required [s]')
xlabel('Matrix size [-]')

legend(...
    'Algorithm 1',...
    'Algorithm 2',...
    'location', 'NorthWest'...
);

results:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Turns out, I didn't understand some things very well. This works beautifully.
0

How about just this:

plot(T)

or if you want the x values, define x and then

plot(x,T(:,1))
hold all
plot(x,T(:,2))

Comments

0

If the first column belongs to the Computation time of the first algorithm for different sizes, and the second column belongs to the Computation time of the second algorithm, the you can plot it beautifully:

assume the matrix for stored computation times is TimeComputation

figure(1)

plot(TimeComputation(:,1),'-.r')

hold on

plot(TimeComputation(:,2),'--.b')

legend('Function 1','Function 2')

Let me know if you have any further questions!

1 Comment

Thank you, this is much the same as the suggestion underneath, but that one is more expansive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.