Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Array assignments in a for loop
#1
I am having difficulty trying to understand why two different expression in a for loop are producing the same result. In this case, I am doing numeric integration and differentiation as an exercise to learn some Python basics. But I am surprised to find that the produce the same values for different explicit assignments for y_int and yder. Inserted below is the Python code.

import numpy as np
import matplotlib.pyplot as plt

M = 101
x = np.linspace(-10, 10, num=M)
y = np.exp(-x**2)
y_int = np.zeros(M)
yder = y_int
plt.plot(x, y)
plt.show()
xs = range(x.size-1)
sm = 0
i = 0
for i in xs:
    sm = sm + 0.5*(y[i]+y[i+1])*(x[i+1]-x[i])
    y_int[i] = sm
    yder[i] = (y[i+1]-y[i])/(x[i+1]-x[i])
    
plt.plot(x[1:x.size-1], y_int[1:x.size-1])
plt.show()
    
for i in xs:
    yder[i] = (y[i+1]-y[i])/(x[i+1]-x[i])
    
print(sm)


plt.plot(x, yder)
plt.show()

del y_int
del yder
Quote
#2
How does line 17 is different from line 23?

17.     yder[i] = (y[i+1]-y[i])/(x[i+1]-x[i])
23.     yder[i] = (y[i+1]-y[i])/(x[i+1]-x[i])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
I think OP means that yder and y_int give the same graph.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)