check variable within 5% of a fixed value

2 ビュー (過去 30 日間)
Bahiran Adnan
Bahiran Adnan 2019 年 2 月 6 日
編集済み: Jan 2019 年 2 月 6 日
checkp5=1;
while checkp5<2
if prms2>=pstd2*0.95
if prms2<=pstd2*1.05
P;
Q;
T;
checkp5=2;
else
alphap=alphap*(pstd2/prms2);
P=((E/(1+v))*a_barjk'*matrixp)/(a_barjk'*a_barjk+alphap*(c'*c));
pmisfit=matrixp-((1+v)/E)*(a_barjk*P);
prms2=(1/20)*sum(pmisfit.^2,'all');
end
end
end
I need a code to check if prms2 is within 5% of pstd2(which is already fixed value) if not, P, pmisfit & prms2 will be recalculated until it falls within the 5% range

採用された回答

Jan
Jan 2019 年 2 月 6 日
編集済み: Jan 2019 年 2 月 6 日
prms2 = inf;
limit = abs(pstd2) * 0.05;
while abs(prms2 - pstd2) < limit
alphap = alphap*(pstd2/prms2);
P = ((E/(1+v))*a_barjk'*matrixp)/(a_barjk'*a_barjk+alphap*(c'*c));
pmisfit = matrixp-((1+v)/E)*(a_barjk*P);
prms2 = (1/20)*sum(pmisfit.^2,'all');
end
Avoid lines, which do not perform anything like:
P;
Q;
T;
Sometimes unnecessary parentheses are useful, if they clarify the intention. Spaces around operators can be nice. Compare:
alphap = alphap * pstd2 / prms2;
P = E / (1+v) * a_barjk' * matrixp / ...
(a_barjk' * a_barjk + alphap * (c' * c));
pmisfit = matrixp - (1 + v) / E * a_barjk * P;
prms2 = sum(pmisfit .^ 2, 'all') / 20;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by