Advertisement
hockeygoalie5

engr 1181 midterm 2 review

Nov 3rd, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.89 KB | None | 0 0
  1. review.m
  2.  
  3. clear;clc
  4. disp('ENGR 1181')
  5. disp('Midterm 2 Review')
  6. disp('----------------')
  7.  
  8. disp('Problem 1')
  9. x = 10;
  10. y = (log10(x) * log(x)) / exp(1.1); % log10 is log and log is ln
  11. fprintf('The answer is %.2f.\n', y)
  12. x = 45;
  13. y = (cosd(x) + sind(x)) / (x - 44); % cosd and sind for degrees
  14. fprintf('The answer is %.2f.\n', y)
  15. disp('---------')
  16.  
  17. % Part of problem 2 is in a seperate function file (problem2.m).
  18. disp('Problem 2')
  19. t = input('Input the values of t.\nt = ');
  20. k = input('Input the values of k.\nk = ');
  21. [y, df] = problem2(t, k);
  22. fprintf('y(t) = ')
  23. fprintf('%.2e ', y)
  24. fprintf('\ndf(k) = ')
  25. fprintf('%.2f ', df)
  26. fprintf('\n');
  27. disp('---------')
  28.  
  29. % Problem 3
  30. subplot(1, 2, 1)
  31. fplot('P + P^2 + 2', [-2 2])
  32. subplot(1, 2, 2)
  33. fplot('(P + 1) / (P - 1)', [-2 2])
  34. % ---------
  35.  
  36. % Problem 4
  37. fMeasured = [3.8 1.01 15.2 32.8 10.6];
  38. m = [1 0.25 4 9 3];
  39. a = 3.7;
  40. fCalculated = m .* a;
  41. figure
  42. hold on
  43. plot(m, fCalculated, 'r-')
  44. plot(m, fMeasured, 'bx')
  45. hold off
  46. title('Force v. Mass on Mars')
  47. xlabel('Mass (kg)')
  48. ylabel('Force (N)')
  49. legend('Calculated force', 'Measured force')
  50. % ---------
  51.  
  52. disp('Problem 5')
  53. x = 1:10;
  54. y = x .^ 2 + x;
  55. y2 = y .* x + x .^ 2 + y;
  56. y3 = y2 .* (y ./ 2) + (2 ./ x) + ((x .* y) ./ y2) + x;
  57. figure
  58. hold on
  59. plot(x, y, 'r-')
  60. plot(x, y2, 'b--')
  61. plot(x, y3, 'k:')
  62. hold off
  63. legend('y', 'y2', 'y3')
  64. axis([1 10 0 1000])
  65. fprintf('| x |   y  |  y2  |  y3  |\n')
  66. fprintf('| %i | %.2f | %.2f | %.2f |\n', [x; y; y2; y3])
  67. disp('---------')
  68.  
  69. % Problem 6
  70. temp = 10:10:50;
  71. expA = [1.15 2.86 3.11 4.1 5.2];
  72. expB = [1.11 2.8 3.05 3.9 6.9];
  73. figure
  74. subplot(1, 2, 1)
  75. hold on
  76. plot(temp, expA, 'rx')
  77. plot(temp, expB, 'bo')
  78. hold off
  79. title('Data v. Temperature')
  80. legend('Experiment A', 'Experiment B')
  81. xlabel('Temperature')
  82. ylabel('Data')
  83. error = ((expA - expB) ./ expA) * 100;
  84. subplot(1, 2, 2)
  85. plot(temp, error)
  86. title('Error v. Temperature')
  87. xlabel('Temperature')
  88. ylabel('Error Percent')
  89. % ---------
  90.  
  91. % Problem 7
  92. V = 100:50:900;
  93. y = 1.4;
  94. a = 1000;
  95. p_ptCalc = zeros(1, length(V));
  96. for i = 1:length(V)
  97.     p_ptCalc(i) = (1 + ((y - 1) / 2) * (V(i) / a)^2) ^ -(y / (y - 1));
  98. end
  99. p_ptExp = [.99 .97 .93 .9 .83 .75 .72 .64];
  100. M = .1:.1:.8;
  101. figure
  102. hold on
  103. plot(V / a, p_ptCalc, 'r-')
  104. plot(M, p_ptExp, 'b*')
  105. hold off
  106. legend('Calculated', 'Experimental')
  107. title('Pressure v. Mach')
  108. xlabel('Mach')
  109. ylabel('Pressure')
  110. % ---------
  111.  
  112. disp('Problem 8')
  113. a = input('What is a?\na = ');
  114. rho = 0.0023;
  115. R = 1716;
  116. % I didn't really understand the directions on what it wanted for T
  117. T = randi([480 520]);
  118. if a == 1
  119.     P = rho * R * T;
  120. elseif a == 2
  121.     P = rho * R * T ^ 1.01;
  122. elseif a == 3
  123.     P = rho ^ 1.01 * R * T;
  124. elseif a > 1.2 && a <= 1.5
  125.     P = rho * R * T ^ 1.05;
  126. else
  127.     disp('Input error. Invalid value for a.')
  128. end
  129. % In the else case, P is never set, which would cause the fprintf
  130. % to throw an error when it tries to access P, so I check to see if
  131. % P is set before I call fprintf.
  132. if exist('P', 'var')
  133.     fprintf('P = %.2f\n', P)
  134. end
  135. disp('---------')
  136.  
  137. disp('Problem 9')
  138. a = zeros(4);
  139. for i = 1:size(a, 1)
  140.     for j = 1:size(a, 2)
  141.         if i == j
  142.             a(i, j) = j;
  143.         elseif j > i
  144.             a(i, j) = 2 * j;
  145.         elseif j < i
  146.             a(i, j) = -j;
  147.         end
  148.     end
  149. end
  150. disp(a)
  151. disp('---------')
  152.  
  153. % Part of problem 10 is in a seperate function file (problem10.m).
  154. disp('Problem 10')
  155. rho = input('What is rho?\nrho = ');
  156. P = input('What is P?\nP = ');
  157. n = input('How many cases?\nn = ');
  158. for i = 1:n
  159.     fprintf('What are the values for velocity for case %i?\n', i)
  160.     V = input('V = ');
  161.     Po = problem10(P, rho, V)';
  162.     fprintf('Case %i Po = ', i)
  163.     fprintf('%.2f ', Po)
  164.     fprintf('\n')
  165. end
  166. disp('----------')
  167.  
  168. % Problem 11
  169. x = 0:6;
  170. y = zeros(1, length(x));
  171. for i = 1:length(x)
  172.     if x(i) >= 0 && x(i) < 2
  173.         y(i) = x(i) ^ 2 + .5 * x(i);
  174.     elseif x(i) >= 2 && x(i) < 4
  175.         y(i) = 2 * x(i) ^ 2 + 2;
  176.     else
  177.         y(i) = 5 * x(i) - 5;
  178.     end
  179. end
  180. figure
  181. plot(x, y)
  182. % ----------
  183.  
  184. problem2.m
  185.  
  186. function [y, df] = problem2(t, k)
  187.     % Usage: problem2(t, k)
  188.     % Returns y(t) and df(k)
  189.     y = (exp(-t) .* sin(2 * t) .^ 2) ./ (2 * (t + 1));
  190.     df = ((k .^ 4 + k .^ 3) ./ (k + 2)) + ((k .^ 2 + k) ./ (k + 1));
  191. end
  192.  
  193. problem10.m
  194.  
  195. function Po = problem10(P, rho, V)
  196.     % Usage: problem10(P, rho, V)
  197.     % Returns the bernoulli equation for the given values.
  198.     Po = P + .5 * rho .* V .^ 2;
  199. end
  200.  
  201. command output
  202.  
  203. ENGR 1181
  204. Midterm 2 Review
  205. ----------------
  206. Problem 1
  207. The answer is 0.77.
  208. The answer is 1.41.
  209. ---------
  210. Problem 2
  211. Input the values of t.
  212. t = 0:10
  213. Input the values of k.
  214. k = 1:5
  215. y(t) = 0.00e+00 7.60e-02 1.29e-02 4.86e-04 1.79e-03 1.66e-04 5.10e-05 5.59e-05 1.54e-06 3.48e-06 1.72e-06
  216. df(k) = 1.67 8.00 24.60 57.33 112.14
  217. ---------
  218. Problem 5
  219. | x |   y  |  y2  |  y3  |
  220. | 1 | 2.00 | 5.00 | 8.40 |
  221. | 2 | 6.00 | 22.00 | 69.55 |
  222. | 3 | 12.00 | 57.00 | 346.30 |
  223. | 4 | 20.00 | 116.00 | 1165.19 |
  224. | 5 | 30.00 | 205.00 | 3081.13 |
  225. | 6 | 42.00 | 330.00 | 6937.10 |
  226. | 7 | 56.00 | 497.00 | 13924.07 |
  227. | 8 | 72.00 | 712.00 | 25641.06 |
  228. | 9 | 90.00 | 981.00 | 44155.05 |
  229. | 10 | 110.00 | 1310.00 | 72061.04 |
  230. ---------
  231. Problem 8
  232. What is a?
  233. a = 2
  234. P = 2172.05
  235. ---------
  236. Problem 9
  237.      1     4     6     8
  238.     -1     2     6     8
  239.     -1    -2     3     8
  240.     -1    -2    -3     4
  241.  
  242. ---------
  243. Problem 10
  244. What is rho?
  245. rho = .0023
  246. What is P?
  247. P = 14.7/144
  248. How many cases?
  249. n = 2
  250. What are the values for velocity for case 1?
  251. V = 70:100
  252. Case 1 Po = 5.74 5.90 6.06 6.23 6.40 6.57 6.74 6.92 7.10 7.28 7.46 7.65 7.83 8.02 8.22 8.41 8.61 8.81 9.01 9.21 9.42 9.63 9.84 10.05 10.26 10.48 10.70 10.92 11.15 11.37 11.60
  253. What are the values for velocity for case 2?
  254. V = 100:120
  255. Case 2 Po = 11.60 11.83 12.07 12.30 12.54 12.78 13.02 13.27 13.52 13.77 14.02 14.27 14.53 14.79 15.05 15.31 15.58 15.84 16.11 16.39 16.66
  256. ----------
  257.  
  258. graphs
  259.  
  260. http://imgur.com/a/LAFHv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement