-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4task.R
53 lines (40 loc) · 1.62 KB
/
4task.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#################################################################
# This task is to calculate trajectory
# With enviroment resistance
#################################################################
g <- 9.81 #gravity acceleration
v0 <- 11.1
alpha <- pi/4
k <- 0.3 #enviroment resistance coefficient
m <- 2 #mass in kilos
x <- vector(length=50) #horizontal axes
y <- vector(length=50) #vertical axes
plot(x, y, col="blue", ylim=c(0, 13), xlim=c(0, 13))
#currentParameters = paste("v0=", v0, "m/s, angle=", alpha, "radians, m=", m, "kg, k=", k)
#title(main="", col.main="red", font.main=4)
legend(1, 13, c(paste("v0 = ", v0, " m/s"), "angle = pi/4", paste("m = ", m, " kg"), paste("k = ", k)), cex=0.8)
v_x <- vector(length=50) #projections on x
v_y <- vector(length=50) #projections on y
v_x[1] <- v0*cos(alpha)
v_y[1] <- v0*sin(alpha)
wholeTime <- 2*v0*sin(alpha)/g #whole time of flight
dt <- wholeTime/50 #step
h <- 0.05
x[1] <- 0
y[1] <- 0
for(cnt in seq(2, 51, 1))
{
#f_v1_x <- (-k * v_x[cnt-1] * v_x[cnt-1] * dt / m)
#f_v1_y <- (-(g + k * v_y[cnt-1] * v_y[cnt-1] / m) * dt)
#v_x_tmp <- (v_x[cnt-1] + h * f_v1_x)
#v_y_tmp <- (v_y[cnt-1] + h * f_v1_y)
#f_x_tmp <- (-k * v_x_tmp * v_x_tmp * dt / m)
#f_y_tmp <- (-(g + k * v_y_tmp * v_y_tmp / m) * dt)
#v_x[cnt] <- (v_x[cnt-1] + h * (f_v1_x + f_x_tmp) / 2)
#v_y[cnt] <- (v_y[cnt-1] + h * (f_v1_y + f_y_tmp) / 2)
v_x[cnt] <- (v_x[cnt-1] - k * v_x[cnt-1] * v_x[cnt-1] * dt / m)
v_y[cnt] <- (v_y[cnt-1] - (g + k * v_y[cnt-1] * v_y[cnt-1] / m) * dt)
x[cnt] <- (x[cnt-1] + v_x[cnt-1]*dt)
y[cnt] <- (y[cnt-1] + v_y[cnt-1]*dt)
}
lines(x, y, type="o", pch=20, lty=1, col="blue")