Function File to Solve Thermodynamics Problem
10 views (last 30 days)
Show older comments
Joseph
on 28 Sep 2011
Commented: Walter Roberson
on 5 Aug 2020
I am trying to solve a thermodynamics problem based on an isentropic relationship. The problem boils down to a higher order equation in terms of T2. Ultimately, T2 needs to be found. I would like to be able to write a function file that is a function of only T2 and then I can use bisection or newtons method to solve. I have input the isentropic relationship as my function, and the rest of my parameters under that. cp_2 and gamma2 are fucntions of T2. Right now, Matlab is not referencing the parameters under the isentropic relationship. Here is the code I have so far.
function [f]=temperature_2(T2)
f= T1*((.02*v1.^(gamma1-1))./(v1.^(gamma2-1)))-T2
%Givens
%Volume at state 1(cubic meters)
v1=.75
%Temperature at state 1(kelvin)
T1=375
%Universal gas constant(kJ/kmole)
R=8.314
%Matrix for a is given in the problem statement
a=[2.0909*10^1; 6.8717*10^-2; -5.2719*10^-5; 2.1664*10^-8; -4.5346*10^-12; 3.7873*10^-16];
%cp_1 calculation, there is enough info to compute this
cp_1=a(1)+T1.*(a(2)+T1.*(a(3)+T1.*(a(4)+T1.*(a(5)+T1.*a(6)))));
%cp_2 calculation, this is a function of T2
cp_2=a(1)+T2.*(a(2)+T2.*(a(3)+T2.*(a(4)+T2.*(a(5)+T2.*a(6)))));
%Compute Gamma as per equation given
gamma1=((cp_1)./(cp_1-R));
%This is a function of T2
gamma2=((cp_2)./(cp_2-R));
end
2 Comments
Andrew Newell
on 28 Sep 2011
Please see http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question to format your code correctly.
Rick Rosson
on 28 Sep 2011
Please format your code. If you do not know how, please click on the link called "Markup help" below.
Accepted Answer
Walter Roberson
on 28 Sep 2011
At the very least you need to move the f= line to the bottom of the function, as it relies upon values calculated or assigned in the function.
3 Comments
Korosh Agha Mohammad Ghasemi
on 3 Aug 2020
No, it is true that if passed down, the final wrong value will be obtained
Walter Roberson
on 5 Aug 2020
Korosh Agha Mohammad Ghasemi could you expand on what you are saying? Can you give an example input T2 value that the code gives the wrong output for (after moving the assignment to f to the end), together with an indication of what the correct value should be for that input ?
More Answers (0)
See Also
Categories
Find more on Thermodynamics and Heat Transfer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!