how to creat if loop?
2 ビュー (過去 30 日間)
古いコメントを表示
function [B]=BusData
% ---------------------------------data input------------------------------
%bus number| Bus type | voltage schedule | Pgen | Pload | Qload
syms Swing
syms Gen
syms Load
syms Pg
syms Pl
syms Ql
B =[1 Swing 1.05 Pg Pl Ql
2 Gen 1.05 0.5 0 0
3 Gen 1.07 0.6 0 0
4 Load 0 0 0.7 0.7
5 Load 0 0 0.7 0.7
6 Load 0 0 0.7 0.7]
end
%-----------------------------------------------------program strat here-------------------------------------------------------------
%Bus parameters:
bn = B(:,1);
bt = B(:,2);
vs = B(:,3);
Pgen = B(:,4);
Pload = B(:,5);
Qload = B(:,6);
mat %mat is 6*6 matrix
E=inv(mat)
%[P1;P2;...;Pn-1]=C
%[q1;1q2;...;qn-1]=D
C=E*D
syms Swing
for i=1:bn
if bt==Swing
E(i,:) = [];
E(:,i) = [];
end
end
disp(E)
and i want to do this for C & D and by the way n depends on which row of B is Swing(so if bn=2 is Swing then delete second row and second column if nb=1 is swing delete first row and column of mtarix E if... ) but i want this code to be general and i have tried many things but still confused and not getting my ans...
0 件のコメント
採用された回答
Walter Roberson
2022 年 1 月 9 日
Your function does not expect any input parameters.
Your function does not define a variable named mat . Therefore in order for the line
mat %mat is 6*6 matrix
to not be an error, then mat would have to be a function that you are invoking.
Then you do
E=inv(mat)
which is not inconsistent with the possibility that mat is a function... as long as the function returns a square matrix.
C=E*D
You have not defined any variable named D so in order for that line to be valid, D must be a function that you are invoking.
if bt==Swing
You have not defined any variable named bt so in order for that line to be valid, bt must be a function that you are invoking
mat(n,:) = [];
mat(:,n) = []
You have not defined any variable named n so in order for that line to be valid, n must be a function that you are invoking.
However... we have established that mat must be a function. It is potentially valid to invoke a function with two parameters, one of which is : . However, it is never valid for the output of a function to be the destination for assignment or deletion using [] .
2 件のコメント
Walter Roberson
2022 年 1 月 9 日
driver()
function driver
syms Swing
syms Gen
syms Load
syms Pg
syms Pl
syms Ql
B =[1 Swing 1.05 Pg Pl Ql
2 Gen 1.05 0.5 0 0
3 Gen 1.07 0.6 0 0
4 Load 0 0 0.7 0.7
5 Load 0 0 0.7 0.7
6 Load 0 0 0.7 0.7]
%-----------------------------------------------------program strat here-------------------------------------------------------------
%Bus parameters:
bn = B(:,1);
bt = B(:,2);
vs = B(:,3);
Pgen = B(:,4);
Pload = B(:,5);
Qload = B(:,6);
mat() %mat is 6*6 matrix
E = inv(mat)
E = [
-0.4296 0.2606 -0.0183 0.2978 -0.3535 0.0347
0.2606 -0.2000 -0.0034 -0.0973 0.3342 -0.0480
-0.0183 -0.0034 -0.0359 -0.0401 0.0732 0.0791
0.2978 -0.0973 -0.0401 -0.1889 0.3016 -0.0705
-0.3535 0.3342 0.0732 0.3016 -0.8023 0.1629
0.0347 -0.0480 0.0791 -0.0705 0.1629 -0.0564];
n = find(ismember(bt, Swing));
mat(n,:) = []
mat(:,n) = []
function MAT = mat()
MAT = magic(6);
end
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Symbolic Variables, Expressions, Functions, and Preferences についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!