Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.48 KB | None | 0 0
  1. require("ggplot2")
  2.  
  3. require("xtable")
  4.  
  5. require("gridExtra")
  6.  
  7.  
  8.  
  9. require("scales")
  10.  
  11. require("pracma")
  12.  
  13.  
  14.  
  15. require("readxl")
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. stdev <- function(x) {
  24.  
  25.   s = sqrt( sum( (x-mean(x) )^2 )/length(x) )
  26.  
  27.   return(s)
  28.  
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #indirizzo
  36.  
  37. #C:/Users/Guglielmo/Desktop/università/ottica laboratorio/esperienza 1
  38.  
  39.  
  40.  
  41. #STABILITÃ  DEL LASER
  42.  
  43.  
  44.  
  45. dati_rumore <- read_excel("C:/Users/Guglielmo/Desktop/università/ottica laboratorio/esperienza 1/01b_Dati_Rumore_di_fondo.xlsx")
  46.  
  47. dati_rumore <- as.matrix.data.frame(dati_rumore)
  48.  
  49.  
  50.  
  51. dati_fluttuazione <- read_excel("C:/Users/Guglielmo/Desktop/università/ottica laboratorio/esperienza 1/01_Dati_Fluttuazioni_laser.xlsx")
  52.  
  53. dati_fluttuazione <- as.matrix.data.frame(dati_fluttuazione)
  54.  
  55.  
  56.  
  57. tempo_fluttuazione <- c(dati_fluttuazione[,1])
  58.  
  59. fluttuazione <- c(dati_fluttuazione[,2])
  60.  
  61. tempo_fluttuazione=tempo_fluttuazione[-c(1,2)] #tolgo i primi due elementi del vettore ridefinendolo
  62.  
  63. tempo_fluttuazione=as.numeric(tempo_fluttuazione) #conversione char --> numero
  64.  
  65. fluttuazione = fluttuazione[-c(1,2)]
  66.  
  67. fluttuazione = as.numeric(fluttuazione) #stessa cosa
  68.  
  69.  
  70.  
  71. #fare grafico
  72.  
  73. plot(tempo_fluttuazione, fluttuazione,
  74.  
  75.      cex = 0.5,
  76.  
  77.      main = "Fluttuazioni nel tempo")
  78.  
  79.  
  80.  
  81. #istogramma
  82.  
  83. hist(fluttuazione, xlim=a, main="Istogramma delle fluttuazioni", xlab="fluttuazione [mV]")
  84.  
  85.  
  86.  
  87. #dati sul rumore giorno/notte
  88.  
  89. tempo_rumore <- c(dati_rumore [,1])
  90.  
  91. rumore_accese  <- c(dati_rumore [,2])
  92.  
  93. rumore_spente  <- c(dati_rumore [,3])
  94.  
  95. tempo_rumore=tempo_rumore[-c(1,2)] #tolgo i primi due elementi del vettore
  96.  
  97. tempo_rumore=as.numeric(tempo_rumore) #cambio tipo char --> numero
  98.  
  99. rumore_accese=rumore_accese[-c(1,2)]
  100.  
  101. rumore_accese=as.numeric(rumore_accese)
  102.  
  103. rumore_spente=rumore_spente[-c(1,2)]
  104.  
  105. rumore_spente=as.numeric(rumore_spente)
  106.  
  107.  
  108.  
  109.  
  110.  
  111. #grafici
  112.  
  113. plot(tempo_rumore, rumore_accese,
  114.  
  115.      cex = 0.5, col = "red",
  116.  
  117.      main = "Rumore nel tempo")
  118.  
  119. plot(tempo_rumore, rumore_spente,
  120.  
  121.      cex = 0.5, col = "blue",
  122.  
  123.      main = "Rumore nel tempo")
  124.  
  125.  
  126.  
  127. #istogramma
  128.  
  129. hist(rumore_accese, main="Istogramma rumore luci accese", xlab="fluttuazioni [mV]")
  130.  
  131. hist(rumore_spente, main="Istogramma rumore luci spente", xlab="fluttuazione [mV]")
  132.  
  133.  
  134.  
  135.  
  136.  
  137. #LEGGE DI MALUS
  138.  
  139.  
  140.  
  141.  
  142.  
  143. pol2 <- read_excel("C:/Users/Guglielmo/Desktop/università/ottica laboratorio/esperienza 1/02_2Polaroid_cifre.xlsx")
  144.  
  145. pol2 <- as.matrix.data.frame(pol2)
  146.  
  147.  
  148.  
  149. #qui i dati sono già nuemeri double senza bisogno di conversioni
  150.  
  151. #inoltre i vettori sono già della corretta lunghezza
  152.  
  153. I <- 1/6 *(pol2[,2] + pol2[,3] + pol2[,4] + pol2[,5] + pol2[,6] + pol2[,7])
  154.  
  155.  
  156.  
  157. incertezzaI <- c(1:length(pol2[,1]))
  158.  
  159.  
  160.  
  161. for(i in 1:length(pol2[,1])){
  162.  
  163.   I
  164.  
  165.   incertezzaI[i] <- sqrt(   ( (pol2[i,2] - I[i])^2 + (pol2[i,3] - I[i])^2 +
  166.  
  167.                               (pol2[i,4] - I[i])^2 + (pol2[i,5] - I[i])^2 +
  168.  
  169.                               (pol2[i,6] - I[i])^2 + (pol2[i,7] - I[i])^2  ) /6   )
  170.  
  171. }
  172.  
  173.  
  174.  
  175. #sommo in quadratura incertezza con quella strumentale (ultimo digit)
  176.  
  177. for(i in 1:length(incertezzaI)){
  178.  
  179.   incertezzaI[i]=sqrt((incertezzaI[i])^2+(0.001)^2 + (0.02*I[i])^2)
  180.  
  181. }
  182.  
  183.  
  184.  
  185. #aggiungo l'offset
  186.  
  187. for(i in 1:length(incertezzaI)){
  188.  
  189.   I[i]=I[i]-0.032
  190.  
  191.   incertezzaI[i]=sqrt((incertezzaI[i])^2+(0.002)^2)#ultimo pezzo da togliere
  192.  
  193.  
  194.  
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205. Malus <- function(x, I0, phi, c){
  206.  
  207.   x <- x*pi/180 #così ho l'angolo in radianti
  208.  
  209.   I = I0 * cos(x+phi)^2 + c
  210.  
  211.   return(I)
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. pesi=1/(incertezzaI^2)
  220.  
  221. d <- data.frame(x=pol2[,1], y=I)
  222.  
  223. fit <- nls(y ~ Malus(x, I0, phi, c), data = d,
  224.  
  225.            start = list(I0 = 5, phi=pi/2, c=0), weights = pesi)  #valori più o meno vicini a quelli giusti
  226.  
  227. summary(fit)
  228.  
  229.  
  230.  
  231. I0=
  232.  
  233. phi=
  234.  
  235. c=
  236.  
  237.  
  238.  
  239. plot(pol2[,1], I,
  240.  
  241.      xlab = expression(theta), ylab = "Intensità  [V]",
  242.  
  243.      main = "Legge di Malus con 2 Polaroid",
  244.  
  245.      pch = 16,
  246.  
  247.      cex = 0.6
  248.  
  249.      )
  250.  
  251. segments(pol2[,1], I+incertezzaI, pol2[,1], I-incertezzaI)
  252.  
  253. arrows(pol2[,1], I+incertezzaI, pol2[,1], I-incertezzaI, length=0.02, angle = 90, code = 3 )
  254.  
  255. curve(Malus(x, I0, phi,c), add=TRUE, col ='blue')
  256.  
  257.  
  258.  
  259.  
  260.  
  261. pol3 <- read_excel("C:/Users/Guglielmo/Desktop/università/ottica laboratorio/esperienza 1/03_3Polaroid_cifre.xlsx")
  262.  
  263. pol3 <- as.matrix.data.frame(pol3)
  264.  
  265.  
  266.  
  267.  
  268.  
  269. x=pol2[,1]
  270.  
  271. #chi2
  272.  
  273. chi2=0
  274.  
  275. for(i in 1:length(I)){
  276.  
  277.   chi2=chi2+(((I[i]-Malus(x,I0,phi,c)[i])^2)/(incertezzaI[i]^2))
  278.  
  279.  
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293. #ricalcolo incertezze sulle x nel fit (metodo iterativo)-----------
  294.  
  295.  
  296.  
  297. x=pol2[,1]
  298.  
  299. sigmax=c(1:length(x))
  300.  
  301. for(i in 1:length(x)){
  302.  
  303.   sigmax[i]=1   #ci vorrebbe /sqrt(3)
  304.  
  305. }
  306.  
  307.  
  308.  
  309.  
  310.  
  311. cor_sigma=I0*(pi/180)*2*cos((pi/180)*x+phi)*sin((pi/180)*x+phi)*sigmax
  312.  
  313. incertezzaI_nuova=sqrt((incertezzaI)^2+(cor_sigma)^2)
  314.  
  315.  
  316.  
  317. #rifaccio fit
  318.  
  319.  
  320.  
  321.  
  322.  
  323. pesi=1/(incertezzaI_nuova^2)
  324.  
  325. d <- data.frame(x=pol2[,1], y=I)
  326.  
  327. fit <- nls(y ~ Malus(x, I0, phi,c), data = d,
  328.  
  329.            start = list(I0 = 5, phi=pi/2,c=0), weights = pesi)  #valori più o meno vicini a quelli giusti
  330.  
  331. summary(fit)
  332.  
  333.  
  334.  
  335. I0=
  336.  
  337. phi=
  338.  
  339. c=
  340.  
  341.  
  342.  
  343. plot(pol2[,1], I,
  344.  
  345.      xlab = expression(theta), ylab = "Intensità [V]",
  346.  
  347.      main = "Legge di Malus con 2 Polaroid",
  348.  
  349.      pch = 16,
  350.  
  351.      cex = 0.6
  352.  
  353. )
  354.  
  355. segments(pol2[,1], I+incertezzaI_nuova, pol2[,1], I-incertezzaI_nuova)
  356.  
  357. arrows(pol2[,1], I+incertezzaI_nuova, pol2[,1], I-incertezzaI_nuova, length=0.02, angle = 90, code = 3 )
  358.  
  359. curve(Malus(x, I0, phi,c), add=TRUE, col ='blue')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement