If you see this, something is wrong
First published on Thursday, Mar 5, 2026 and last modified on Thursday, Mar 5, 2026
Draw a Function
This is the web output of the basic sample document.
And now we shall learn to draw a function, with numpy and matplotlib.pyplot as well.
First we must have numpy and matplotlib.pyplot available, so we type from numpy import * and from matplotlib.pyplot import *.
We must define a function The first method is with a for loop.
It is the more general method.
We must define vectors x and y.
We shall define two vectors y1 equal to cos x and y2 equal to sin x.
We must initialize the vectors.
with zeros and a certain length.
We choose 101.
Then we make a for loop for k going from -50 to 50.
51 is not included.
Then we define x of k plus 50, that is k multiplied by pi over 10.
It is a sampling from minus 5 pi to 5 pi.
Then we define y1 of k plus 50, that is the cos of x of k plus 50.
And y2 of k plus 50, that is the sin of x of k plus 50.
So we define element by element the cosine of x and the sinus of x.
And then we shall plot the functions y1 equal to cos of x and y2 equal to sin of x.
Launch Anaconda in Spyder and follow me to see how to do that.
First we define a new file.
Then we give it a name.
We place it in the right directory and we call it Functions Plotting dot py.
We first type from numpy import * and from matplotlib.pyplot import star.
from numpy import *
from matplotlib.pyplot import *Then we define, we initialize, x with zeros in a vector of length 101.
x=zeros([101])We copy equals zeros of 101.
Because we want to initialize y1 with the same vector filled with zeros and the same length.
And y2 the same way.
y1=zeros([101])
y2=zeros([101])
Cosine and Sinus