Bottom Page

• 1 Vote(s) - 2 Average
• 1
• 2
• 3
• 4
• 5
 Array assignments in a for loop mt_reilly Unladen Swallow Posts: 2 Threads: 2 Joined: Nov 2018 Reputation: 0 Likes received: 0 #1 Nov-23-2018, 03:51 PM 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 wavic So-and-so of the Yard    Posts: 2,870 Threads: 37 Joined: Sep 2016 Reputation: 89 Likes received: 358 #2 Nov-23-2018, 04:39 PM 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 ichabod801 Bunny Rabbit Posts: 3,837 Threads: 83 Joined: Sep 2016 Reputation: 231 Likes received: 1163 #3 Nov-23-2018, 05:08 PM 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 « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)