how to creat if loop?

3 次查看(过去 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...

采纳的回答

Walter Roberson
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 个评论
arian hoseini
arian hoseini 2022-1-9
编辑:Walter Roberson 2022-1-9
you are right ...thats because i didnt write the entire code here so
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];
just tell me what should i do with E
and forget that C=E*D
Walter Roberson
Walter Roberson 2022-1-9
driver()
B = 
Unrecognized function or variable 'mat'.

Error in solution>driver (line 24)
mat() %mat is 6*6 matrix
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,:) = []
Using 'mat' as the name of a variable and the name of a nested function in the same scope is not supported.
mat(:,n) = []
function MAT = mat()
MAT = magic(6);
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by