Question 1 (a) Use the provided python code plot-exp-Canon.py to write a program to numerically compute the trajectory of a ball with initial speed 700m/s at an angle of 25 degrees. Use n=10(the n value changes the the time step) and plot also on the same axes the exact y solution. (0) Tabulate the numerical values of x and y, exact value of y, relative error of your solutions at each time step similar to: X-numerical y-numerical y-exact relative percentage error Question 2. (a) repeat question 1(a) but make n = 30 (b) repeat question 1(b) but with n = 30. Discuss differences in the percentage error with 1(b) and make conclusions.
#!/usr/bin/env python ####### #### ##### ################################# import matplotlib.pyplot as plt import numpy as np from numpy import linspace, sin, exp, pi, cos, sqrt from matplotlib.ticker import MultipleLocator import sys import pylab ############# ### #################### #### ###### # studnum = "????" name = "????" group = "3" day = "Monday" date = "2022/05/??" expname = "Motion without air resistance" filename = "Projectile motion with exact solution" #name for your saved plot file, will save as PNG new = open ("Trajectory_Canon.txt", "W") #new1 = open("Nuclei_Versus_time_dt0.2.txt", "W") #new2 = open("Nuclei_Versus_time_dt0.5.txt", #for i in range(len (vall)): #if vall >= 75: new.write("%6.1f \n" % (vall)) #new.close() # The axis label have basic latex command examples for greek letters, subscript, # superscript (exponents). They are enclosed in between $...$. Use if you need. # some greek letters require two \ infront of them, some don't, try out. xlabel = "$x$ (km)" ylabel "$y$ (km)" title "Figure 2: " + expname aratio = 210/297. #aspect ratio of your canvas ######## ##### ################### ######### ### ###### n=10
b=110 a=0 dt=(b-a)/n t = np.linspace(a, dt, n) #gives the time t=(i-1)*dt X = np.linspace(0, 0, n) vx = np.linspace(0, 0, n) 1 y = np.linspace(0, 0, n) # fill it up with zeros vy = np.linspace(0, 0, n) #Initializing velocities for theta = 45 deg vx[0]=700*cos(45*pi/180) vy[@]=700*sin(45*pi/180) # THE MAIN CALCULATION IS HERE # Calculate the x[i+1] and v[i+1] after initializing x and v for i in range (len(x)-1): vx[i+1] = vx x[i+1)=x+vx*dt vy[i+1]=vy-(9.8*dt) y[i+1]=y+vy*dt if len(x) != len(y): print("Error, X and Y arrays not of the same size") sys.exit() # Set your minimum and maximum x-values on the graph xmin = 0.0 xmax = 55.0 # Set your minimum and maximum x-values on the graph
xmin = 0.0 xmax = 55.0 # Set your minimum and maximum x-values on the graph ymin = 0.0 ymax = 2.0 = fig = plt. figure(figsize=(6,8*aratio)) #Figure canvas size. 10 x ?? is good fig.set_tight_layout(True) #Useful to get rid of extra white space borders ax = fig.add_subplot(1,1,1) ax.plot(x/1000, y/1000, color='0.2', linewidth=2.0, label='Numerical') ax.legend() ax.plot(x/1000, (x-4.9*(x*x)/245000)/1000, marker='0', markersize=3, markerfacecolor= 'white', color='b', linestyle="", label='Exact') #Exact solution ax.legend() ax.set_xlim( xmin, xmax) ax.set_ylim(ymin, ymax) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_title(title) plt. savefig(filename + '.png') plt.close(fig)
Question 1 (a) Use the provided python code plot-exp-Canon.py to write a program to numerically compute the trajectory o
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Question 1 (a) Use the provided python code plot-exp-Canon.py to write a program to numerically compute the trajectory o
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!