lettura simple

Multiple plots with overlapping functions in Octave

In this online lesson, I will show you how to plot two or more functions on the same graph in Octave.

Let's start with a practical example.

Create an array (vector) with ten values in ascending sequence from 1 to 10.

>> x = linspace(1,10,10);

Then create two arrays, y1 and y2, with the values of two different functions.

The square of the values in x

>> y1=x.^2;

and the cube of the values in x

>> y2=x.^3

The arrays x, y1, and y2 each contain 10 elements.

The values in the arrays x, y1, and y2.

Now plot the two graphs using the plot() function.

plot(x,y1,x,y2)

The final result will be a multiple graph with two functions.

The multiple graph

You can use the same technique to plot three or more different functions on the same graph.

To customize the colors of the functions, use these codes:

0 = black
1 = red
2 = green
3 = blue
4 = purple
5 = cyan
6 = white

For example, to plot the first function in red ("1") and the second function in black ("0"), type

>> plot(x,y1,"1",x,y2,"0")

This way, you can choose the colors of the functions yourself.

Customizing the colors.

You can also customize the symbol for data points for each function.

For example, use the "+" symbol for the first function and the "x" symbol for the second function.

>> plot(x,y1,"1+",x,y2,"0x")

This way, you will get a different view of the graph.

Customizing the symbols.

Note. In this lesson, I explained how to display two or more overlapping graphs in a single Cartesian diagram. If you want to display the graphs in separate diagrams, I recommend you follow another lesson on multiple graphs with separate views.

If you find this Octave lesson by Nigiara helpful, keep following us.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin