library("readODS")
## Warning: package 'readODS' was built under R version 4.4.2
file_path <- "C:/Studieren/Physics/phy_exp_1.ods"
dist_vector <- c(0.5,0.9,1.3,1.7)


df <- read_ods(file_path)
## New names:
## • `` -> `...1`
## • `` -> `...2`
mean_all_1 <-mean(df$`L1=0.5 m`, na.rm = TRUE)
mean_all_2 <-mean(df$`L2=0.9 m`, na.rm = TRUE)
mean_all_3 <- mean(df$`L3=1.3 m`, na.rm = TRUE)
mean_all_4 <- mean(df$`L4 = 1.7 m`, na.rm = TRUE)

time_all<-c(mean_all_1,mean_all_2,mean_all_3,mean_all_4)


head(df)
## # A tibble: 6 × 7
##   ...1             ...2  intervals `L1=0.5 m` `L2=0.9 m` `L3=1.3 m` `L4 = 1.7 m`
##   <chr>            <chr> <lgl>          <dbl>      <dbl>      <dbl>        <dbl>
## 1 initial, alpha=… ex.1  NA             0.298      0.594      0.838         1.03
## 2 <NA>             ex.2  NA             0.29       0.58       0.819         1.02
## 3 <NA>             ex.3  NA             0.289      0.582      0.825         1.02
## 4 <NA>             ex.4  NA             0.288      0.582      0.824         1.02
## 5 20g added        ex.5  NA             0.288      0.582      0.824         1.02
## 6 <NA>             ex.6  NA             0.299      0.597      0.841         1.04
plot(dist_vector, time_all, type = "b", col = "blue", pch = 19, xlab = "Path, meters", ylab = "Time, seconds", main = "s(t) function")
points(dist_vector, time_all, col = "red", pch = 19)  # Add red dots over the blue line
grid()  

diff_all <-c(mean_all_1,mean_all_2-mean_all_1,mean_all_3-mean_all_2,mean_all_4-mean_all_3)
length = 0.125




V_i <- length / diff_all
t_i_prime <- time_all + (diff_all / 2)



library(ggplot2)

plot(t_i_prime, V_i, type = "b", col = "blue", pch = 19, xlab = "Time", ylab = "Velocity", main = "v(t) function")
points(t_i_prime, V_i, col = "red", pch = 19)  
grid()  

ex_data <- data.frame(
  time = c(0.298,   0.594,  0.838,  1.035,
0.290,  0.580,  0.819,  1.019,
0.289,  0.582,  0.825,  1.021,
0.288,  0.582,  0.824,  1.020,
0.288   ,0.582, 0.824,  1.020,
0.299,  0.597,  0.841,  1.038,
0.302,  0.602,  0.847,  1.045,
0.303,  0.603,  0.848,  1.046,
0.336,  0.652,  0.905,  1.107,
0.332,  0.669,  0.926,  1.127,
0.323,  0.665,  0.921,  1.121,
0.336,  0.661,  0.916,  1.116,
0.287,  0.581,  0.815,  1.010,
0.283,  0.574,0.806,    1.000,
0.285,  0.577,  0.810,  1.004,
0.286,  0.581,  0.813,  1.008,
0.293,  0.592,  0.826,  1.021,
0.291,  0.589,  0.834,  1.019,
0.299,  0.599,  0.835,  1.031,
0.295,  0.593,  0.829,  1.025,
0.328,  0.640,  0.882,  1.081,
0.334,  0.647,  0.890,  1.090,
0.326,  0.635,  0.876,  1.075,
0.335,  0.648,  0.89,   1.091),
  displacement =rep(c(0.5,0.9,1.3,1.7), length.out = 96),
  experiment_id = rep(1:24, each = 4)
)

head(ex_data)
##    time displacement experiment_id
## 1 0.298          0.5             1
## 2 0.594          0.9             1
## 3 0.838          1.3             1
## 4 1.035          1.7             1
## 5 0.290          0.5             2
## 6 0.580          0.9             2
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
results <- ex_data %>%
  group_by(experiment_id) %>%
  do({

    model <- lm(displacement ~ poly(time, 2), data = .)
    

    coefficients <- coef(model)
    a <- coefficients[3]  
    b <- coefficients[2]  
    c <- coefficients[1]  
    

    velocity_model <- function(t) { 2 * a * t + b }
    acceleration <- 2 * a
    

    tibble(a = a, b = b, c = c, velocity_model = velocity_model(0), acceleration = acceleration)
  })


print(results)
## # A tibble: 24 × 6
## # Groups:   experiment_id [24]
##    experiment_id      a     b     c velocity_model acceleration
##            <int>  <dbl> <dbl> <dbl>          <dbl>        <dbl>
##  1             1 0.0798 0.891   1.1          0.891        0.160
##  2             2 0.0739 0.891   1.1          0.891        0.148
##  3             3 0.0787 0.891   1.1          0.891        0.157
##  4             4 0.0796 0.891   1.1          0.891        0.159
##  5             5 0.0796 0.891   1.1          0.891        0.159
##  6             6 0.0813 0.891   1.1          0.891        0.163
##  7             7 0.0817 0.891   1.1          0.891        0.163
##  8             8 0.0817 0.891   1.1          0.891        0.163
##  9             9 0.0881 0.890   1.1          0.890        0.176
## 10            10 0.102  0.889   1.1          0.889        0.204
## # ℹ 14 more rows
mean_acc<-mean(results$acceleration)
mean_vel<-mean(results$velocity_model)

cat("initial velocity: ", mean_vel)
## initial velocity:  0.8902275
cat("Acceleration: ", mean_acc)
## Acceleration:  0.1718807
i=0.202
i1=0.202+0.02
i2=0.202+0.12

mass <- rep(NA, 48)
mass[1:16] <- i
mass[17:32] <- i1
mass[33:48] <- i2


displacement <- rep(c(0.5,0.9,1.3,1.7), length.out = 48)
time <- c(0.298,    0.594,  0.838,  1.035,
0.290,  0.580,  0.819,  1.019,
0.289,  0.582,  0.825,  1.021,
0.288,  0.582,  0.824,  1.020,
0.288   ,0.582, 0.824,  1.020,
0.299,  0.597,  0.841,  1.038,
0.302,  0.602,  0.847,  1.045,
0.303,  0.603,  0.848,  1.046,
0.336,  0.652,  0.905,  1.107,
0.332,  0.669,  0.926,  1.127,
0.323,  0.665,  0.921,  1.121,
0.336,  0.661,  0.916,  1.116)


acceleration <- 2 * displacement / time^2


data <- data.frame(Mass = mass, Displacement = displacement, Time = time, Acceleration = acceleration)





plot(data$Mass, data$Acceleration, 
     xlab = "Mass (kg)", ylab = "Acceleration (m/s)", 
     main = "Acceleration vs Mass angle = 6", 
     type = "n")  


model <- lm(Acceleration ~ Mass, data = data)
abline(model, col = "red")

i=0.202
i1=0.202+0.02
i2=0.202+0.12

mass <- rep(NA, 48)
mass[1:16] <- i
mass[17:32] <- i1
mass[33:48] <- i2


displacement <- rep(c(0.5,0.9,1.3,1.7), length.out = 48)
time <- c(0.287,    0.581,  0.815,  1.010,
0.283,  0.574,0.806,    1.000,
0.285,  0.577,  0.810,  1.004,
0.286,  0.581,  0.813,  1.008,
0.293,  0.592,  0.826,  1.021,
0.291,  0.589,  0.834,  1.019,
0.299,  0.599,  0.835,  1.031,
0.295,  0.593,  0.829,  1.025,
0.328,  0.640,  0.882,  1.081,
0.334,  0.647,  0.890,  1.090,
0.326,  0.635,  0.876,  1.075,
0.335,  0.648,  0.89,   1.091)


acceleration <- 2 * displacement / time^2


data_1 <- data.frame(Mass = mass, Displacement = displacement, Time = time, Acceleration = acceleration)





plot(data_1$Mass, data_1$Acceleration, 
     xlab = "Mass (kg)", ylab = "Acceleration (m/s)", 
     main = "Acceleration vs Mass angle = 10", 
     type = "n")  


model_1 <- lm(Acceleration ~ Mass, data = data_1)
abline(model_1, col = "red")

a=6
a1=10
angles<- rep(NA, 24) 
angles[1:12]<-a
angles[13:24] <-a1



accelerations<-results$acceleration


angles_rad <- angles * pi / 180


numerator <- sum(sin(angles_rad) * accelerations)
denominator <- sum(sin(angles_rad)^2)


g_estimated <- numerator / denominator
cat("Estimated gravity (g): ", g_estimated, "m/s^2\n")
## Estimated gravity (g):  9.899708 m/s^2