Question: FOR Python Programming. My code was this and I got half of it wrong. import numpy as np def tanc(x): if isinstance(x,np.ndarray): arr = np.zeros(len(x))

FOR Python Programming.

FOR Python Programming. My code was this and I got half ofit wrong. import numpy as np def tanc(x): if isinstance(x,np.ndarray): arr =

My code was this and I got half of it wrong.

import numpy as np def tanc(x): if isinstance(x,np.ndarray): arr = np.zeros(len(x)) for index,val in enumerate(x): if val==0: arr[index] = 1 else: arr[index]= np.tan(val)/val return arr else: if x==0: return 1 else: return np.tan(x)/x import numpy as np def theta(t, omega, theta0=np.pi/6, Omega0=0): if isinstance(t,np.ndarray): th = np.zeros(len(t))

fi = -np.arctan((Omega0/omega)/theta0)

A = np.sqrt(((theta0**2)+((Omega0/omega)**2)))

for index,val in enumerate(t): th[index] = A * np.cos(omega*t[0] + fi)

return th

else:

fi = -np.arctan((Omega0/omega)/theta0)

A = np.sqrt(((theta0**2)+((Omega0/omega)**2))) return A * np.cos(omega*t + fi)

18.3 Functions This lab will be available until February 14th, 11:59 PM MST Solve the following problems. Use numpy and numpy arrays when possible. tanc function Define the function tan tanca : 1 as a Python function tanc(x) 1. Note that the function value at x = 0 is well defined lim tancx = 1. 20 Your function should take this into account. 2. Your function should behave like a numpy ufunc, i.e., it should be possible to use a numpy array as an argument and then it should return an array of the same size in which tanc() was applied element-wise. For example: >>> X = np.pi*np.array([0, 0.25, 1]) >>> tanc (X) array([ 1.0, 1.27323954e+00, -3.89817183e-17]) Note: Getting (2) to work is much harder than (1). Try to get (1) to work first (the initial tests only test scalar arguments). Note: Getting (2) to work is much harder than (1). Try to get (1) to work first (the initial tests only test scalar arguments). Harmonic pendulum trajectory The solution o(t) = A cos(wt+) A= 02 + 120/ = - arctan 00 for the harmonic pendulum depends on the frequency w and the initial conditions 69 = e(0) and No = N(0) = (0). Write a function theta() that does the following: 1. takes an array t of times as mandatory argument; 2. takes the mandatory argument omega to set the frequency: 3. returns an array of the same size as t in which e(t) was evaluated for each element in t; 4. contains a doc string that briefly explains the function and the arguments that it takes; 5. takes an optional argument thetal with default value 1/6; 6. takes an optional argument OmegaO with default value 0. 282326.1742132 LAB ACTIVITY 18.3.1: Functions 0/30 main.py Load default template... 1 # add your code 2 # (only function definitions are tested) 18.3 Functions This lab will be available until February 14th, 11:59 PM MST Solve the following problems. Use numpy and numpy arrays when possible. tanc function Define the function tan tanca : 1 as a Python function tanc(x) 1. Note that the function value at x = 0 is well defined lim tancx = 1. 20 Your function should take this into account. 2. Your function should behave like a numpy ufunc, i.e., it should be possible to use a numpy array as an argument and then it should return an array of the same size in which tanc() was applied element-wise. For example: >>> X = np.pi*np.array([0, 0.25, 1]) >>> tanc (X) array([ 1.0, 1.27323954e+00, -3.89817183e-17]) Note: Getting (2) to work is much harder than (1). Try to get (1) to work first (the initial tests only test scalar arguments). Note: Getting (2) to work is much harder than (1). Try to get (1) to work first (the initial tests only test scalar arguments). Harmonic pendulum trajectory The solution o(t) = A cos(wt+) A= 02 + 120/ = - arctan 00 for the harmonic pendulum depends on the frequency w and the initial conditions 69 = e(0) and No = N(0) = (0). Write a function theta() that does the following: 1. takes an array t of times as mandatory argument; 2. takes the mandatory argument omega to set the frequency: 3. returns an array of the same size as t in which e(t) was evaluated for each element in t; 4. contains a doc string that briefly explains the function and the arguments that it takes; 5. takes an optional argument thetal with default value 1/6; 6. takes an optional argument OmegaO with default value 0. 282326.1742132 LAB ACTIVITY 18.3.1: Functions 0/30 main.py Load default template... 1 # add your code 2 # (only function definitions are tested)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!