 CoCalc Public Filesatms391geodata / Week 1 / Homework 1: Basic python notebook (answer key).ipynb
Author: Steve Nesbitt
Views : 227
Compute Environment: Ubuntu 18.04 (Deprecated)

# ATMS 391: Geophysical data analysis

Homework 1: Basic usage of python and iPython notebook

# Instructions

You will use this notebook to provide your answers to this assignment

## Problem 1

Write a function to solve the definite integral $\int_0^1 ax^3 + 3 dx$ The following will be very useful: Scipy documentation.
What is the result of the integral with defined values of $a$ being $a=2$, $a=3$, and $a=4$?
Your function must be set up to run with only one input $a$, which is passed into the function as the first argument. Print your answers for each value of $a$ in your notebook that you turn in.

In :
from scipy import integrate

def integral(a):
func = lambda x : a * x**3 + 3
print("a = %d, result = %f" %(a, result))

for a in [2,3,4]:
integral(a)

a = 2, result = 3.500000 a = 3, result = 3.750000 a = 4, result = 4.000000

## Problem 1 Rubrics:

Write the correct function, print the results, results are correct [10 points]

Don't write the correct function [-6 points] (will get partial credit if function is partial correct)

Don't print out the results [-1 point]

A few notes:

1. In the function, remember to put the return statement at the end. If you put a print statement after return statement, the function will not execute the print statement.

2. As stated in the instruction, your function must be set up to run with only one input $a$. Violating this will result in losing points.

3. Calculting the integral using analytical solution other than using scipy also gets full mark.

## Problem 2

The angular diameter (in degrees, $\theta$) subtended by an object $x$ in the sky is given by $\theta=2\sin^{-1}\frac{R_x}{D_x}$ where ${R_x}$ is the radius of the object and ${D_x}$ is the distance to the object. The units of ${R_x}$ and ${D_x}$ must be the same.

(a) Create a python function to perform this calculation.

In :
import numpy as np

return 2 * np.degrees(np.arcsin(radius / distance))


(b) Execute this function for the moon (${D_m = 3.84 \times 10^{5} }$ km, ${R_m = 1.74 \times 10^3}$ km) and sun (${D_s = 1.496 \times 10^{8}}$ km. ${R_s = 6.96 \times 10^{5}}$ km) using the function you wrote. Store the results into a variable for each calculation, and print the result.

In :
Dm, Rm = 3.84e5, 1.74e3
Ds, Rs = 1.496e8, 6.96e5

theta_m = angular_diameter(Rm, Dm)
theta_s = angular_diameter(Rs, Ds)
print("Angular diameter for the moon is: %f" % theta_m)
print("Angular diameter for the sun is: %f" % theta_s)

Angular diameter for the moon is: 0.519245 Angular diameter for the sun is: 0.533128

(c) Calculate the ratio of the moon to the sun apparent angular diameter with the results of (b) stored in memory (i.e. not typed in!!). Print the result.

In :
ratio = theta_m / theta_s
print("The ratio of the moon to the sun angular diameter is: %f" % ratio)

The ratio of the moon to the sun angular diameter is: 0.973958

(d) Does this answer explain why we have solar eclipses? Why or why not? Answer in a markdown cell.

Yes. The ratio is nearly 1, which means during solar exlipses the moon can almost cover the sun from the view on the earth.

## Problem 2 Rubrics:

(a) Correctly create the function [3 points]

(b) Correct answer, store the results to variables [3 points] (both degrees and radians are okay)