0
votes

I would like to plot a variable at each unit-time interval. In the example below I would therefore like to plot x at time equal to 1, 2, 3, 4 and 5 (but not time equal to 1.25, 1.5, 1.75, 2.25 and so on). Is it for example possible to save the x-array for times equal to 1, 2, 3, 4 and 5, and then plot this vs time (so we should get five graphs in the same figure)?

dtime=0.25;         % time-step
NTime=5/dtime;      % Number of time-steps

dspace=0.5;         % Distance between each x-values
NSpace=10/dspace;   % Number of x-values at each time-steps

x_old=ones(NSpace,1);
for j=1:NTime
for i=1:NSpace
x(i,1)=x_old(i,1)*5;
end
x_old=x;
end
1

1 Answers

1
votes

Yes, it's possible. Try something like:

dtime=0.25;         % time-step
NTime=5/dtime;      % Number of time-steps

dspace=0.5;         % Distance between each x-values
NSpace=10/dspace;   % Number of x-values at each time-steps

x_old=ones(NSpace,1);
figure; %create a figure
for j=1:NTime
    for i=1:NSpace
        x(i,1)=x_old(i,1)*5;
    end

    % logic to find right time and plot
    current_time = j * dtime;
    if ~isempty(find(current_time == [1,2,3,4,5] ))
        plot(1:dspace:NSpace, x) % you might have to change this depending on what you want to plot
        hold on;
    end
x_old=x;
end