Search code examples
matlabmatlab-figurematlab-cvst

Plotting using scalar values. (vector/matrix/array input arguments are not accepted by code.)


I'm having a bit of trouble trying to make a 2D plot of a function depending on only one variable. Cutting a long story short, the function can only accept scalar values; it will not accept vectors. Hence it is not possible to use, for example, plot(vector, function(vector)), for a range of independent values vector. I've tried to use loop also, but my knowledge is limited, and it hasn't worked in any case.

To summarise: I want to plot function(x) vs x, however function may only have a scalar input, so taking x=-10:1:10 and then plotting it against function wouldn't work. Can anybody point me in the right direction?


Solution

  • vector = -10:10 % set up your vector
    output = zeros(size(vector); % initialise the output
    for ii = 1:numel(vector)% loop over all elements of your vector
        output(ii) = function(vector(ii)); % feed the function a single element at a time
    end
    
    plot(vector,output) % Now you can plot the two vectors