fork download
  1. % Ideal Gas Law Plot and Temperature Calculation
  2.  
  3. % Given Data
  4. V = [1, 2, 3, 4, 5]; % Volume in Liters
  5. P = [2494.2, 1247.1, 831.4, 623.6, 498.8]; % Pressure in kPa
  6.  
  7. % Calculate 1/V
  8. invV = 1 ./ V;
  9.  
  10. % Perform linear regression: P = slope * (1/V) + intercept
  11. coeffs = polyfit(invV, P, 1);
  12. slope = coeffs(1);
  13. intercept = coeffs(2);
  14.  
  15. % Given constants
  16. n = 1; % mol
  17. R = 8.314; % kPa·L/mol·K
  18.  
  19. % Calculate temperature
  20. T = slope / (n * R);
  21.  
  22. % Display result
  23. fprintf('--- Sample Output ---\n');
  24. fprintf('Linear fit equation: P = %.4f*(1/V) + %.4f\n', slope, intercept);
  25. fprintf('Calculated Temperature: %.1f K\n', T);
  26.  
  27. % Predicted pressure values for the line
  28. P_fit = polyval(coeffs, invV);
  29.  
  30. % Plotting
  31. figure;
  32. plot(invV, P, 'bo', 'MarkerFaceColor', 'b'); hold on;
  33. plot(invV, P_fit, 'r-', 'LineWidth', 2);
  34. xlabel('1/V (1/L)');
  35. ylabel('Pressure (kPa)');
  36. title('Pressure vs. 1/Volume');
  37. legend('Experimental Data', 'Best Fit Line', 'Location', 'northwest');
  38. grid on;
  39.  
  40.  
  41.  
Success #stdin #stdout 0.37s 61328KB
stdin
Standard input is empty
stdout
--- Sample Output ---
Linear fit equation: P = 2494.1998*(1/V) + 0.0021
Calculated Temperature: 300.0 K