defining a matrix that can contain symbolic variables
1 次查看(过去 30 天)
显示 更早的评论
hello I have a matrix that have 5 columns that contain 0/1 in each cell and I am interested to add a column that contains expressions that contain symbolic expressions, but I can't do it.
clear all
close all
clc
syms P R F Rs t
F=0.01*t; %represents the Failure probability of a component and is defined only in the range t<=100
R=1-0.01*t; %represents the Surviving probability of a component and is defined only in the range t<=100
P=[];
n=5; %ne is the number of components in the complex system
power=n;
M=zeros(2^n,n+1); %M represents the combination Matrix - each row represents a possible combination and in addition 2 outputs: system filure/surviving and the probability of such event.
V_temp=zeros(1,n);
%creating the combination matrix
for counter1=1:1:n %RA RB RC RD RE
value=0;
power=power-1;
num=2^(power);
stage=1;
for counter2=1:1:(2^n) %Row 1 - Row 32
if (counter2>num*stage)
stage=stage+1;
if (value==0)
value=1;
else
value=0;
end
end
M(counter2,counter1)=value;
end
end
%now ae are dealing with the first output of each row (possible combination) - we determine if the system fails or survives based on the state of each component and the architecture of the system
V_check=[];
for counter3=1:1:(2^n)
if ((M(counter3,2)==1)&&(M(counter3,3)==1)&&(M(counter3,4)==1))%if B&C&D=1
M(counter3,n+1)=1; %we mark this combination as a surviving combination (we proved that at least one branch B-C-D is operative)
else %if B or C or D (or some of them) equals to 0 - so now we have a case that the first branch isn't operative and we have to check the backup possibilities
if M(counter3,2)==0 %checking if the first element in the B-C-D branch, B, equals to 0
if M(counter3,1)==0 %if A=0 - no backup to B - System Failure
V_check(1)=0; %we mark this combination as a failure
else
V_check(1)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
else %in case B=1
V_check(1)=1; %we temporarily mark this combination as a success - we still have to make sure that C/A/E and D/E is operative
end
if M(counter3,3)==0 %if C=0
if((M(counter3,1)==0)&&(M(counter3,5)==0)) %if A&E=0 - no backup to C and hence a system failure
V_check(2)=0; %we mark this combination as a failure
else % in case A or E (or both of them) equals to 1
V_check(2)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
else %if C=1
V_check(2)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
if M(counter3,4)==0 %if D=0
if M(counter3,5)==0 %if E=0 - no backup to D
V_check(3)=0; %we mark this combination as a failure
else %if E=1 - we have a backup to D
V_check(3)=1; %we temporarily mark this combination as a success
end
else %if D=1
V_check(3)=1; %we temporarily mark this combination as a success
end
if ((V_check(1)*V_check(2)*V_check(3))==0) %in case that the main branch B-C-D isn't perfectly working we started moving on from component to component (B,C,D) and making sure that it works (or have a backup) and marked the temporary check V_check() as 1 or 0. we finally want to make sure that all the components have a backup and hence multiplying all the V_check() and hoping to get a survival (1)
M(counter3,n+1)=0; %we mark this combination as a failure combination
else
M(counter3,n+1)=1; %we mark this combination as a surviving combination
end
end
end
%calculating probability for each case
for counter4=1:1:(2^n)
num_of_1=sum(M(counter4,:)); %we sum up all the survivals in each row
num_of_0=n-num_of_1; %we sum up all the failures in each row
P(counter4)=((1-0.01*t)^num_of_1)*((0.01*t)^num_of_0); %we calculate the probability for each combination/row
end
0 个评论
采纳的回答
madhan ravi
2018-10-26
编辑:madhan ravi
2018-10-26
clear all
close all
clc
syms P R F Rs t
F=0.01*t; %represents the Failure probability of a component and is defined only in the range t<=100
R=1-0.01*t; %represents the Surviving probability of a component and is defined only in the range t<=100
% P=[];
n=5; %ne is the number of components in the complex system
power=n;
M=sym(zeros(2^n,n+1)); %ERROR HERE %M represents the combination Matrix - each row represents a possible combination and in addition 2 outputs: system filure/surviving and the probability of such event.
V_temp=sym(zeros(1,n)); %ERROR HERE
%creating the combination matrix
for counter1=1:1:n %RA RB RC RD RE
value=0;
power=power-1;
num=2^(power);
stage=1;
for counter2=1:1:(2^n) %Row 1 - Row 32
if (counter2>num*stage)
stage=stage+1;
if (value==0)
value=1;
else
value=0;
end
end
M(counter2,counter1)=value;
end
end
%now ae are dealing with the first output of each row (possible combination) - we determine if the system fails or survives based on the state of each component and the architecture of the system
V_check=[];
for counter3=1:1:(2^n)
if ((M(counter3,2)==1)&&(M(counter3,3)==1)&&(M(counter3,4)==1))%if B&C&D=1
M(counter3,n+1)=1; %we mark this combination as a surviving combination (we proved that at least one branch B-C-D is operative)
else %if B or C or D (or some of them) equals to 0 - so now we have a case that the first branch isn't operative and we have to check the backup possibilities
if M(counter3,2)==0 %checking if the first element in the B-C-D branch, B, equals to 0
if M(counter3,1)==0 %if A=0 - no backup to B - System Failure
V_check(1)=0; %we mark this combination as a failure
else
V_check(1)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
else %in case B=1
V_check(1)=1; %we temporarily mark this combination as a success - we still have to make sure that C/A/E and D/E is operative
end
if M(counter3,3)==0 %if C=0
if((M(counter3,1)==0)&&(M(counter3,5)==0)) %if A&E=0 - no backup to C and hence a system failure
V_check(2)=0; %we mark this combination as a failure
else % in case A or E (or both of them) equals to 1
V_check(2)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
else %if C=1
V_check(2)=1; %we temporarily mark this combination as a success - we still have to make sure that D or E is operative
end
if M(counter3,4)==0 %if D=0
if M(counter3,5)==0 %if E=0 - no backup to D
V_check(3)=0; %we mark this combination as a failure
else %if E=1 - we have a backup to D
V_check(3)=1; %we temporarily mark this combination as a success
end
else %if D=1
V_check(3)=1; %we temporarily mark this combination as a success
end
if ((V_check(1)*V_check(2)*V_check(3))==0) %in case that the main branch B-C-D isn't perfectly working we started moving on from component to component (B,C,D) and making sure that it works (or have a backup) and marked the temporary check V_check() as 1 or 0. we finally want to make sure that all the components have a backup and hence multiplying all the V_check() and hoping to get a survival (1)
M(counter3,n+1)=0; %we mark this combination as a failure combination
else
M(counter3,n+1)=1; %we mark this combination as a surviving combination
end
end
end
%calculating probability for each case
for counter4=1:1:(2^n)
num_of_1(counter4)=sum(M(counter4,:)); %we sum up all the survivals in each row %ERROR HERE
num_of_0(counter4)=n-num_of_1(counter4); %we sum up all the failures in each row
P(counter4)=(((1-0.01*t)^num_of_1(counter4))*((0.01*t)^num_of_0(counter4))); %we calculate the probability for each combination/row
end
6 个评论
Stephen23
2018-10-26
hey and thank you for replying! please see that I defined the P as syms in the begining and I still get an error. I am attaching a photo:
Stephen23
2018-10-26
another thing please, how can I plot all the results of the symbolic vector so I will see the results? I know that I can use the eval() function, but it worked for me only in cases in which I am trying to plot a 1x1 and not a matrix or vector
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!