Search code examples
matlabfor-loopgraphnumerical-methods

plot two series in matlab


I would want to graph the values of r1 and r2 in one plot, how can I do it?

for i=1:10
r1=rand(1)
r2=max(rand(1,2))
end

Thank you for your help!


Solution

  • There are many, many ways, here is one that exposes a few options:

    clc;
    clear all, close all;
    
    r1 = zeros(10, 1);
    r2 = zeros(10, 1);
    x = 1:10;
    
    for i=1:10
        r1(i) = rand(1);
        r2(i) = max(rand(1,2));
    end
    
    figure('Name', 'Values of r1 and r2', 'NumberTitle', 'off');
    hold on;
    axis([0, 11, -0.5, 1.5])
    plot(x, r1, '+', x, r2,'o'); %many possibilities and options here for line style
    h = legend('r1', 'r2');
    xlabel('index')
    ylabel('value')
    
    disp('press a key');
    pause();
    close all;