iteration in the loop but the result doesn't change ; help

1 view (last 30 days)
Hey every one :)
I have a small problem with my script .
I change the pression_max and V(j) for each iteration but the I have the same U in the end
Any one can help me please
Thanks
*****************************************************************
cte_Rdg_standard=0.011
cte_Rdg_premium=0.008
pression_max=[2 3 6 10 15 23 30 50 75 100 300 450 750 800]
Q_RPT_Span=100
V=[0.15*Q_RPT_Span , 0.3*Q_RPT_Span, 0.5*Q_RPT_Span , Q_RPT_Span]; % vecteur util lors de l'utilisation des Q_RPT Premium
Gauge_23=23;
Gauge_50=50;
Gauge_500=500;
Gauge_1000=1000;
U=ones(length(pression_max),1);
Up=ones(length(pression_max),1);
Matrice_0=ones(length(pression_max),length(V))
for j=1:1:length(V)
for k=1:1:length(pression_max)
if pression_max(k)<=23
Q_RPT_Span=Gauge_23;
Pressure=pression_max(k);
%V=[0.15*Q_RPT_Span , 0.3*Q_RPT_Span, 0.5*Q_RPT_Span , Q_RPT_Span];
Q_RPT_AutoRanged_Span=V(j) ;
if Q_RPT_AutoRanged_Span==Q_RPT_Span
for i=1:1:length(Pressure)
if (0.3*Q_RPT_Span <= Pressure(i)) && (Pressure(i) <= Q_RPT_Span)
Up(i)=(cte_Rdg_premium/100).*Pressure(i);
else
Up(i)=0.3*(cte_Rdg_premium/100).*Q_RPT_Span;
end
end
end
if (0.3*Q_RPT_Span < Q_RPT_AutoRanged_Span) && (Q_RPT_AutoRanged_Span < Q_RPT_Span)
for i=1:1:length(Pressure)
if (0.3*Q_RPT_AutoRanged_Span <= Pressure(i)) && (Pressure(i) <= Q_RPT_Span)
Up(i)=(cte_Rdg_premium/100).*Pressure(i);
else
Up(i)=0.3*(cte_Rdg_premium/100).*Q_RPT_AutoRanged_Span;
end
end
end
if Q_RPT_AutoRanged_Span<=0.3*Q_RPT_Span
for i=1:1:length(Pressure)
if (0.09*Q_RPT_Span <= Pressure(i))% && (Pressure(i) <= Q_RPT_AutoRanged_Span)
Up(i)=(cte_Rdg_premium/100).*Pressure(i);
else
Up(i)=0.09*(cte_Rdg_premium/100).*Q_RPT_Span;
end
end
end
U(k)=Up(i);
U;
end
if pression_max(k)>23 && pression_max(k) <= 50
Q_RPT_Span=Gauge_50;
if (0.3*Q_RPT_Span <= pression_max(k)) && (pression_max(k) <= Q_RPT_Span)
U(k)=(cte_Rdg_standard/100).*pression_max(k);
else
U(k)=0.3*(cte_Rdg_standard/100).*Q_RPT_Span;
end
end
if pression_max(k)>50 && pression_max(k) <= 500
Q_RPT_Span=Gauge_500;
if (0.3*Q_RPT_Span <= pression_max(k)) && (pression_max(k) <= Q_RPT_Span)
U(k)=(cte_Rdg_standard/100).*pression_max(k);
else
U(k)=0.3*(cte_Rdg_standard/100).*Q_RPT_Span;
end
end
if pression_max(k)>500 && pression_max(k) <= 1015
Q_RPT_Span=Gauge_1000;
if (0.3*Q_RPT_Span <= pression_max(k)) && (pression_max(k) <= Q_RPT_Span)
U(k)=(cte_Rdg_standard/100).*pression_max(k);
else
U(k)=0.3*(cte_Rdg_standard/100).*Q_RPT_Span;
end
end
end
U;
Matrice_0(:,j)=U;
end

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jun 2015
You assign
Pressure=pression_max(k);
so Pressure is going to be a scalar. But then later in multiple places you loop over the elements of Pressure. It is legal to loop over the elements of a scalar, but it tends to suggest that the author of the code may have been confused over the meaning of the variable Pressure.
At the end of that group of loops you have
U(k)=Up(i);
outside of all of the "for i" loops. That is going to rely on the last value that "i" happened to have. Which would be 1. It does work out, but it is confusing. If there is the possibility of Pressure being more than one element long then are you sure that you want to store the last such result?
Using array indexing for scalars that must be scalars is legal but is going to lead to people like me asking "Is the reason you always get the same U because you are confused about the length of what you are processing?"

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!