Search code examples
matlabmatlab-figure

Imposing a condition on the work of the 'hold on' command


There is a code for constructing harmonics (cosines with different frequency values in the range from 0.8 to 11.1 with step 0.1). It is necessary to output a graph in which there is each separate frequency iteration of the cosine.

Well, there are too many of them (about a hundred). It's hard to analyze such a mess of dependencies y(x). Is it possible to make it so that not all one hundred frequency iterations are output, but only selective ones? For example, those y(x) that are obtained at a frequency = 0.8:1:10.8. I.e., impose a condition on 'hold on' command. At the same time, without changing the array of frequency values that is given initially, in order to always be able to set any other condition to 'hold on'.

clear,clc
%% Data 
x = 0:1:100; 
a = 5.76; 
omega = 0.8:0.1:11.1; 
k = omega ./ a; 

[X,K,OMEGA] = meshgrid(x,k,omega)
y = cos(OMEGA - K .* X);

figure(1);
plot(x, y); hold on
title('y(x)');
grid on;

enter image description here


Solution

  • Firstly, hold on is doing nothing in your code, since you call it after the plot: it just means any future plotting on this axis will not discard existing plots. Even with hold off, all plots in your above code are kept, because they are from a single plot command.

    Secondly, k is just a multiple of omega, so you don't want to include it in your meshgrid. Instead of

    k = omega ./ a; 
    [X,K,OMEGA] = meshgrid(x,k,omega)
    

    do (; is also important to avoid lots of output)

    [X,OMEGA] = meshgrid(x,omega);
    K = OMEGA / a; 
    

    Finally, to plot only some of the results, use e.g. plot(x, y(1:10:end,:)) to show every 10th one.