How to save multiple functions in an array in MATLAB?

I am new to MATLAB, so I don’t even know if this is possible, but here it is ... I am trying to print several lines in one graph using the plot function. The problem is that I want to indicate how many rows the graph should be displayed by simply changing the variable, for example: {This is pseudo-code for what I want to do}

 number_of_lines = 4;
 x = 0:0.5:5;

 function_output[number_of_lines];

 for n=0:number_of_lines
     function_output[n] = sin(n + x);
 end

 for n=0:number_of_lines
     plot(x,function_output[n]);
 end

I know that the above pseudo code is not exactly MATLAB, but all I want to know is whether it is possible to make such an algorithm in MATLAB.

+3
source share
2 answers

Here is one way to implement your example in MATLAB:

function_output = zeros(numel(x), number_of_lines);  % Initialize a 2-D array
for n = 1:number_of_lines                   % MATLAB uses 1-based indexing
    function_output(:, n) = sin(n + x).';  %' Compute a row vector, transpose
                                            %   it into a column vector, and
                                            %   place the data in a column of
                                            %   the 2-D array
end
plot(x, function_output);  % This will plot one line per column of the array

And here are some documentation links that you should read to find out and understand the code above:

+2

MATLAB? - . , ...

http://www.mathworks.com/help/techdoc/creating_plots/f9-53405.html

script, : http://www.mathworks.com/help/techdoc/creating_plots/f9-47085.html

--- script

number_of_lines = 4;

x = 0: 0,5: 5;

function_output = (NUMBER_OF_LINES, 1) * ;

, ;

n = 1: number_of_lines

function_output (n, 1) = plot (x, sin (n + x), 'color', [1-n/number_of_lines 0 n/number_of_lines]);

(function_output)

.

+1
source

Source: https://habr.com/ru/post/1778568/


All Articles