Hello every one
I have Steady-Space model(Controller) as below:
A =[ *M* ]; B =[0 0 2 0 0 0 0];;
C =[0;2;0]; D =[0 2 0 0 0 0 0 ; 2 0 2 0 0 0 0 ; 0 0 0 *M* 0 2 0]
Controller =ss(A,B,C,D)
This controller have 7 inputs and 3 outputs.
I don't want to use simulink steady-space block to define this controller in it.
As How to change variables in time in Simulink?
I have variable M in my controller that can be changed with time and I want to use
variable signal to this scenario like top linked link.
How can I use user defined blocks to write this variable steady-space controller ?
Which User defined blocks can be use for programming and how?
Need help
Thanks
You can probably use the Matrix Concatenate block to create your D matrix from your M signal , muxed with other constants (0 and 2) to create a vector, which you can then contatenate with 2 other constant vectors to create the matrix. Matrices A, B, and C are constant, so you can then just construct your state-space system from scratch using these 4 matrices, using simple Add, Multiply and Subtract blocks.
Another alternative is to use a MATLAB Function block, taking M as an input, but I don't know if state-space objects are allowed as a data type for the function output. I guess you'd need to compute the state-space output at each time step based on the state-space input. Not sure how you do that with a MATLAB Function block.
Related
I have the following in table in matlab
k ak
0 1
1 -0.166666667
2 0.008333333
... ...
where ak = (-1)^k/(2k+1)! but that's not really important, all that is important is that ak is a function of k and the (-1)^k.
I am trying to generate a new set of columns as below
i ai
0 1
1 0
2 -0.166666667
3 0
4 0.008333333
... ...
So far I've figured out that by stretching my indices (incrementing by 2 instead of 1) and applying the ak function it gets pretty close to what I want but it returns the absolute value.
if mod(i,2)=0
ai=(-1)^i/factorial(2*(i/2)+1);
else
ai=0;
end
How can I change this to be what I want?
You should be using this:
ai = (-1)^(ii/2)/factorial(2*(ii/2)+1);
% Notice ----^
Your if-else statements can be given a vector input and written in one-line as:
ai = ~mod(ii,2) .* (-1).^(ii/2)./factorial(2*(ii/2)+1);
%I used ii instead of i since i is for imaginary numbers in MATLAB
I have a set of independent binary random variables (say A,B,C) which take a positive value with some probability and zero otherwise, for which I have generated a matrix of 0s and 1s of all possible combinations of these variables with at least a 1 i.e.
A B C
1 0 0
0 1 0
0 0 1
1 1 0
etc.
I know the values and probabilities of A,B,C so I can calculate E(X) and E(X^2) for each. I want to treat each combination in the above matrix as a new random variable equal to the product of the random variables which are present in that combination (show a 1 in the matrix). For example, random variable Row4 = A*B.
I have created a matrix of the same size to the above, which shows the relevant E(X)s instead of the 1s, and 1s instead of the 0s. This allows me to easily calculate the vector of Expected values of the new random variables (one per combination) as the product of each row. I have also generated a similar matrix which shows E(X^2) instead of E(X), and another one which shows prob(X>0) instead of E(X).
I'm looking for a Matlab script that computes the Covariance matrix of these new variables i.e. taking each row as a random variable. I presume it will have to use the formula:
Cov(X,Y)=E(XY)-E(X)E(Y)
For example, for rows (1 1 0) and (1 0 1):
Cov(X,Y)=E[(AB)(AC)]-E(X)E(Y)
=E[(A^2)BC]-E(X)E(Y)
=E(A^2)E(B)E(C)-E(X)E(Y)
These values I already have from the matrices I've mentioned above. For each Covariance, I'm just unsure how to know which two variables appear in both rows, because for those I will have to select E(X^2) instead of E(X).
Alternatively, the above can be written as:
Cov(X,Y)=E(X)E(Y)*[1/prob(A>0)-1]
But the problem remains as the probabilities in the denominator will only be the ones of the variables which are shared between two combinations.
Any advice on how automate the computation of the Covariance matrix in Matlab would be greatly appreciated.
I'm pretty sure this is not the most efficient way to do that but that's a start:
Assume r1...n the combinations of the random variables, R is the matrix:
A B C
r1 1 0 0
r2 0 1 0
r3 0 0 1
r4 1 1 0
If you have the vector E1, E2 and ER as:
E1 = [E(A) E(B) E(C) ...]
E2 = [E(A²) E(B²) E(C²) ...]
ER = [E(r1) E(r2) E(r3) ...]
If you want to compute E(r1,r2) you can:
1) Extract the R1 and R2 columns from R
v1 = R(1,:)
v2 = R(2,:)
2) Sum both vectors in vs
vs = v1 + v2
3) Loop in vs, if you see a 2 that means the value in R2 has to be used, if you see a 1 it is the value in R1, if it is 0 do not use the value.
4) Using the loop, compute your E(r1,r2) as wanted.
this is my question:
I have six parametres that I'll vary in fmincon toolbox of MATLAB, and these are the restrictions:
variables=[x1,x2,x3,x4,x5,x6]
x1<=x2
x3<=x4
x5<=x6
So, I defined the A matrix for fmincon like this:
A=[1 -1 0 0 0 0; 0 0 1 -1 0 0; 0 0 0 0 1 -1];
and vector b:
b=[0;0;0];
And the problem is that fmincon doesn't respect the conditions (if they are well defined), I mean por the first iteration, the parameters were:
ddx=[1 1 1 1 1 1];
And for the second iteration the variation was:
ddx=[2 1 1 1 1 1];
As you can see, there's a mistake with respect the definition in the boundaries, because x1=2 and x2=1; which is not right.
I hope you can give me some ideas.
This is an example:
function Example
clc
global xo
lbr=[2,2,300,300,3,3];
upr=[8,8,2000,2000,6,6];
xo =[3,4,400,500,4,5];
Aeq=[];
beq=[];
% Matríz de condiciones
Ain=[1 -1 0 0 0 0;0 0 1 -1 0 0;0 0 0 0 1 -1];
bin=[0;0;0];
lb=lbr./xo;
ub=upr./xo;
xo_n=ones(1,size(upr,2));
options1 = optimset('Display','iter',...
'Algorithm','sqp','PlotFcns',#optimplotfval,...
'MaxIter',400,'MaxFunEvals',1000,'DiffMinChange',1);
[ddx, FOval] = fmincon(#MyfunSSI,xo_n,Ain,bin,Aeq,beq,lb,ub,[],options1);
end
function [FO]=MyfunSSI(ddx)
global xo
ddx
x1=xo(1)*ddx(1);
x2=xo(2)*ddx(2);
x3=xo(3)*ddx(3);
x4=xo(4)*ddx(4);
x5=xo(5)*ddx(5);
x6=xo(6)*ddx(6);
% These are the conditions:
x1<=x2
x3<=x4
x5<=x6
FO=1;
end
As you can see, the restrictions are not respected.
After the function fmincon terminates, it should either satisfy the constraints, or indicated that it cannot find a feasible solution.
That during the optimization iterations constraints are violated, can be advantageous for certain problems, and hence this is supported by some of the algorithms (at least the Active Set algorithm) in fmincon. If you dont like this behavior, use Interior Point. For more details, see fmincon algorithms and violating constraints.
edit The thing that is going wrong is your objective function MyfunSSI. It just returns 1, and hence there is nothing to minimize. In addition, you are just putting the constraint statements, without processing them in any way. The constraints are dealt with by the A and b matrix, so forget about these. But the main thing is: change the function a proper objective function that uses the input ddx to come up with an output value FO.
And by the way, using sqp in this way means that the optimizer will use finite differences and hence evaluate the objective function near feasible and hence possibly infeasible points.
I'm having trouble with this problem since I'm new to matlab
"Use help to learn about the built in function ‘rand’. Write a script to use the rand function to generate a sequence of ‘head’ or ‘tail’ where one is head and zero is tail. The other function that you need to use is ‘round’ to convert the output of the ‘rand’ function to an integer. When we run your function it should display something like this: “T H T T H H H…..”.
I've used the help function and searched online but I still don't understand the random function.
I've used
flip = random('norm',1:10,1)
flip =
1.0774 0.7859 1.8865 3.9932 6.5326 5.2303 7.3714 7.7744 10.1174 8.9109
As you can see, it keeps giving me random numbers. I want my numbers to be either 0 or 1.
I know the 10 in 1:10 will display 10 values, but what does the two 1's mean?
I'd appreciate any help, thanks!
For completeness, there's also the randi function (at least from 2011b onwards):
faceId = randi(2,1,10) % generates random integers between 1 and 2 (inclusively)
faceId =
2 2 2 1 1 2 2 1 1 2
That avoids the need for the < 0.5 comparison and the +1
You can do as follow:
faceId=rand(1,10)<0.5
faceId =
1 1 0 0 1 1 1 0 0 0
faceName='TH';
faceName(faceId+1)
ans =
THHHHTTHTH
I want to train a decision tree in MATLAB for binary data. Here is a sample of data I use.
traindata <87*239> [array of data with 239 features]
1 0 1 0 0 0 1 1 0 0 1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 ... [till 239]
1 1 1 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 1 0 0 0 1 0 1 ... [till 239]
....
The thing is that this data corresponds to a form which has only options for yes/no. The outcome of the form is also binary and has the meaning that a patinet has some medical disorder or not! we have used classification tree and the classifier shows us double numbers. for example it branches the first node based on x137 value being bigger than 0.75 or not! Since we don't have 0.75 in our data and it has no yes/no meaning we wanted to use a decision tree which is best for our work. The best decision tree for us is the one that is trained based on boolean variables not double ones. Also it understands that the data is not continuous and for example instead of above representation shows x137 is yes o no (1 or 0). Can someone help me with this? I would also appreciate a solution to map our data to double variables and features if the boolean decision tree is not appliable. I am currently using classregtree in matlab with <87*237> as train and <87*1> as results.
classregtree has an optional input parameter categorical. Using this option, you can pass in a vector indicating which of your input variables are categorical (in your case, this vector would be 1x239, all ones). The decision tree should then contain yes/no decisions rather than numerical thresholds.
From the help of classregtree:
t = classregtree(X,y) creates a decision tree t for predicting the response y as a function of the predictors in the columns of X. X is an n-by-m matrix of predictor values. If y is a vector of n response values, classregtree performs regression. If y is a categorical variable, character array, or cell array of strings, classregtree performs classification.
What's the type of y in your case? It seems that classregtree is doing regression in your case but you want classification. So, y should be a categorical variable.
EDIT: To make your y categorical, you can try "nominal(y)".