I write code below but i find an error that I do not know what it is please help me
the error is
Error using idmodel/sim (line 114)
The simulation input data must be specified using an iddata object or a double
matrix.
Error in Untitled (line 17)
y = sim(sys,u);
clc;
clear all ;
close all;
A = [1 -0.5 0.06];
B = [5 -2];
C = [1 -0.2 0.001];
Ts = 1; %sample time
sys = idpoly(A,B,C,'Ts',1);
Range = [-1 1];
Band = [0 1];
u = stairs(idinput(100,'prbs',Band,Range)); %form a prbs input
opt1 = simOptions('AddNoise',true);
y = sim(sys ,u,opt1);
iodata = iddata(y,u,Ts);
na = 3; nb = 2; nc = 3; nk = 1;
me = armax(iodata,[na,nb,nc,nk]);
compare(iodata,me)
thank you very much
Your input variable u, should be a column vector, but with your code it is a graphics object, use class(u) to check this. If you replace this line
u = stairs(idinput(100,'prbs',Band,Range)); %form a prbs input
with something like this:
u = [zeros(25, 1); ones(25, 1)]; % step input
Then the code no longer crashes.
Related
I am trying to write a code for the colculate autocorrelation function. Could you suggest what I'm doing wrong.
Thanks in advance
i = 1:199;
U =sin(0.3*i);
sum_1 = 0;
for tau = 1:length(U)
for t = tau:length(U)-1
sum_1 = sum_1+sum(U(t).*U(t+1-tau));
r(tau)=sum_1;
end
end
You can do something like this. This is an implementation of the original algorithm with only a small optimization; calculating half the sequence and copying the remaining half.
n = length(x);
m = 2*n - 1;
rxx = zeros(1,m);
for i = 1:length(x)
rxx(i) = dot(x(n-i+1:n), x(1:i).');
rxx(m-i+1) = rxx(i)';
end
Example:
x = [2, 3, -1];
rxx =
-2 3 14 3 -2
% MATLAB built-in
xcorr(x,x)
ans =
-2 3 14 3 -2
For a state space equation in which matrix A is dependent on variable t(time), how can I get the step or output response?
This is the code, which doesn't work:
A = [sin(t) 0;0 cos(t)];
B = [0.5; 0.0];
C = [1 0; 0 1];
G = ss(A,B,C,[]);
step(G,t)
x0 = [-1;0;2];
initial(G,x0)
Here are error message:
Error using horzcat Dimensions of matrices being concatenated are not
consistent.
Error in Response (line 11) A = [sin(t) 0;0 cos(t)];
As pointed already, you can only use the ss function to generate LTI systems, but you can discretise your model analytically using methods like forward Euler, backwards Euler, Tustin etc. and simulate your model using a for loop.
For your example, you could run something like this:
consider a sampling period h = 0.01 (or lower if the dynamics are not captured properly);
select N as the number of steps for the simulation (100, 1000 etc.);
declare the time instants vector t = (0:N-1)*h;
create a for loop which computes the system states and outputs, here using the forward Euler method (see https://en.wikipedia.org/wiki/Euler_method):
% A = [sin(t) 0;0 cos(t)];
B = [0.5; 0.0];
C = [1 0; 0 1];
D = [0; 0];
x0 = [-1;1]; % the initial state must have two elements as this is a second-order system
u = 0; % constant zero input, but can be modified
N = 1000;
h = 0.01;
t = (0:N-1)*h;
x_vec = [];
y_vec = [];
xk = x0;
yk = [0;0];
for k=1:N
Ad = eye(2)+h*[sin(t(k)) 0; 0 cos(t(k))];
Bd = h*B; % C and D remain the same
yk = C*xk + D*u;
xk = Ad*xk + Bd*u;
x_vec = [x_vec, xk]; % keep results in memory
y_vec = [y_vec, yk];
end
% Plot output response
plot(t,y_vec);
I am trying to solve this Mixed integer Linear programming problem using MATLAB. However, I am having issues with the definition of summation of linear objective function and decision variable in matlab. I am using the intlinprog function in MATLAB. Please find image link below.
Please also find a snippet of my code written in MATLAB. I am getting an error as I observed "fun" cannot be used with MATLAB intlinprog function.
for i = 1: length(bt)
f(i,1) = bt(i) + nch*w;
f(i,2) = (w/nch) - st;
f(i,3:5) = 0;
end
M = [4,5,9,10,14,15,19,20,24,25,29,30,34,35,39,40,44,45,49,50,54,55,59,60,64,65,69,70,74,75];
e = 0.00001;
A = [0 0 0 1 1;
-nch 1/nch 1 0 0];
ACell = repmat({A}, 1, 15);
A = blkdiag(ACell{:});
B = [1; 0.7];
ub = ones(5,1);
ub(1:2,1) = 2.3;
ub = repmat(ub, length(bt),1);
lb = zeros(5,1);
lb(3,1) = 0.01;
lb = repmat(lb,length(bt),1);
f = reshape( f.' ,1,numel(f))';
B = repmat(B,length(bt),1);
Opt = opti('f',f,'ineq',[],[],'eq',A,B,'bounds',lb,ub,'xtype',M)
[x,fval,exitflag,info] = solve(Opt)
Given the following PDE (non-homogenous heat equation):
ut(x,t) = c2uxx(x,t) + f(x,t)
u(0,t) = u(l,t) = 0
u(x,0) = g(x)
0 < x < l ; t > 0 ; c > 0
I wrote the following code in Matlab, to solve the problem using finite differences:
syms xj tk
% Manually define this values
c = 9;
f(xj,tk) = xj;
g(xj) = 0*xj;
l = 1;
Tmax = 0.1;
% Grid definition
Nx = 50;
Nt = 50;
hx = 1/Nx;
ht = 1/Nt;
x = 0:hx:l;
t = 0:ht:Tmax;
lambda = c^2*ht/hx^2;
% Our target
u = zeros(Nx+1,Nt+1);
% Initial values
for j=1:Nx,
u(j,1) = g(x(j)); % u(x,0) = g(x)
end
for k=1:Nx,
u(1,k+1) = 0; % border condition u(0,t) = 0
for j=2:Nt,
u(j,k+1) = u(j,k) + lambda*(u(j+1,k)-2*u(j,k)+u(j-1,k)) + ht*f(j,k); % the formula here is ok
end
u(Nt,k+1) = 0; % border condition u(l,t) = 0
end
contour3(u)
For some reason that I cant't figure out, data is only appearing in the last columns and in a very strange way.
I'm guessing the implementation of the BC's are doing something nasty. But I don't see it.
Is there something that I'm missing?
Thanks in advance!
I try to implement image compression using Burrows-Wheeler transform. Consider an 1D matrix from path scanning is:
p = [2 5 4 2 3 1 5];
and then apply the Burrows-Wheeler transform :
function output = bwtenc(p)
n = numel(p);
x = zeros(length(p),1);
for i = 1:length(p)
left_cyclic = mod(bsxfun(#plus, 1:n, (0:n-1).')-1, n) + 1;
x = p(left_cyclic);
end
[lex ind] = sortrows(x);
output = lex(:,end);
output = uint8(output(:)');
end
And it works! But the problem is when i try to implement 1D matrix from Lena.bmp which the size is 512*512, Error message showing that bsxfun is out of memory. Anyone please help me.
See if this works for you -
function output = bwtenc(p)
np = numel(p);
[~,sorted_ind] = sort(p);
ind1 = mod((1:np)+np-2,np)+1;
output = p(ind1(sorted_ind));
output = uint8(output(:)');
end