-1
$\begingroup$

I am an amature in python, I wrote a simple code in jupyter. But it is giving an error. I want to plot a function:

import numpy as np
import math as m
#from scipy import random
import matplotlib.pyplot as plt

y1=np.zeros(10)      
y2=np.zeros(360)   
x=np.array(range(10))
print("a")

    x=np.linspace(0,100,1)
    d=1.0
    y=np.linspace(0,100,1)
    sigma=np.linspace(0,100,1)
    for i in range(0,100,1):
        y[i]= x[i]**2+d**2`

However I am getting an output with error: 1.0


IndexError Traceback (most recent call last) in () 4 sigma=np.linspace(0,100,1) 5 for i in range(0,100,1): ----> 6 y[i]= x[i]2+d2 7 print(y[i]) 8 # sigma[i]=1/(y[i])

IndexError: index 1 is out of bounds for axis 0 with size 1

1.0


IndexError Traceback (most recent call last) in () 4 sigma=np.linspace(0,100,1) 5 for i in range(0,100,1): ----> 6 y[i]= x[i]2+d2 7 print(y[i]) 8 # sigma[i]=1/(y[i])

IndexError: index 1 is out of bounds for axis 0 with size 1 1.0

IndexError Traceback (most recent call last) in () 4 sigma=np.linspace(0,100,1) 5 for i in range(0,100,1): ----> 6 y[i]= x[i]2+d2 7 print(y[i]) 8 # sigma[i]=1/(y[i])

Help me find the problem please

$\endgroup$

1 Answer 1

2
$\begingroup$

The error message is quite explanatory. Notice that

x=np.linspace(0,100,1) 

is [0.]

Probably you wanted to write: x=np.linspace(0,1,100). In the documentation https://numpy.org/doc/stable/reference/generated/numpy.linspace.html you can observe that the first two parameters are start and stop, while you probably wrote the number of samples num in place of stop

Same story for y

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.