필터 지우기
필터 지우기

Determine which element violates the second law of thermodynamics

조회 수: 1 (최근 30일)
Caitlyn
Caitlyn 2023년 9월 29일
답변: Walter Roberson 2023년 9월 29일
Given a set of known and unknown mass balances, the problem wanted me to solve for mass, and temperature. It also wants me to solve based on it the second law of thermodynamics is violated at any point in the code or if a temperature cross over is reached that is not possible. I coded a series of if statements, but it keeps returning the same value. Here is my code:
%MASTERY PROBLEM 1.1
clc
close all
clear variables
%---------------------------------------------------------------%
% Part 1
%---------------------------------------------------------------%
%form matrix of mass balances
A_F = zeros(20);
A_F(1,1) = 1;A_F(1,2) = -1;
A_F(2,2) = 1;A_F(2,3) = -1;
A_F(3,3) = 1;A_F(3,4) = -1;
A_F(4,3) = 1;A_F(4,4) = -1;
A_F(5,5) = 1;A_F(5,6) = -1;
A_F(6,6) = 1;A_F(6,7) = -1;
A_F(7,6) = 1;A_F(7,7) = -1;
A_F(8,8) = 1;A_F(8,9) = -1;
A_F(9,9) = 1;A_F(9,10) = -1;
A_F(10,9) = 1;A_F(10,10) = -1;
A_F(11,11) = 1;A_F(11,12) = -1;
A_F(12,12) = 1;A_F(12,13) = -1;
A_F(13,12) = 1;A_F(13,13) = -1;
A_F(14,14) = 1;A_F(14,15) = -1;
A_F(15,15) = 1;A_F(15,16) = -1;
A_F(16,16) = 1;A_F(16,17) = -1;
A_F(17,16) = 1;A_F(17,17) = -1;
A_F(18,18) = 1;A_F(18,19) = -1;
A_F(19,19) = 1;A_F(19,20) = -1;
A_F(20,19) = 1;A_F(20,20) =1;
b_F = zeros(20,1);
%given values of F in matrix
FR = zeros(20,1);
FR(1) = 3.3;FR(2) = 3.3;FR(3) = 3.3;FR(4) = 3.3;
FR(5) = 2.4;FR(6) = 2.4;FR(7) = 2.4;
FR(8) = 1.8;FR(9) = 1.8;FR(10) = 1.8;
FR(11) = 6.9;FR(12) = 6.9;FR(13) = 6.9;
FR(14) = 3.4;FR(15) = 3.4;FR(16) = 3.4;FR(17) = 3.4;
FR(18) = 4.1;FR(19) = 4.1;FR(20) = 4.1;
%presents F in a table
% Heat capacities corresponding to flow rates
Cp = zeros(20,1);
Cp(1) = 2.4;Cp(2) = 2.4;Cp(3) = 2.4;Cp(4) = 2.4;
Cp(5) = 2.6;Cp(6) = 2.6;Cp(7) = 2.6;
Cp(8) = 0.8;Cp(9) = 0.8; Cp(10) = 0.8;
Cp(11) = 0.95;Cp(12) = 0.95;Cp(13) = 0.95;
Cp(14) = 1.35;Cp(15) = 1.35;Cp(16) = 1.35;Cp(17) = 1.35;
Cp(18) = 1.1;Cp(19) = 1.1;Cp(20) = 1.1;
%-------------------------------------------------------------------%
% Part 2: Display Temperatures in command line
%-------------------------------------------------------------------%
A_T = zeros(20);
b_T = zeros(20,1);
%input known temperature values into b_T
b_T(2) = 60;b_T(3) = 51;b_T(4) = 50;b_T(5) = 102;
b_T(6) = 81;b_T(9) = 121;b_T(10) = 70;b_T(11) = 55;
b_T(12) = 62;b_T(13) = 71;b_T(14) = 14;b_T(19) = 42; b_T(20) = 65;
%first row
A_T(1,1) = FR(1)*Cp(1);
A_T(1,2) = -FR(2)*Cp(2);
A_T(1,11) = FR(11)*Cp(11);
A_T(1,12) = -FR(12)*Cp(12);
A_T(2,2) = 1;
A_T(3,3) = 1;
A_T(4,4) = 1;
A_T(5,5) = 1;
A_T(6,6) = 1;
%seventh row
A_T(7,6) = FR(6)*Cp(6);
A_T(7,7) = -FR(7)*Cp(7);
A_T(7,19) = FR(19)*Cp(19);
A_T(7,20) = -FR(20)*Cp(20);
%eighth row
A_T(8,8) = FR(8)*Cp(8);
A_T(8,9) = -FR(9)*Cp(9);
A_T(8,12) = FR(12)*Cp(12);
A_T(8,13) = -FR(13)*Cp(13);
A_T(9,9) = 1;
A_T(10,10) = 1;
A_T(11,11) = 1;
A_T(12,12) = 1;
A_T(13,13) =1;
A_T(14,14) = 1;
%row fifteen
A_T(15,2) = FR(2)*Cp(2);
A_T(15,3) = -FR(3)*Cp(3);
A_T(15,14) = FR(14)*Cp(14);
A_T(15,15) = -FR(15)*Cp(15);
%row sixteen
A_T(16,5) = FR(5)*Cp(5);
A_T(16,6) = -FR(6)*Cp(6);
A_T(16,15) = -FR(15)*Cp(15);
A_T(16,16) = -FR(16)*Cp(16);
%row seventeen
A_T(17,9) = FR(9)*Cp(9);
A_T(17,10) = -FR(10)*Cp(10);
A_T(17,16) = FR(16)*Cp(16);
A_T(17,17) = FR(17)*Cp(17);
%row eighteen
A_T(18,3) = FR(3)*Cp(3);
A_T(18,4) = -FR(4)*Cp(4);
A_T(18,18) = FR(18)*Cp(18);
A_T(18,19) = -FR(19)*Cp(19);
A_T(19,19) = 1;
A_T(20,20) = 1;
% Create a system of equations to solve
N = length(A_T); % Size of system
% Perform Forward Elimination
for i = 1:(N-1)
for k = (i+1):N
rki = A_T(k,i)/A_T(i,i);
for j = i:N
A_T(k,j) = A_T(k,j) - rki*A_T(i,j);
end
b_T(k) = b_T(k) - rki*b_T(i);
end
end
% Perform Back Substitution
T = zeros(N,1);
T(N) = b_T(N)/A_T(N,N);
for i = N-1 : -1 : 1
summ = b_T(i);
for j = i+1:N
summ = summ - A_T(i,j)*T(j);
end
T(i) = summ/A_T(i,i);
end
t = renamevars(table(FR,T),["FR","T"],["Flowrates (kg/sec)","Temperatures (C)"])
t = 20×2 table
Flowrates (kg/sec) Temperatures (C) __________________ ________________ 3.3 65.794 3.3 60 3.3 51 3.3 50 2.4 102 2.4 81 2.4 64.377 1.8 161.97 1.8 121 1.8 70 6.9 55 6.9 62 6.9 71 3.4 14 3.4 29.529 3.4 -0.98039
%-------------------------------------------------------------%
% Part 3: Determine if any streams violate the second law
%-------------------------------------------------------------%
%The hot streams must get colder, cold streams must get hotter
law_violation = false;
% Exchanger A
if T(1) < T(2)
fprintf('Second Law Violated with temperatures %g\n', T(1),T(2), 'Hot streams cannot get hotter %g\n');
end
if T(11) > T(12)
fprintf('Second Law Violated with temperatures %g\n', T(11),T(12), 'Cold streams cannot get colder %g\n');
end
if (T(2) <= T(11))||T(1) >= T(12)
law_violation = true;
fprintf('Temperature crossover reached: %g\n', T(i));
end
Temperature crossover reached: 65.7936
%Exchanger B
if T(8) < T(9)
fprintf('Second Law Violated with temperatures %g\n', T(8),T(9), 'Hot streams cannot get hotter %g\n');
end
if T(12) > T(13)
fprintf('Second Law Violated with temperatures %g\n', T(12),T(13), 'Cold streams cannot get colder %g\n');
end
if T(9)<=T(12)||T(8)>=T(13)
law_violation = true;
fprintf('Temperature crossover reached: %g\n',T(i));
end
Temperature crossover reached: 65.7936
%Exchanger C
if T(2) < T(3)
fprintf('Second Law Violated with temperatures %g\n', T(2),T(3), 'Hot streams cannot get hotter %g\n');
end
if T(14) > T(15)
fprintf('Second Law Violated with temperatures %g\n', T(14),T(15), 'Cold streams cannot get colder %g\n');
end
if T(3)<T(14)
fprintf('Temperature crossover reached: %g\n', T(3),'<%g\n',T(14));
end
if T(2)>T(15)
fprintf('Temperature crossover reached: %g\n', T(2),'>%g\n',T(15));
end
Temperature crossover reached: 60 Temperature crossover reached: 62 Temperature crossover reached: 37 Temperature crossover reached: 103 Temperature crossover reached: 92 Temperature crossover reached: 110 Temperature crossover reached: 29.5294
%Exchanger D
if T(5) < T(6)
fprintf('Second Law Violated with temperatures %g\n', T(5),T(6), 'Hot streams cannot get hotter %g\n');
end
if T(15) > T(16)
fprintf('Second Law Violated with temperatures %g\n', T(15),T(16), 'Cold streams cannot get colder %g\n');
end
Second Law Violated with temperatures 29.5294 Second Law Violated with temperatures -0.980392 Second Law Violated with temperatures 67 Second Law Violated with temperatures 111 Second Law Violated with temperatures 108 Second Law Violated with temperatures 100 Second Law Violated with temperatures 32 Second Law Violated with temperatures 115 Second Law Violated with temperatures 116 Second Law Violated with temperatures 114 Second Law Violated with temperatures 101 Second Law Violated with temperatures 97 Second Law Violated with temperatures 109 Second Law Violated with temperatures 115 Second Law Violated with temperatures 32 Second Law Violated with temperatures 99 Second Law Violated with temperatures 97 Second Law Violated with temperatures 110 Second Law Violated with temperatures 110 Second Law Violated with temperatures 111 Second Law Violated with temperatures 116 Second Law Violated with temperatures 32 Second Law Violated with temperatures 103 Second Law Violated with temperatures 101 Second Law Violated with temperatures 116 Second Law Violated with temperatures 32 Second Law Violated with temperatures 99 Second Law Violated with temperatures 111 Second Law Violated with temperatures 108 Second Law Violated with temperatures 100 Second Law Violated with temperatures 101 Second Law Violated with temperatures 114 Second Law Violated with temperatures 32 Second Law Violated with temperatures 37 Second Law Violated with temperatures 103 Second Law Violated with temperatures 92 Second Law Violated with temperatures 110
if T(6)<T(15)
fprintf('Temperature crossover reached: %g\n', T(6),'<%g\n',T(15));
end
if T(5)>T(16)
fprintf('Temperature crossover reached: %g\n', T(5),'>%g\n',T(16));
end
Temperature crossover reached: 102 Temperature crossover reached: 62 Temperature crossover reached: 37 Temperature crossover reached: 103 Temperature crossover reached: 92 Temperature crossover reached: 110 Temperature crossover reached: -0.980392
%Exchanger E
if T(9) < T(10)
fprintf('Second Law Violated with temperatures %g\n', T(9),T(10), 'Hot streams cannot get hotter %g\n');
end
if T(16) > T(17)
fprintf('Second Law Violated with temperatures %g\n', T(16),T(17), 'Cold streams cannot get colder %g\n');
end
Second Law Violated with temperatures -0.980392 Second Law Violated with temperatures -15.0196 Second Law Violated with temperatures 67 Second Law Violated with temperatures 111 Second Law Violated with temperatures 108 Second Law Violated with temperatures 100 Second Law Violated with temperatures 32 Second Law Violated with temperatures 115 Second Law Violated with temperatures 116 Second Law Violated with temperatures 114 Second Law Violated with temperatures 101 Second Law Violated with temperatures 97 Second Law Violated with temperatures 109 Second Law Violated with temperatures 115 Second Law Violated with temperatures 32 Second Law Violated with temperatures 99 Second Law Violated with temperatures 97 Second Law Violated with temperatures 110 Second Law Violated with temperatures 110 Second Law Violated with temperatures 111 Second Law Violated with temperatures 116 Second Law Violated with temperatures 32 Second Law Violated with temperatures 103 Second Law Violated with temperatures 101 Second Law Violated with temperatures 116 Second Law Violated with temperatures 32 Second Law Violated with temperatures 99 Second Law Violated with temperatures 111 Second Law Violated with temperatures 108 Second Law Violated with temperatures 100 Second Law Violated with temperatures 101 Second Law Violated with temperatures 114 Second Law Violated with temperatures 32 Second Law Violated with temperatures 37 Second Law Violated with temperatures 103 Second Law Violated with temperatures 92 Second Law Violated with temperatures 110
if T(10)<T(16)
fprintf('Temperature crossover reached: %g\n', T(10),'<%g\n',T(16));
end
if T(9)>T(17)
fprintf('Temperature crossover reached: %g\n', T(9),'>%g\n',T(17));
end
Temperature crossover reached: 121 Temperature crossover reached: 62 Temperature crossover reached: 37 Temperature crossover reached: 103 Temperature crossover reached: 92 Temperature crossover reached: 110 Temperature crossover reached: -15.0196
%Exchanger F
if T(3) < T(4)
fprintf('Second Law Violated with temperatures %g\n', T(3),T(4), 'Hot streams cannot get hotter %g\n');
end
if T(18) > T(19)
fprintf('Second Law Violated with temperatures %g\n', T(18),T(19), 'Cold streams cannot get colder %g\n');
end
if T(4)<T(18)
fprintf('Temperature crossover reached: %g\n', T(4),'<%g\n',T(18));
end
if T(3)>T(19)
fprintf('Temperature crossover reached: %g\n', T(3),'>%g\n',T(19));
end
Temperature crossover reached: 51 Temperature crossover reached: 62 Temperature crossover reached: 37 Temperature crossover reached: 103 Temperature crossover reached: 92 Temperature crossover reached: 110 Temperature crossover reached: 42
%Exchanger G
if T(6) < T(7)
fprintf('Second Law Violated with temperatures %g\n', T(6),T(7), 'Hot streams cannot get hotter %g\n');
end
if T(19) > T(20)
fprintf('Second Law Violated with temperatures %g\n', T(19),T(20), 'Cold streams cannot get colder %g\n');
end
if T(7)<T(19)
fprintf('Temperature crossover reached: %g\n', T(7),'<%g\n',T(19));
end
if T(6)>T(20)
fprintf('Temperature crossover reached: %g\n', T(6),'>%g\n',T(20));
end
Temperature crossover reached: 81 Temperature crossover reached: 62 Temperature crossover reached: 37 Temperature crossover reached: 103 Temperature crossover reached: 92 Temperature crossover reached: 110 Temperature crossover reached: 65

답변 (1개)

Walter Roberson
Walter Roberson 2023년 9월 29일
fprintf('Temperature crossover reached: %g\n', T(6),'<%g\n',T(15));
That is not the syntax for outputing multiple variables. fprintf() does not take pairs of formats and values to output: it takes a single format followed by zero or more values to output.
You would instead use something similar to
fprintf('Temperature crossover reached: %g\n<%g\n', T(6),T(15));

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by