Question
1. Write a ~ython function that implements or calculates the following mathematical function for a single input value: A few hints: f (x) = {x2
1. Write a ~ython function that implements or calculates the following mathematical function for a single input value: A few hints: f (x) = {x2 sin (x), x < O x2 ex, x 0 In Python, ex is implemented using either math.exp() or numpy.exp() In Python, sin (x) is implement using either math.sin() or numpy.sin() You will need to use a conditional, e.g., if x 2. Write a Python function called vecfun() that implements or calculates the same mathematical function as problem 1 but now for a single input vector. Test the function by generating a vector of values between -2 and 2 using: myx = numpy.linspace(-2,2) and then plot the function using matplotlib.pyplot.plot(myx, vecfun(myx)). This function is more difficult than problem 1, so here are some hints and suggestions for the code inside of the function: After the first line of the function, which should be def vecfun(x):, you will need to allocate space for storing the vector of function values at each x. I did this by creating a vector of zeros ... f=numpy.zeros(x.shape) with the same shape as the vector of x values. The result is a vector of zeros the same length/shape as x. Next we need to loop through every value in the vector x. I used a 'for' loop. Be sure to access the individuals values inside the vector x using indices, i.e., x[i], and store individual values inside the vector fusing indices, f[i].
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