Variable range gives a wrong answer

1 vue (au cours des 30 derniers jours)
BioZ
BioZ le 8 Nov 2021
Commenté : BioZ le 8 Nov 2021
Greetings everyone, I have the following code, and my issue with it is that when I use a range of Pc from 400 to 600 then my mdot at 400 is 0.0002383 but it has to be as in mdotc equation = 0.0001987. There is a significant difference. Any idea why?
Equations with extra c is check equations which gives the right answer and equations below are those where I tried to use a range from 400 to 600.
Any help woud be useful. :)
clc; clear all; close all
%Variables
g=5/3;
g1=(g-1)/2;
g2=(1-g)/2;
Ac=1.267*10^-4;
Pc=400:600; % Pc range from 400 to 600
Pcc= 400; %Variable check
T0=350;
P0=615;
Rg=207.85;
Tcc = T0*(Pcc/P0)^((g-1)/g)-1 %Check at Pcc=400
mc= sqrt((1/g1)*((P0/Pcc)^((g-1)/g)-1))%Check at Pcc=400
mdotc = (Pcc/(sqrt(Tcc))*mc*sqrt(g/Rg)*Ac)%Check at Pcc=400
Tc = T0*(Pc/P0).^((g-1)/g)-1;
m = sqrt((1/g1)*((P0./Pc).^((g-1)/g)-1));
mdot = (Pc/(sqrt(Tc))*m*sqrt(g/Rg)*Ac);
plot(Pc,mdot)
xlabel('Background Pressure (Pc)')
ylabel('Mass flow rate (kg/s)')

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Nov 2021
Vectorize, Vectorize, Vectorize!
I recommend that you get in the habit of always using .* and ./ and .^ for everything (except perhaps when using scalar numeric constants as the first or last thing at the same artithmetic priority). Even if you are writing with variables you know are scalar now, if later you extend the variable to non-scalar, if you are in the habit of using the dot versions of the operators then you will not need to debug the failures later.
The exception, of course, being for cases where you are deliberately doing inner product (algebraic matrix multiplication) or deliberately doing matrix left divide \ or deliberately doing matrix right divide /
Even when using scalar numerics, keep in mind that 1/x with scalar x works the same as 1./x but that if you extend x to be non-scalar then 1/x suddenly will do something completely different. It is always safe to use / to divide by a scalar numeric value (unless the numeric value is being raised to a power, as power has higher priority)
It is safe to have expression * scalar_numeric . It is safe to have scalar_numeric * variable. However, expression * scalar_numeric * variable is not always safe, since expression * scalar_numeric might be non-scalar and non-scalar * variable could be a problem...
%Variables
g=5/3;
g1=(g-1)/2;
g2=(1-g)/2;
Ac=1.267*10^-4;
Pc=400:600; % Pc range from 400 to 600
Pcc= 400; %Variable check
T0=350;
P0=615;
Rg=207.85;
Tcc = T0*(Pcc/P0)^((g-1)/g)-1 %Check at Pcc=400
Tcc = 293.6741
mc= sqrt((1/g1)*((P0/Pcc)^((g-1)/g)-1))%Check at Pcc=400
mc = 0.7505
mdotc = (Pcc/(sqrt(Tcc))*mc*sqrt(g/Rg)*Ac)%Check at Pcc=400
mdotc = 1.9875e-04
Tc = T0.*(Pc./P0).^((g-1)./g)-1;
m = sqrt((1./g1).*((P0./Pc).^((g-1)./g)-1));
mdot = (Pc./(sqrt(Tc)).*m.*sqrt(g./Rg).*Ac);
plot(Pc,mdot)
xlabel('Background Pressure (Pc)')
ylabel('Mass flow rate (kg/s)')

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by