Question

In: Computer Science

Using a python program find the numerical derivative at the indicated point, using the backward, forward,...

Using a python program find the numerical derivative at the indicated point, using the backward, forward, and centered formula. Use ℎ=0.05h=0.05, then each case compute the error. Which one is more accurate?

  1. ??ex at ?=0x=0.
  2. tan−1(?2−?+1)tan−1⁡(x2−x+1) at ?=1x=1.
  3. tan−1(100?2−199?+100)tan−1⁡(100x2−199x+100) at ?=1x=1.

Using a python program find the Monte Carlo integration, compute the value of π.

Solutions

Expert Solution

I hope this solves your issue. If you have any doubt or anything related to it, do comment down, I'll revert and will solve your doubts. Do drop a "LIKE" for the efforts

# this is the template of our weight function g(x)

def g_of_x(x, A, lamda):

e = 2.71828

return A*math.pow(e, -1*lamda*x)

def inverse_G_of_r(r, lamda):

return (-1 * math.log(float(r)))/lamda

def get_IS_variance(lamda, num_samples):

"""

This function calculates the variance if a Monte Carlo

using importance sampling.

Args:

- lamda (float) : lamdba value of g(x) being tested

Return:

- Variance

"""

A = lamda

int_max = 5

  

# get sum of squares

running_total = 0

for i in range(num_samples):

x = get_rand_number(0, int_max)

running_total += (f_of_x(x)/g_of_x(x, A, lamda))**2

  

sum_of_sqs = running_total / num_samples

  

# get squared average

running_total = 0

for i in range(num_samples):

x = get_rand_number(0, int_max)

running_total += f_of_x(x)/g_of_x(x, A, lamda)

sq_ave = (running_total/num_samples)**2

  

  

return sum_of_sqs - sq_ave

# get variance as a function of lambda by testing many

# different lambdas

test_lamdas = [i*0.05 for i in range(1, 61)]

variances = []

for i, lamda in enumerate(test_lamdas):

print(f"lambda {i+1}/{len(test_lamdas)}: {lamda}")

A = lamda

variances.append(get_IS_variance(lamda, 10000))

clear_output(wait=True)

  

optimal_lamda = test_lamdas[np.argmin(np.asarray(variances))]

IS_variance = variances[np.argmin(np.asarray(variances))]

print(f"Optimal Lambda: {optimal_lamda}")

print(f"Optimal Variance: {IS_variance}")

print(f"Error: {(IS_variance/10000)**0.5}")


Related Solutions

Find the directional derivative of f at the given point in the direction indicated by the...
Find the directional derivative of f at the given point in the direction indicated by the angle θ. f(x, y) = y cos(xy),    (0, 1),    θ = π/4 Duf(0, 1) =
Plot the estimate of the derivative dy"dx from the following data. Do this by using forward, backward, and central differences. Compare the results.
Plot the estimate of the derivative dy"dx from the following data. Do this by using forward, backward, and central differences. Compare the results.
What's the difference between numerical method to calculate differential (backward, central, forward)? At what condition do...
What's the difference between numerical method to calculate differential (backward, central, forward)? At what condition do I have to use forward over central and backward, etc? If there's boundary condition that permits the complete use of central method, what will happen if I still use forward, backward, and central regardless there's boundary condition that permits the use of central method? note: I'm not used to describe pure mathematical problem in english, so I'm sorry if my question is hard to...
A.) Find the partial derivative indicated. Assume the variables are restricted to a domain on which...
A.) Find the partial derivative indicated. Assume the variables are restricted to a domain on which the function is defined. z=sin(7x^3 y−3xy^4) B.)Compute the partial derivative: f(x,y)=sin(x^3−4y) fy(0,π)= C.) The equation r=5sinθ represents a circle. Find the cartesian coordinates of the center: x = ,y = , r = D.) Convert (xy)^3=8 to an equation in polar coordinates. E.)Find the slope of the tangent line to the polar curve r=sin(3θ) at θ=π/6 F.)Find the equation (in terms of x and...
A) Find the directional derivative of the function at the given point in the direction of...
A) Find the directional derivative of the function at the given point in the direction of vector v. f(x, y) = 5 + 6x√y, (5, 4), v = <8, -6> Duf(5, 4) = B) Find the directional derivative, Duf, of the function at the given point in the direction of vector v. f(x, y) =ln(x2+y2), (4, 5), v = <-5, 4> Duf(4, 5) = C) Find the maximum rate of change of f at the given point and the direction...
Find the directional derivative of the function at the given point in the direction of the...
Find the directional derivative of the function at the given point in the direction of the vector v. ? = (2? + 3?)2, ? ⃑ = 1 2 ? ⃑+ √3 2 ? ⃑, (-1,1,1)
Find maximum, minimum and saddle point by using the second derivative test: Show all the work...
Find maximum, minimum and saddle point by using the second derivative test: Show all the work f(x, y) =8xy-x^4-y^4
What kind of forward and backward linkages would each of the following publicly funded program might...
What kind of forward and backward linkages would each of the following publicly funded program might have? Comment on the number, strength, and intrinsic profitability of the linkages. Do you recommend that this initiative should be funded by the government or should it be left to the private sector? a. Construction of a big hospital in a rural area where is no such hospital for 200 miles. b. Creation of a large public park that from purchasing private agricultural property...
Find some companies with the following strategies: Backward vertical integration, Forward vertical integration, Horizontal integration, Conglomerate...
Find some companies with the following strategies: Backward vertical integration, Forward vertical integration, Horizontal integration, Conglomerate merger
PYTHON 1. Write a program that takes the first derivative of f(x) = 3x2 + 20x...
PYTHON 1. Write a program that takes the first derivative of f(x) = 3x2 + 20x + 5 and prints the values of this derivative at x= -5; x= +5 and x = +10        2. Write a program that takes the 2nd derivative of x5 and prints its value when x = 10. 3.  Write a program that generates 5 random integers between 1 and 75 inclusive ad prints the 5 integers, one per line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT