Optimize code using loop

1 回表示 (過去 30 日間)
Ahmad AlJuhani
Ahmad AlJuhani 2019 年 2 月 11 日
回答済み: Ahmad AlJuhani 2019 年 2 月 12 日
clc; clear all; close all;
Tc = 369.8; % Propan critical Temp in K
funPr = @(Vr,Tr) 8/3 * Tr./(Vr - 1/3) - 3./(Vr.^2); % van der Waals equation of state for a gas
T = 300
Tr = T / Tc;
Vr = linspace(0.5,4,100);
Pr1 = funPr(Vr,Tr);
T = 310;
Tr = T / Tc;
Vr = linspace(0.5,4,100);
Pr2 = funPr(Vr,Tr);
T = 320;
Tr = T / Tc;
Vr = linspace(0.5,4,100);
Pr3 = funPr(Vr,Tr);
T = 330;
Tr = T / Tc;
Vr = linspace(0.5,4,100);
Pr4 = funPr(Vr,Tr);
% Plot the P_V Equaiton of State
figure; hold all
plot (Vr,Pr1)
plot (Vr,Pr2)
plot (Vr,Pr3)
plot (Vr,Pr4)
ylim([-0.5 2])
xlabel('V reduced')
ylabel('P reduced')
  2 件のコメント
Bob Thompson
Bob Thompson 2019 年 2 月 11 日
What are you actually looking for help with? I don't see a question in your post.
Star Strider
Star Strider 2019 年 2 月 11 日
What is your code supposed to do?
What is your Question?

サインインしてコメントする。

採用された回答

aara
aara 2019 年 2 月 11 日
編集済み: aara 2019 年 2 月 11 日
Check the comments within the code:
clc; clear all; close all;
Tc = 369.8; % Propan critical Temp in K
funPr = @(Vr,Tr) 8/3 * Tr./(Vr - 1/3) - 3./(Vr.^2); % van der Waals equation of state for a gas
T = 300:10:330; %start by creating vector which holds your temperatures
Vr = linspace(0.5,4,100); %there is no need to re-declare Vr every time in the code.
for i=1:length(T) %for loop cycles through all the temperatures stored in T
Tr = T(i)/Tc;
Pr(i,:) = funPr(Vr,Tr); %Pr(i,:) is a matrix that stores your results
end
plot(Vr,Pr); %plot
ylim([-0.5 2])
xlabel('V reduced')
ylabel('P reduced')
You can increase the T vector size (change 330 to 400 for example) to add more isotherms to plot for the van der Waals equation.

その他の回答 (1 件)

Ahmad AlJuhani
Ahmad AlJuhani 2019 年 2 月 12 日
The qustion was how to optimize the code by using loop function.
Thanks aara

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by