
Step-by-Step Graphs in Octave
In this lesson, I will explain how to create a step-by-step graph in Octave.
What is a step-by-step graph? The graph of a function is drawn in a discrete form with jumps that look like steps, and it is not continuous. The step graph is useful for highlighting variations in the function's increment.
Let me give you a practical example.
Create an array, x, of the independent variable with values 1 to 20.
>> x=linspace(1,20,20);
Now, create an array for a linear function.
>> y=x
Plot the function with the plot(x,y) function.
>> plot(x,y)
The graph of the function y = f(x) = x is a simple line passing through the origin.
To create a step-by-step graph, use the stairs(x, y) function.
>> stairs(x,y)
The diagram shows the same graph using steps.
The steps are all the same, which means the function has a constant growth rate.
Let me give you another practical example
Create a function y = x^2.
y=x.^2
Plot the function using the instruction plot(x,y)
In this case, the function grows exponentially.
Use the command stairs(x, y) to draw the step-by-step diagram of the function.
>> stairs(x,y)
The step-by-step diagram shows steps of different heights.
The last steps are higher, which means the function has a growth rate that tends to increase.
How to display multiple graphs in one diagram?
To display two step-by-step graphs in the same diagram, indicate the two functions in a Y matrix with two columns.
>> X = linspace(0,4*pi,50)';
>> Y = [0.5*cos(X), 2*cos(X)];
The first column of the Y matrix indicates the values of the function 1/2*cos(X), and the second column indicates the values of the function 2*cos(x).
Now, display the graph using the command stairs(Y) or stairs(X, Y).
>> stairs(X,Y)
In this way, the step-by-step graphs of both functions are displayed in the diagram.
If you find this Niagara lesson useful, keep following us.