現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
If statement didnt work correctly
5 ビュー (過去 30 日間)
古いコメントを表示
Mariam Gasra
2019 年 3 月 26 日
If i run the program if p less than min the result must be equal to zero but in this programme the result equal to min can any one correct the program
clc;
unit=input('input the number of unit:');
D=input('input total load :');
dP=D;
Bdat1=input('transmission losses considered matrix:');
B=Bdat1;
dB=diag(B);
data1=input('input the data:n a b c min max');
DA=array2table(data1,'V',{'Unit' 'a' 'b' 'c' 'Pl' 'Ph'});
x=max(DA.b);
n=input('insert number of iteration:')
for i=1:n
while abs(dP)>0.00001
P=(x-DA.b)./(2*(DA.c+x*dB));
P=min(P,DA.Ph);
P=max(P,DA.Pl);
dP=D+P'*B*P-sum(P);
x=x+dP*2/(sum(1./DA.c));
end
end
if P<min(P,DA.Ph)
P=0;
P=P+P;
else
P=P;
end
if P>max(P,DA.Pl)
P=0;
P=P+P;
else
P=P;
end
C=DA.a+DA.b.*P+DA.c.*P.*P;
totalCost=sum(C);
display(totalCost);
lamda=x;
display(lamda);
Loss=P'*B*P;
display(Loss);
table(DA.Unit,P,C,'V',{'Unit' 'Power' 'Cost'})
19 件のコメント
Stephan
2019 年 3 月 26 日
Attaching pictures of code is useless. provide code we can copy and paste in Matlab.
Walter Roberson
2019 年 3 月 26 日
I have a suspicion that P becomes a vector or matrix rather than a scalar, but we would need the actual code (instead of an image of the code) and we would need to know what inputs to use.
James Tursa
2019 年 3 月 26 日
What are the sizes of the variables involved? In particular, which ones are scalars and which ones are vectors/matrices/arrays?
Mariam Gasra
2019 年 3 月 26 日
編集済み: Walter Roberson
2019 年 3 月 26 日
Unit=3
Total load=850
Bdat1=[0.00003 0 0;0 0.00009 0;0 0 0.00012]
data1=[605 7.92 0.001562 500 600; 310 7.785 0.00194 100 400;78 7.97 0.00482 50 300]
n=1
Walter Roberson
2019 年 3 月 26 日
Your code is expecting 6 columns for data1, but your suggested input has only 5 columns for it.
Mariam Gasra
2019 年 3 月 27 日
編集済み: Walter Roberson
2019 年 3 月 27 日
Unit=3
Total load=850
Bdat1=[0.00003 0 0;0 0.00009 0;0 0 0.00012]
data1=[1 605 7.92 0.001562 500 600; 2 310 7.785 0.00194 100 400;3 78 7.97 0.00482 50 300]
n=1
Yasasvi Harish Kumar
2019 年 3 月 27 日
DA.Ph is not less than P, therefore your condition isn't true and P is not equal to 0. Consider changing your input for data1 to satisfy the condition.
Mariam Gasra
2019 年 3 月 27 日
編集済み: Walter Roberson
2019 年 3 月 27 日
Unit=3
Total load=850
Bdat1=[0.00003 0 0;0 0.00009 0;0 0 0.00012]
data1=[605 7.92 0.001562 700 800; 310 7.785 0.00194 100 400;78 7.97 0.00482 50 300]
n=1
Jan
2019 年 3 月 27 日
編集済み: Jan
2019 年 3 月 27 日
The code is meaningless:
P=0;
P=P+P;
else
P=P;
??? While "P=P" does nothing, setting P to 0 and adding it to itself will not change the value of P.
The comparison:
if P<min(P,DA.Ph)
is not smart also: min(P, X) replies the minimum of both values. Comparing P with something, which is the minimum of P and a value, is the same as comparing P with this value:
if P < DA.Ph
This means, that you can replace:
if P<min(P,DA.Ph)
P=0;
P=P+P;
else
P=P;
end
if P>max(P,DA.Pl)
P=0;
P=P+P;
else
P=P;
end
by
if P < DA.Ph || P > DA.Pl
P = 0;
end
Mariam Gasra
2019 年 3 月 27 日
Thanks alot But i want in second step add the value of p if its less than min or greater than max to another p's For example if min of p1 500 and the value of p1 get 400 I wannt p1 equal to zero and the 400 added to another p which is p2 and p3
Jan
2019 年 3 月 27 日
@Mariam: Then simply implement this. I do not understand, what this means:
"I wannt p1 equal to zero and the 400 added to another p which is p2 and p3"
Mariam Gasra
2019 年 3 月 27 日
If i run the program there is error Operands to the and && operators must be convertible to logical scalar VaLues
Walter Roberson
2019 年 3 月 27 日
Your P is a vector 3 x 1 (at least for the parameters you posted in https://www.mathworks.com/matlabcentral/answers/452567-if-statement-didnt-work-correctly#comment_686298 )
You have tests comparing P to various values. With P being a vector, those are vector comparisons producing a vector result of logical values. When you use if or while with a non-scalar, the test is considered to succeed if all of the values are non-zero (which would correspond to all of the values being true for logical tests.) Thus
if P<min(P,DA.Ph)
is interpreted by MATLAB as
if all(P<min(P,DA.Ph))
Are you sure that is what you mean?
Perhas what you would want is
P(P < DA.Pl | P > DA.Ph) = 0;
Mariam Gasra
2019 年 3 月 27 日
Still i have same problem if p less than min the output equal to min value of p but i want it to be equal to zero
Mariam Gasra
2019 年 3 月 28 日
in this example P1=435.2
P2=299.97 P3=130.66
if min of P1=500
thhen by if statment condition p=0 and the value of P1 "435.2" add it to P2 and P3
Jan
2019 年 3 月 28 日
@Mariam: The variables P1, P2 and P3 do not occur anywhere in your code, so what is the meaning of providing their values here?
Another hint:
for i=1:n
while abs(dP)>0.00001
P=(x-DA.b)./(2*(DA.c+x*dB));
P=min(P,DA.Ph);
P=max(P,DA.Pl);
dP=D+P'*B*P-sum(P);
x=x+dP*2/(sum(1./DA.c));
end
end
The "for i=1:n" loop is meaningless here. In the first iteration for i==1 the inner loop runs until dP has the wanted size. In all following loops i=2:n the inner loop is not entered anymore, because the value of dP is not modified. So you can omit the for loop completely.
There is a language problem also: I do not understand the meaning of "by if statment condition p=0 and the value of P1 "435.2" add it to P2 and P3".
Without meaningful and exhaustive comments it is impossible to guess the purpose of the code. Please cleanup the code and post it again. Otherwise "Still i have same problem" is not clear enough to be answered.
Mariam Gasra
2019 年 3 月 28 日
If I enter the above data i get3 value of P : 435.2, 299.97,130.66 If min of first p =500 Then first p should equal to zero by if statement conditions
For loop it's for number of unit
Jan
2019 年 5 月 3 日
@Mariam: You still did not mention, what the problem is. Posting a bunch of unclear question might decrease the interest of the forum in reading your questions.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
タグ
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)