CodeCodeCode

ENGR132: HW04 - phases_name

May 1st, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.91 KB | None | 0 0
  1. function phase = phases_name(temp, percent)
  2.  
  3. % FUNCTION: Given the temperature and the percent composition of B,
  4. % program finds the phase at which the compound would be.
  5. %
  6. % INPUTS:
  7. % 1) temp: The temperature. (°C)
  8. % 2) percent: The percentage composition of B. (%)
  9. %  
  10. % OUTPUTS:
  11. % 1) phase: The phase of the compound.
  12.  
  13. %-----------INPUT-------------
  14. % A / A+B boundary:           y = 20x
  15. % A / A+Liquid boundary:      y = (-330/15)x + 630
  16. % A+Liquid / Liquid boundary: y = (-330/35)x + 630
  17. % B+Liquid / Liquid boundary: y = (450/65)(x-35) + 300
  18. % B / B+Liquid boundary:      y = (450/20)(x-80) + 300
  19. % B / A+B boundary:           y = -15(x - 80) + 300
  20.  
  21. %--------CALCULATIONS---------
  22. % If temperature is between 0°C and 300°C.
  23. if temp >= 0 && temp < 300
  24.     % If percent is between 0% and 15%.
  25.     if percent >= 0 && percent <= 15
  26.         % Compares temperature "temp" to function y = 20x.
  27.         % Depending on whether "temp" is more than, equal to, or less
  28.         % than y, we can find if it is above, on, or below our line.
  29.         if temp > (20 * percent)
  30.             phase = 'A';
  31.         elseif temp == (20 * percent)
  32.             phase = 'Between A and A+B';
  33.         elseif temp < (20 * percent)
  34.             phase = 'A+B';
  35.         end
  36.     % If percent is between 15% and 80%.
  37.     elseif percent > 15 && percent < 80
  38.         phase = 'A+B';
  39.     % If percent is between 80% and 100%.
  40.     elseif percent >= 80 && percent <= 100
  41.         % Compares temperature "temp" to function y = -15(x - 80) + 300.
  42.         % Depending on whether "temp" is more than, equal to, or less
  43.         % than y, we can find if it is above, on, or below our line.
  44.         if temp > ((-15 * (percent - 80)) + 300)
  45.             phase = 'B';
  46.         elseif temp == ((-15 * (percent - 80)) + 300)
  47.             phase = 'Between B and A+B';
  48.         elseif temp < ((-15 * (percent - 80)) + 300)
  49.             phase = 'A+B';
  50.         end
  51.     % Error check for percentage input.
  52.     else
  53.         error('You put in a weird percentage; 0 to 100 only.');
  54.     end
  55. % If temperature is exactly 300°C.
  56. elseif temp == 300
  57.     % Checks areas along the eutectic line for differences.
  58.     if percent >= 0 && percent < 15
  59.         phase = 'A';
  60.     elseif percent == 15
  61.         phase = 'Between A, A+B, and A+Liquid (Eutectic line)';
  62.     elseif percent > 15 && percent < 35
  63.         phase = 'Between A+Liquid and A+B (Eutectic line)';
  64.     elseif percent == 35
  65.         phase = 'Eutectic point';
  66.     elseif percent > 35 && percent < 80
  67.         phase = 'Between B+Liquid and A+B (Eutectic line)';
  68.     elseif percent == 80
  69.         phase = 'Between B, B+Liquid, and A+B (Eutectic line)';
  70.     elseif percent > 80 && percent <= 100
  71.         phase = 'B';
  72.     % Error check for percentage input.
  73.     else
  74.         error('You put in a weird percentage; 0 to 100 only.');
  75.     end
  76. % If temperature is over 300°C.
  77. elseif temp > 300
  78.     % If percent is between 0% and 35%.
  79.     if percent >= 0 && percent < 35
  80.         % Compares temperature "temp" to function y = (-330/35)x + 630.
  81.         % Depending on whether "temp" is more than, equal to, or less
  82.         % than y, we can find if it is above, on, or below our line.
  83.         if temp > (((-330 / 35) * percent) + 630)
  84.             phase = 'Liquid';
  85.         elseif temp == (((-330 / 35) * percent) + 630)
  86.             phase = 'Between A+Liquid and Liquid';
  87.         elseif temp < (((-330 / 35) * percent) + 630)
  88.             % Compares temperature "temp" to function y = (-330/15)x + 630.
  89.             % Depending on whether "temp" is more than, equal to, or less
  90.             % than y, we can find if it is above, on, or below our line.
  91.             if temp > (((-330 / 15) * percent) + 630)
  92.                 phase = 'A+Liquid';
  93.             elseif temp == (((-330 / 15) * percent) + 630)
  94.                 phase = 'Between A+Liquid and A';
  95.             elseif temp < (((-330 / 15) * percent) + 630)
  96.                 phase = 'A';
  97.             end
  98.         end
  99.     % If percent is exactly 35%.
  100.     elseif percent == 35
  101.         phase = 'Liquid';
  102.     % If percent is between 35% and 100%.
  103.     elseif percent > 35 && percent <= 100
  104.         % Compares temperature "temp" to function y = (450/65)(x-35) + 300.
  105.         % Depending on whether "temp" is more than, equal to, or less
  106.         % than y, we can find if it is above, on, or below our line.
  107.         if temp > (((450 / 65) * (percent - 35)) + 300)
  108.             phase = 'Liquid';
  109.         elseif temp == (((450 / 65) * (percent - 35)) + 300)
  110.             phase = 'Between B+Liquid and Liquid';
  111.         elseif temp < (((450 / 65) * (percent - 35)) + 300)
  112.             % Compares temperature "temp" to function y = (450/20)(x-80) + 300.
  113.             % Depending on whether "temp" is more than, equal to, or less
  114.             % than y, we can find if it is above, on, or below our line.
  115.             if temp > (((450 / 20) * (percent - 80)) + 300)
  116.                 phase = 'B+Liquid';
  117.             elseif temp == (((450 / 20) * (percent - 80)) + 300)
  118.                 phase = 'Between B+Liquid and B';
  119.             elseif temp < (((450 / 20) * (percent - 80)) + 300)
  120.                 phase = 'B';
  121.             end
  122.         end
  123.     % Error check for percentage input.
  124.     else
  125.         error('You put in a weird percentage; 0 to 100 only.');
  126.     end
  127. % Error check for temperature input.
  128. else
  129.     error('You put in a weird temperature; positive only.');
  130. end
  131.  
  132. %-----------OUTPUT------------
  133. %Prints results in a fancy fasion.
  134. fprintf('For %0.1f mass %%B at %0.1f degrees C, the phase is %s.\n', percent, temp, phase);
  135.  
  136. % phases_name(400,12);
  137. % For 12.0 mass %B at 400.0 degrees C, the phase is A+Liquid.
  138. % phases_name(300,35);
  139. % For 35.0 mass %B at 300.0 degrees C, the phase is Eutectic point.
  140. % phases_name(280,60);
  141. % For 60.0 mass %B at 280.0 degrees C, the phase is A+B.
  142. % phases_name(100,110);
  143. % ??? Error using ==> phases_name at 71
  144. % You put in a weird percentage; 0 to 100 only.
Advertisement
Add Comment
Please, Sign In to add comment