Question
Scipy: We have the min and max temperatures in a city In India for each months of the year. We would like to find a
Scipy:
We have the min and max temperatures in a city In India for each months of the year. We would like to find a function to describe this and show it graphically, the dataset given below.
Task is
1. fitting it to the periodic function
2. plot the fit
Data
Max = 39, 41, 43, 47, 49, 51, 45, 38, 37, 29, 27, 25
Min = 21, 23, 27, 28, 32, 35, 31, 28, 21, 19, 17, 18
Solution:Max = 39, 41, 43, 47, 49, 51, 45, 38, 37, 29, 27, 25
Min = 21, 23, 27, 28, 32, 35, 31, 28, 21, 19, 17, 18
months = np.arange(12)
from scipy import optimize
def func(times, avg, ampl, time_offset):
return (avg
+ ampl * np.cos((times + time_offset) * 2 * np.pi / times.max()))
res_max, cov_max = optimize.curve_fit(func, months,
Max, [20, 10, 0])
res_min, cov_min = optimize.curve_fit(func, months,
Min, [-40, 20, 0])
days = np.linspace(0, 12, num=365)
plt.figure()
plt.plot(months,Max, 'ro')
plt.plot(days, func(days, *res_max), 'r-')
plt.plot(months, Min, 'bo')
plt.plot(days, func(days, *res_min), 'b-')
plt.xlabel('Months')
plt.ylabel('Temperature')
plt.show()
My question is shall u explain the solution code??
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started