how to creat if loop?

조회 수: 1 (최근 30일)
arian hoseini
arian hoseini 2022년 1월 9일
댓글: Walter Roberson 2022년 1월 9일
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에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by