I got the error "Solving CHIU requires an initial condition vector of length 651" when running this Matlab file:
TSPAN = (0:1:320);
[ta,xa] = ode15s('Chiu',TSPAN,[0.0258 0 0 0 0 0 0 0 5.88e9*exp(-701/(1.987*T)) 2.95e7*exp(-4353/(1.987*T)) 8.83]);
I really don't get what exactly this error means. What and where is the vector length 651? I only have 11 ODE equation. Can anyone help me?
I beieve, the first input to ode15s should be a functions handle, not the name of the function. So, instead of
[ta,xa] = ode15s('Chiu',TSPAN,...
try
[ta,xa] = ode15s(#Chiu,TSPAN,...
Related
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 am trying to create a turbo encoder for my project.
Till now i have created the convolution encoder of 1/2 rate. Now i am having difficulty to apply interleaver. Here is my code. I am not getting output in ilvr. Correct me where i am going incorrect. Thanks in advance
msg = [0 1 0 1 1 1 0 0 1 0 1 0 0 0 1];
t= poly2trellis(3,[6 7]);
[isok,status] = istrellis(t);
code1 = convenc(msg,t);
ilvr = randperm(msg);
code2 = convenc(ilvr,t);
Well, firstly, if your question is about ilvr then why include the rest of the code other than the definition of msg and the line in question?
Secondly, when I ran this:
msg = [0 1 0 1 1 1 0 0 1 0 1 0 0 0 1];
ilvr = randperm(msg);
I got the following error:
Error using randperm
Size inputs must be scalar.
So, this means the the input is not what randperm was expecting. Then I typed help randperm, and looked at the help for randperm, so now I understand what randperm does and what inputs it expects. The error is because you gave randperm a vector, but the first input must be an integer.
I'm not sure what you are trying to do on that line, maybe you are trying to get a random permutation of the elements in msg? Following a hint in the help page, try this:
ilvr=msg(randperm(numel(msg)));
I want want to do some string calculation using Matlab, and then stored the value in an matrix.
For numerical study, I often predefined the dimensions in Matlab using zeros to create a 4*4 array.
a = zeros(4)
Now I want to do the same thing for the symbolic matrix. Obviously zeros didn't work at this time.
I tried to copy the official tutorial at this page http://www.mathworks.com/help/symbolic/sym.html
a = sym('0' ,4) % error
Still didn't work.
Now I have do use the ugly code like this
a = sym('[0 0 0 0; 0 0 0 0; 0 0 0 0; 0 0 0 0]');
Since I will use iterations, and dimension of the matrix grows every time. This method is not convenient.
Do you have any ideas? Thanks a lot!
Num = sym(Num) converts a number or a numeric matrix Num to symbolic form.
a=sym(zeros(4,4))
Can't try but suspect that the variables get initialized as zero by default.
For example when using
a = sym('a' ,[2 2])
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 have two vectors o and c of equal length:
o = [-1 -1 -1 0 0 0 1 1 0 0];
c = [-1 -1 -1 -1 0 1 1 1 0 -1];
o represents opening signals (neg or pos) and c represents closing signals, assuming an opening signal has preceeded it with opposite sign. Only one signal can be active at a time so consecuitive signals must be ignored. In the two vectors above, my first signal would be in o(1) and its corresponding closing signal would be found in c(6). This also means that the opening signals in o(2) and o(3) should be ignored and my next opening signal is found at o(7) with its corresponding close at c(10), consequently leading to a void signal at o(8)
I am trying to find a vectorized solution to identifying a correct sequence or indices of opened/closed signals to produce something along the lines of the following solution example:
o = [-1 0 0 0 0 0 1 0 0 0];
c = [ 0 0 0 0 0 1 0 0 0 -1];
I can obviously solve this by looping through each element in a for loop but since my dataset can be up to millions of elements and I find looping in Matlab can be rather 'expensive', I would greatly appreciate if someone has a solution to my problem that is more matrix-oriented, or through arrayfun or something equivalent that may make the code more efficient?
You can use diff, along with some logical operations to get your answer.
o=[-1,-1,-1,0,0,0,1,1,0,0];
oFinal=abs(diff([0,o])).*o;
oFinal=
-1 0 0 0 0 0 1 0 0 0
The trick is that the output of diff and your original vector o both have a non-zero value at the same index only for the first occurrence of the value in o (i.e., first occurrence in a chain). So, by multiplying it element-wise with o, you get your answer. The abs is to ensure that a sign change doesn't occur due to the output from diff.
The approach is similar for c, and I'll leave that for you to try :)
Usually looping is not more expensive as performing some other operation which just hiddes the loop behind another function (e.g. arrayfun). From your text it just sounds that you just chose the wrong algorithm. Your problem sounds very linear, that is O(n), but you write about loop in loop which means O(n^2). With millions of elements quadratic runtime is not so nice.
The algorithm you want is something like this:
open = 0;
for i=1:length(o)
if (open == 0)
open=o(i)
else
o(i) = 0;
end
if (c(i) ~= -open)
c(i) = 0;
else
open = 0;
end
end
It maybe needs some finetunig, as you didnt describe in detail e.g. what the order of the c and o signals are (e.g. if the same index opens and closes is first the open processed or the closed, my example code assumes open), or whether the order of the signals is always ok, or if there must be some error treatment - but I guess you get the idea of the single loop.