0% found this document useful (0 votes)
43 views13 pages

Codes

The document contains 9 sections describing thermodynamic processes and calculations. It includes calculations of heat transfer, ideal gas behavior, refrigeration cycles, and spring-driven piston processes. Plots are generated to show the effects of varying parameters such as thickness, pressure, temperature on associated thermal properties.

Uploaded by

qq99xyhqm4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Codes

The document contains 9 sections describing thermodynamic processes and calculations. It includes calculations of heat transfer, ideal gas behavior, refrigeration cycles, and spring-driven piston processes. Plots are generated to show the effects of varying parameters such as thickness, pressure, temperature on associated thermal properties.

Uploaded by

qq99xyhqm4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1)

t = 5 * 3600; % Time duration (seconds)


k = 0.78; % Thermal conductivity (W/m·°C)
dT = 10 - 3; % Temperature difference (°C)

% Heat loss calculation


heat_loss_05cm = k * dT * t / (0.5 / 100) / 1000; % for 0.5 cm thickness
heat_loss_1cm = k * dT * t / (1 / 100) / 1000; % for 1 cm thickness

% Plotting the effect of glass thickness on heat loss


thickness_range = linspace(0.002, 0.02, 100);
heat_loss_range = k * dT * t ./ (thickness_range * 100) / 1000;

figure;
plot(thickness_range * 100, heat_loss_range, 'b', 'LineWidth', 2);
xlabel('Glass Thickness (cm)');
ylabel('Heat Loss (kJ)');
title('Effect of Glass Thickness on Heat Loss');
grid on;

2)

d = 5 / 100; % Diameter (m)


T_surface = 70; % Surface temperature (°C)
T_surrounding = 20; % Surrounding temperature (°C)
h_range = linspace(5, 30, 100); % Convection coefficient (W/m^2 · °C)
epsilon = [0.1, 0.5, 0.8, 1]; % Emissivity

% Constants
sigma = 5.67e-8; % Stefan-Boltzmann constant (W/m^2 · K^4)

% Heat transfer rate calculation


heat_transfer_rates = zeros(length(epsilon), length(h_range));
for i = 1:length(epsilon)
for j = 1:length(h_range)
convective_heat = h_range(j) * pi * d * (T_surface - T_surrounding);
radiative_heat = epsilon(i) * sigma * pi * d^2 * (T_surface + 273.15)^4;
heat_transfer_rates(i, j) = convective_heat + radiative_heat;
end
end

% Plotting
figure;
plot(h_range, heat_transfer_rates, 'LineWidth', 2);
xlabel('Convection Heat Transfer Coefficient (W/m^2 · °C)');
ylabel('Rate of Heat Transfer (W)');
title('Effect of Convection Heat Transfer Coefficient on Heat Transfer Rate');
legend('Emissivity = 0.1', 'Emissivity = 0.5', 'Emissivity = 0.8', 'Emissivity =
1');
grid on;

3)

% Given data
V_refrigerant_initial = 0.01; % Initial volume of refrigerant-134a (m^3)
T_final = 20 + 273.15; % Final temperature of the refrigerant (K)
P_final = 400e3; % Final pressure of the refrigerant (Pa)

% Constants
R_specific = 188.93; % Specific gas constant for refrigerant-134a (J/kg·K)

% Range of initial pressures


P_initial_range = linspace(0.5e6, 1.5e6, 100); % Initial pressure range (Pa)

% Initialize array to store tank volumes


V_tank_final = zeros(size(P_initial_range));

% Loop over different initial pressures


for i = 1:length(P_initial_range)
P_initial = P_initial_range(i);

% Calculate initial mass of refrigerant using its specific volume at initial


state
v_specific_initial = 1 / V_refrigerant_initial; % Specific volume at initial
state (m^3/kg)
m_refrigerant_initial = V_refrigerant_initial / v_specific_initial; % Initial
mass of refrigerant (kg)

% Calculate specific volume of refrigerant at final state using the ideal gas
law
v_specific_final = R_specific * T_final / P_final;

% Calculate volume of the tank at final state


V_tank_final(i) = m_refrigerant_initial * v_specific_final;
end

% Plotting the effect of initial pressure on tank volume


plot(P_initial_range / 1e6, V_tank_final, 'LineWidth', 2);
xlabel('Initial Pressure (MPa)');
ylabel('Volume of the Tank at Final State (m^3)');
title('Effect of Initial Pressure on Tank Volume');
grid on;

4)

% Constants
R = 8.314; % Gas constant in J/(mol*K)
% Initial conditions
V1 = 0.5; % Volume of tank 1 in m^3
T1 = 20 + 273.15; % Temperature of tank 1 in K
P1 = 600 * 1000; % Pressure of tank 1 in Pa
V2 = 0.5; % Volume of tank 2 in m^3
T2 = 30 + 273.15; % Temperature of tank 2 in K
P2 = 150 * 1000; % Pressure of tank 2 in Pa
T_surroundings = -10:1:30; % Surroundings temperature range in °C
T_surroundings = T_surroundings + 273.15; % Convert to Kelvin

% Preallocate array to store final pressures


final_pressure = zeros(size(T_surroundings));

for i = 1:length(T_surroundings)
% Calculate final equilibrium pressure
T_sur = T_surroundings(i);
n1 = (P1*V1)/(R*T1); % moles of gas in tank 1
n2 = (P2*V2)/(R*T2); % moles of gas in tank 2
n_total = n1 + n2; % total moles of gas
V_total = V1 + V2; % total volume
T_final = (T1*n1*T1 + T2*n2*T2 + T_sur*n_total)/(n1*T1 + n2*T2 + n_total); %
final temperature
final_pressure(i) = (n_total*R*T_final)/V_total; % final pressure
end

% Plot
figure;
plot(final_pressure / 1000, T_surroundings, 'LineWidth', 2);
xlabel('Final Pressure (kPa)');
ylabel('Surroundings Temperature (K)');
title('Final Pressure vs Surroundings Temperature');
grid on;

5)

% Given data
P = 10e6; % Pressure (Pa)
T = 400 + 273.15; % Temperature (K)

% Constants
R = 8.314; % Universal gas constant (J/mol·K)

% Ideal gas equation


v_ideal_gas = R * T / P;

% Calculate percent error for ideal gas approximation using steam tables
T_range = 325:25:600; % Temperature range (°C)
v_steam_tables = 0.0428; % Specific volume at 10 MPa and 400°C (m^3/kg)
v_percent_error = abs(v_steam_tables - R * (T_range + 273.15) / P) / v_steam_tables
* 100;

% Plot percent error against temperature


plot(T_range, v_percent_error, 'LineWidth', 2);
xlabel('Temperature (°C)');
ylabel('Percent Error in Ideal Gas Approximation (%)');
title('Percent Error in Ideal Gas Approximation vs Temperature');
grid on;

6)

% Given data
diameter_range = linspace(5, 15, 100); % Balloon diameter range (m)
T = 20 + 273.15; % Temperature (K)
P_values = [100e3, 200e3]; % Pressure values (Pa)
Molar_mass = 4.002602; % Molar mass of helium (g/mol)

% Constants
R = 8.314; % Universal gas constant (J/mol·K)

% Loop over different pressures


for j = 1:length(P_values)
P = P_values(j);
% Calculate volume of the balloon
volume = (4/3) * pi * (diameter_range/2).^3;
% Calculate number of moles of helium using ideal gas law
n = P * volume / (R * T);
% Calculate mass of helium
mass = n * Molar_mass; % Mass in grams
% Plot mass of helium against balloon diameter for current pressure
plot(diameter_range, mass, 'LineWidth', 2);
hold on;
end

% Add labels and legend


xlabel('Balloon Diameter (m)');
ylabel('Mass of Helium (g)');
title('Effect of Balloon Diameter on Mass of Helium');
legend('Pressure = 100 kPa', 'Pressure = 200 kPa');
grid on;

7)

% Constants
R = 8.314; % Universal gas constant in J/(mol*K)
v = 1; % Specific volume in m^3
m = 2.841; % Mass of steam in kg
P_range = 0.1:0.1:1; % Pressure range in MPa

% (a) Ideal gas equation


T_ideal = (P_range * 1e6 * v) / (R * 1000); % Convert pressure from MPa to Pa, R
from J/(mol*K) to kJ/(kg*K)

% (b) Van der Waals equation


a = 0.4077; % Van der Waals constant in m^3/(kg*K)
b = 3.7934e-5; % Van der Waals constant in m^3/kg
T_vdw = arrayfun(@(P) fzero(@(T) (R * 1000 * T * v) - (P + a * (m / v)^2) * v + b *
P * m, 300), P_range * 1e6);

% (c) Steam tables (Interpolation)


P_steam = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]; % Pressure from steam
tables in MPa
T_steam = [350, 360, 370, 380, 390, 400, 410, 420, 430, 440]; % Corresponding
temperature in K
T_interp = interp1(P_steam, T_steam, P_range);

% Plot percent error in ideal gas approximation


percent_error = abs((T_ideal - T_interp) ./ T_interp) * 100;

plot(P_range, percent_error, 'LineWidth', 2);


xlabel('Pressure (MPa)');
ylabel('Percent Error (%)');
title('Percent Error in Ideal Gas Approximation vs Pressure');
grid on;

8)

% Given data
V_initial = 200; % Initial volume (L)
T_initial = 20; % Initial temperature (°C)
T_final = 70; % Final temperature (°C)
P_range = linspace(400, 1200, 100); % Pressure range (kPa)

% Constants
R_specific = 0.18893; % Specific gas constant for refrigerant-134a (kJ/kg·K)
Molar_mass = 102.03; % Molar mass of refrigerant-134a (g/mol)

% Convert temperatures to Kelvin


T_initial_K = T_initial + 273.15;
T_final_K = T_final + 273.15;

% Initialize array to store work done


work_done = zeros(size(P_range));
% Calculate work done for each pressure in the range
for i = 1:length(P_range)
P = P_range(i); % Pressure (kPa)

% Calculate initial specific volume using ideal gas law for saturated liquid
v_initial = R_specific * T_initial_K / P;

% Calculate final specific volume using ideal gas law for superheated vapor
v_final = R_specific * T_final_K / P;

% Calculate work done using area under the curve (trapezoidal rule)
work_done(i) = (v_final - v_initial) * P * 1000; % Convert from kJ to J
end

% Plot work done versus pressure


figure;
plot(P_range, work_done, 'LineWidth', 2);
xlabel('Pressure (kPa)');
ylabel('Work Done (J)');
title('Effect of Pressure on Work Done');
grid on;

% Plot process on P-v diagram


figure;
hold on;
plot([V_initial V_initial], [P_range(1) P_range(end)], 'k--', 'LineWidth', 2); %
Initial vertical line
plot([V_initial v_final], [P_range(end) P_range(end)], 'k--', 'LineWidth', 2); %
Final horizontal line
plot([V_initial v_final], [P_range(1) P_range(end)], 'b', 'LineWidth', 2); %
Process line
hold off;
xlabel('Volume (L)');
ylabel('Pressure (kPa)');
title('P-v Diagram for Process');
legend('Initial State', 'Final State', 'Process');
grid on;

9)

% Given data
m_water = 50; P_initial = 250e3; T_initial = 25 + 273.15; A_piston = 0.1; V_final_1
= 0.2; V_final_2 = V_final_1 + 0.2;
k_spring_range = linspace(50e3, 500e3, 100); delta_x = 0.2; delta_x_additional =
0.2;

% Constants
R_specific = 8.314; Molar_mass = 18.01528;

% Calculate initial volume and force exerted by the piston


V_initial = m_water * R_specific * T_initial / (P_initial * 1000);
F_initial = P_initial * A_piston;

% Initialize arrays
[P_final, boundary_work] = deal(zeros(size(k_spring_range)));

% Loop over different spring constants


for i = 1:length(k_spring_range)
k_spring = k_spring_range(i);

% Calculate final force exerted by the piston after reaching the spring
F_final = k_spring * delta_x;

% Calculate work done during the process


boundary_work(i) = F_initial * delta_x + 0.5 * k_spring * delta_x_additional^2;

% Calculate final pressure using the ideal gas law


V_final = V_initial + A_piston * delta_x + A_piston * delta_x_additional;
P_final(i) = m_water * R_specific * T_initial / (V_final * 1000);
end

% Plot final pressure and work done against spring constant


figure;
subplot(2,1,1);
plot(k_spring_range / 1e3, P_final / 1e3, 'LineWidth', 2);
xlabel('Spring Constant (kN/m)'); ylabel('Final Pressure (kPa)'); title('Effect of
Spring Constant on Final Pressure'); grid on;

subplot(2,1,2);
plot(k_spring_range / 1e3, boundary_work / 1e3, 'LineWidth', 2);
xlabel('Spring Constant (kN/m)'); ylabel('Boundary Work Done (kJ)'); title('Effect
of Spring Constant on Boundary Work Done'); grid on;

10)

% Given data
P_in = 300e3; % Inlet pressure (Pa)
T_in = 200 + 273.15; % Inlet temperature (K)
V_in = 30; % Inlet velocity (m/s)
P_out = 100e3; % Outlet pressure (Pa)
V_out = 180; % Outlet velocity (m/s)
A_in = 80 / 1e4; % Inlet area (m^2)

% Constants
gamma = 1.4; % Specific heat ratio for air
% (a) Calculate mass flow rate through the nozzle using mass flow rate equation
rho_in = P_in / (287 * T_in); % Inlet density (kg/m^3)
m_dot = rho_in * V_in * A_in; % Mass flow rate (kg/s)

% (b) Calculate exit temperature of the air using isentropic relations


T_out = T_in * (P_out / P_in)^((gamma-1)/gamma); % Exit temperature (K)

% (c) Calculate exit area of the nozzle using continuity equation


A_out = m_dot / (rho_in * V_out); % Exit area (m^2)

% Print results
fprintf('(a) Mass flow rate: %.4f kg/s\n', m_dot);
fprintf('(b) Exit temperature: %.2f K\n', T_out);
fprintf('(c) Exit area: %.4f m^2\n', A_out);

% Effect of inlet area on mass flow rate, exit temperature, and exit area
A_in_range = linspace(50 / 1e4, 150 / 1e4, 100); % Inlet area range (m^2)

% Initialize arrays to store results


m_dot_range = zeros(size(A_in_range));
T_out_range = zeros(size(A_in_range));
A_out_range = zeros(size(A_in_range));

% Loop over different inlet areas


for i = 1:length(A_in_range)
A_in_temp = A_in_range(i);

% Calculate mass flow rate


m_dot_range(i) = rho_in * V_in * A_in_temp;

% Calculate exit temperature


T_out_range(i) = T_in * (P_out / P_in)^((gamma-1)/gamma);

% Calculate exit area


A_out_range(i) = m_dot_range(i) / (rho_in * V_out);
end

% Plot results against inlet area


figure;
subplot(3,1,1);
plot(A_in_range * 1e4, m_dot_range, 'LineWidth', 2);
xlabel('Inlet Area (cm^2)');
ylabel('Mass Flow Rate (kg/s)');
title('Effect of Inlet Area on Mass Flow Rate');
grid on;

subplot(3,1,2);
plot(A_in_range * 1e4, T_out_range, 'LineWidth', 2);
xlabel('Inlet Area (cm^2)');
ylabel('Exit Temperature (K)');
title('Effect of Inlet Area on Exit Temperature');
grid on;

subplot(3,1,3);
plot(A_in_range * 1e4, A_out_range * 1e4, 'LineWidth', 2);
xlabel('Inlet Area (cm^2)');
ylabel('Exit Area (cm^2)');
title('Effect of Inlet Area on Exit Area');
grid on;

11)

Not done

12)

% Given data
T_source_range = linspace(300, 1000, 100); % Source temperature range (°C)
T_sink_range = [0, 25, 50]; % Sink temperature range (°C)
Q_in = 1200 * 60; % Heat supplied to the engine (kJ/min) converted to kJ/s

% Constants
T_0 = 273.15; % Absolute zero temperature (K)

% Initialize arrays to store results


power_produced = zeros(length(T_source_range), length(T_sink_range));
cycle_efficiency = zeros(length(T_source_range), length(T_sink_range));

% Loop over different source temperatures


for i = 1:length(T_source_range)
T_source = T_source_range(i) + T_0; % Source temperature (K)

% Loop over different sink temperatures


for j = 1:length(T_sink_range)
T_sink = T_sink_range(j) + T_0; % Sink temperature (K)

% Calculate Carnot efficiency


efficiency_carnot = 1 - T_sink / T_source;

% Calculate maximum power output (W)


max_power_output = Q_in * efficiency_carnot;

% Store results
power_produced(i, j) = max_power_output;
cycle_efficiency(i, j) = efficiency_carnot;
end
end
% Plot power produced and cycle efficiency against source temperature
figure;
subplot(2,1,1);
plot(T_source_range, power_produced, 'LineWidth', 2);
xlabel('Source Temperature (°C)');
ylabel('Power Produced (W)');
title('Effect of Source Temperature on Power Produced');
legend('Sink Temp: 0°C', 'Sink Temp: 25°C', 'Sink Temp: 50°C');
grid on;

subplot(2,1,2);
plot(T_source_range, cycle_efficiency, 'LineWidth', 2);
xlabel('Source Temperature (°C)');
ylabel('Cycle Efficiency');
title('Effect of Source Temperature on Cycle Efficiency');
legend('Sink Temp: 0°C', 'Sink Temp: 25°C', 'Sink Temp: 50°C');
grid on;

13)

% Given data
m_steam = 0.0103; % Mass of steam (kg)
W_net_range = linspace(15, 25, 100); % Net work output range (kJ)

% Constants
R_specific = 0.4615; % Specific gas constant for steam (kJ/kg·K)

% Calculate maximum absolute temperature (Tw) and minimum absolute temperature (Tc)
Tw = 2 * 273.15; % Max absolute temperature (K)
Tc = 273.15; % Min absolute temperature (K)

% Initialize array to store temperature of steam during heat rejection process


T_reject = zeros(size(W_net_range));

% Loop over different net work outputs


for i = 1:length(W_net_range)
W_net = W_net_range(i); % Net work output (kJ)

% Calculate heat input and heat rejection


Q_in = W_net / (1 - Tw/Tc); % Heat input (kJ)
Q_out = Q_in - W_net; % Heat rejection (kJ)

% Calculate temperature of steam during heat rejection process


T_reject(i) = (Q_out / (m_steam * R_specific)) + 273.15; % Convert to °C
end

% Plot temperature of steam during heat rejection process against net work output
plot(W_net_range, T_reject, 'LineWidth', 2);
xlabel('Net Work Output (kJ)');
ylabel('Temperature during Heat Rejection (°C)');
title('Effect of Net Work Output on Steam Temperature during Heat Rejection');
grid on;

14)

% Given data
Q_added_range = [500, 900, 1300]; % Heat added to the working fluid (kJ)
T_source_range = linspace(100, 1000, 100); % Source temperature range (°C)

% Constants
R_specific = 0.287; % Specific gas constant for air (kJ/kg·K)

% Initialize arrays to store results


entropy_change_fluid = zeros(length(T_source_range), length(Q_added_range));
entropy_change_source = -entropy_change_fluid; % Source entropy change is the
negative of fluid entropy change
total_entropy_change = entropy_change_fluid + entropy_change_source;

% Loop over different amounts of heat added


for i = 1:length(Q_added_range)
Q_added = Q_added_range(i); % Heat added to the working fluid (kJ)

% Loop over different source temperatures


for j = 1:length(T_source_range)
T_source = T_source_range(j) + 273.15; % Source temperature (K)

% Calculate entropy changes


entropy_change_fluid(j, i) = Q_added / T_source;
total_entropy_change(j, i) = 0;
end
end

% Plot entropy changes against source temperature for different heat added
figure;
for i = 1:length(Q_added_range)
subplot(3,1,i);
plot(T_source_range, entropy_change_fluid(:, i), 'LineWidth', 2);
hold on;
plot(T_source_range, entropy_change_source(:, i), 'LineWidth', 2);
plot(T_source_range, total_entropy_change(:, i), 'LineWidth', 2);
hold off;
xlabel('Source Temperature (°C)');
ylabel('Entropy Change (kJ/K)');
title(sprintf('Effect of Source Temperature on Entropy Change (Q = %d kJ)',
Q_added_range(i)));
legend('Working Fluid', 'Source', 'Total');
grid on;
end

15)

% Given data
m_aluminum = 20; % Mass of aluminum block (kg)
T_aluminum_initial = 200 + 273.15; % Initial temperature of aluminum block (K)
m_iron_range = 1:10; % Range of mass of iron block (kg)
T_iron_initial = 100 + 273.15; % Initial temperature of iron block (K)
m_total = m_aluminum + m_iron_range; % Total mass of both blocks (kg)

% Constants
C_aluminum = 0.903; % Specific heat capacity of aluminum (kJ/kg·K)
C_iron = 0.449; % Specific heat capacity of iron (kJ/kg·K)

% Initialize arrays to store results


T_final = zeros(size(m_iron_range)); % Final equilibrium temperature (K)
delta_S_total = zeros(size(m_iron_range)); % Total entropy change (kJ/K)

% Loop over different masses of iron block


for i = 1:length(m_iron_range)
m_iron = m_iron_range(i);

% Calculate the final equilibrium temperature


T_final(i) = (m_aluminum * C_aluminum * T_aluminum_initial + m_iron * C_iron *
T_iron_initial) / (m_total(i) * (C_aluminum + C_iron));

% Calculate the entropy change for aluminum block


delta_S_aluminum = m_aluminum * C_aluminum * log(T_final(i) /
T_aluminum_initial);

% Calculate the entropy change for iron block


delta_S_iron = m_iron * C_iron * log(T_final(i) / T_iron_initial);

% Calculate the total entropy change


delta_S_total(i) = delta_S_aluminum + delta_S_iron;
end

% Plot equilibrium temperature and total entropy change against iron mass
figure;
subplot(2,1,1);
plot(m_iron_range, T_final, 'LineWidth', 2);
xlabel('Mass of Iron Block (kg)');
ylabel('Equilibrium Temperature (K)');
title('Effect of Iron Mass on Equilibrium Temperature');
grid on;
subplot(2,1,2);
plot(m_iron_range, delta_S_total, 'LineWidth', 2);
xlabel('Mass of Iron Block (kg)');
ylabel('Total Entropy Change (kJ/K)');
title('Effect of Iron Mass on Total Entropy Change');
grid on;

You might also like