Advertisement
Guest User

Lab 1

a guest
Jan 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.33 KB | None | 0 0
  1. function [sum] = vecsum(RV)
  2. %this function will input a row vector and return a sum of all the elements
  3. L = length(RV);
  4. i = 1;
  5. sum = 0;
  6. while i <= L
  7.     sum = sum + RV(i);
  8.     i = i + 1;
  9. end
  10. end
  11.  
  12. function [A,B,dA,dB] = wregression(x,y,dy)
  13. %this function calculates slope, y intercept and uncertainty in both
  14. %it also assumes that x, y and dy are of the same length
  15. w = 1./(dy.^2);
  16. %First we get the sums we will need to find our return values
  17. w_sum = vecsum(w);
  18. wy_sum = vecsum(w.*y);
  19. wx_sum = vecsum(w.*x);
  20. wxy_sum = vecsum(w.*x.*y);
  21. wx2_sum = vecsum(w.*(x.^2));
  22. %Now we find the value of D to be used later
  23. D = (w_sum*wx2_sum) - (wx_sum^2);
  24. %Here we use our sums along with D to find the return values
  25. A = ((wx2_sum*wy_sum)-(wx_sum*wxy_sum))/D;
  26. B = ((w_sum*wxy_sum)-(wx_sum*wy_sum))/D;
  27. dA = sqrt(wx2_sum/D);
  28. dB = sqrt(w_sum/D);
  29. end
  30.  
  31. function regressionplot(x,y,dy)
  32. hold on
  33. figure(1)
  34. errorbar(x,y,dy,'rx')
  35. xfit = linspace(0,5);
  36. [A,B,dA,dB] = wregression(x,y,dy);
  37. yfit = xfit*B + A;
  38. plot(xfit,yfit)
  39. end
  40.  
  41. function [z2] = zSq(V,I);
  42. z2 = (V./I).^2;
  43. end
  44.  
  45. function [R L dR dL] = indRes(f,V,I,dV,dI)
  46. f=[152 361 568 823 1031 1358]; % frequency [Hz];
  47. V=[1.63 2.01 2.47 3.00 3.34 3.75]; % inductor RMS voltage [V]
  48. I=[0.0615 0.0593 0.056 0.0512 0.0472 0.0413]; % inductor RMS current [A]
  49. dV=V*0.005+0.02; % Uncertainty in the voltage measurements [V]
  50. dI=I*0.0075+0.0002; % Uncertainty in the current measurements [A]
  51. radF = (2*f*pi).^2;
  52. z2 = zSq(V,I);
  53. dZSq=((zSq(V+dV,I)-zSq(V,I)).^2+(zSq(V,I+dI)-zSq(V,I)).^2).^0.5;
  54. [R2,L2,dR2,dL2] = wregression(radF,z2,dZSq);
  55. R = R2^0.5;
  56. L = L2^0.5;
  57. dR = abs((R2+dR2)^0.5 - (R2)^0.5);
  58. dL = abs((L2+dL2)^0.5 - (L2)^0.5);
  59. end
  60.  
  61. function [A B dA dB] = capa(f,V,I,dV,dI)
  62. f=[113 124 212 238 294 350 395 489]; % frequency [Hz]
  63. V=[7.39 7.47 7.46 7.35 7.36 7.35 7.44 7.43]; % Capacitor RMS Voltage [V]
  64. I=[2.51 2.73 4.82 5.41 6.61 7.81 8.91 11.2]*10^-3; % RMS Current [A]
  65. dV=V*0.005+0.02; % Experimental Uncertainty in Voltage Measurements [V]
  66. dI=I*0.0075+0.0002; % Uncertainty in Current [A]
  67. radF = (2*f*pi).^2;
  68. xvals = 1./radF
  69. z2 = zSq(V,I);
  70. dZSq=((zSq(V+dV,I)-zSq(V,I)).^2+(zSq(V,I+dI)-zSq(V,I)).^2).^0.5;
  71. [R2,cap2,dR2,dCap2] = wregression(xvals,z2,dZSq);
  72. R = R2^0.5;
  73. cap = cap2^-0.5;
  74. dR2 = abs((R2+dR2)^0.5 - (R2)^0.5);
  75. dCap2 = abs((1/(cap2+dCap2))^0.5 - (1/cap2)^0.5);
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement