Question: I have a script in python 2.7.12 that is incorrect. What I need is to be able to plot the sum over i of a
I have a script in python 2.7.12 that is incorrect. What I need is to be able to plot the sum over i of a function that depends on j, where I can input values of ai. What I have is a script that evaluates a function at each individual point and plots it, but does not sum over i.Can someone help me fix this? Here is the script along with the actual equation
where ai goes from a0,a1,...,a_(m-1), and in our case m is 3, and j is a natural number. This should look like a combination of cos waves, fourier-like, but when I use values for a0,a1,a2 = .333, .444,.222 respectively, it looks like just 3 points connected by straight lines.
# Plotting a graph import matplotlib.pyplot as plt import numpy as np
m = 3 #definig m = 3 as given j = np.arange(1, m+1) # Giving j range from 1 to m+1 equally a = [] # list to store a0,a1,a2..a10 valuess result = [] # list to store result of cos funcion result sum=0 # variable to sum the value for i in range(0,m): # Getting a0..a10 values from input ai = input("Enter a" + str(i) + " value :-\t") a.append(ai)
for i in range (0,m): # Getting result value value = a[i] * np.cos(2*np.pi*i*j[i]/m) sum=sum+a[i]; # code to compute the sum result.append(value) print('Sum value is',sum)
plt.plot(j, result) # plotting j and f(j) plt.xlabel('j') plt.ylabel('f(j)') plt.title('Graph') plt.grid(True) #plt.savefig("result.png") plt.show() # showing plot
m-1 ai cos(2Tij/m) 1-0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
