I´m trying to solve some examples from a book that are written in MATLAB code in Scilab.
num = [1 0];
den = [1 14 56 160];
[A,B,C,D] = tf2ss(num,den)
I tried copying it as it is in Scilab but I get the following error:
Wrong number of output arguments
While in MATLAB your inputs for tf2ss should be coefficients of polynomials, in Scilab, the only input should be a rational matrix, as you can see in the help page. Also, in MATLAB, the outputs are the ABCD matrices of the system, but Scilab returns a object of type state-space. If you want the ABCD matrices, you need to use abcd.
You can create the polynomials using Scilab's %s as variable:
num = 1*%s + 0;
den = 1*%s^3 + 14*%s^2 + 56*%s + 160;
sl = tf2ss(num/den);
[A,B,C,D] = abcd(sl);
Related
I have an equation of the following form:
where A,B,C, and q are 3-by-3 matrix and Tr[...] represent trace. And
here b is a constant. The explicit form of A,B(x,y,E),C(x,y,E), q(x,y) matrices is written in the below MATLAB code. I am trying to solve it using the integral3() function of MATLAB. But it is giving me errors.
I wrote the function for the integrant in two different ways. And run the script:
integral3(#fun1,-pi,pi,-pi,pi,-inf,inf)
function file 1:
function out = fun1(x,y,E)
%=============just some constants==========
DbyJ = 2/sqrt(3);
T = 1e-2;
eta = 1e-3;
b = 1/T;
D = 1+1i*DbyJ;
fk1 = 1+exp(1i*x);
fk2 = 1+exp(1i*y);
fk1k2 = 1+exp(1i*(x-y));
%=============Matrices==========
A = eye(3); A(1,1) = 1;
q = [0, 1i*D*exp(-1i*x), 0 ;
-1i*conj(D)*exp(1i*x), 0,-1i*D*exp(1i*(x-y));
0,1i*conj(D)*exp(1i*(y-x)),0];
h = [0 -D*conj(fk1) -conj(D)*conj(fk2);
-conj(D)*fk1 0 -D*fk1k2;
-D*fk2 -conj(D)*conj(fk1k2) 0];
B = ((E-1i*eta)*eye(3) - h)^(-1);
C = conj(B);
Term1 = A*(B-C)*q*(B-C);
trc = trace(Term1);
N = -b*exp(b*E)/((exp(b*E)-1)^2);
out = trc*E*N;
It gave me the following error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in fun1 (line 19)
q = [0, 1i*D*exp(-1i*x), 0 ;
Then I tried to solve Tr[...] part symbolically and removed the matrices from integrant. The function file is very large for this, so, I am not putting it here. Anyway, it give me error that
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in fun1 (line 33)
trc = (D*exp(-x*1i)*((exp(conj(x ... (it is a very long expression that I calculated symbolically to remove matrices.)
Question:
Why integral3() is not working?
Is there any other way to solve this kind of integrals numerically? (In Python or in any other software/language).
Thank you for reading.
TLDD:
How can I solve the above given integral numerically?
I'm trying to solve the system below in Matlab. This system is a discrete system. I need to convert to a state space model system, to extract 4 matrices. Then find the transfer function.
y(k+2) + 4y(k+1) + 5y(k)= u(k+2)+2u(k+1)+u(k).
I solved this by hands and I found the four matrices:
A=[0,1:-5,-4]
B=[-2;4]
C=[1,0,0]
D=[1]
My problem is when I try to run my below code I got this error:
Error using ss2tf (line 26)
The A and C matrices must have the same number of columns.
Error in no1 (line 5)
[N1,D1]=ss2tf(A,B,C,D,1);
My Matlab code:
A=[0,1;-5,-4];
B=[-2;4];
C=[1,0,0];
D=[1];
[N1,D1]=ss2tf(A,B,C,D,1);
H=tf(N1,D1)
I expect to get a transfer function
Don't forget that you are dealing with a discrete-time system (add 1as third argument to ss2tf). If you correct the C matrix as already noticed in the comment, then the following code will do what you want:
A = [0,1;-5,-4];
B = [-2;4];
C = [1,0];
D = 1;
[N1,D1] = ss2tf(A,B,C,D);
H = tf(N1,D1,1)
H =
z^2 + 2 z + 1
-------------
z^2 + 4 z + 5
i'm beginner in MATLAB. My problem is :
When i want to take the value of the line in my matrice L(1x2) to stock it in the a and b variables, I tried to use the following code
[a,b]=L;
It didn't work well. I don't understand why it's not possible like this. I used this syntax for the line ginput(n). I used another method to take the value but I want to understand my error in the code above.
My final code is this :
clf();
n=10;
axis([0 10 0 10]);
[px,py] = ginput(n);
Y = py';
X = ones(1,n);
X=[X ; px'];
L= Y*pinv(X);
a = L(1,1);
b = L(1,2);
x = 0:0.2:10;
plot(x,b*x+a, px,py,'r+');
grid;
Thanks a lot
You cannot assign in MATLAB using
[a,b]=L
Instead, you can do the following:
L=[1,2];
L=num2cell(L);
[a,b]=deal(L{:});
Then a=1 and b=2.
num = zeros(1,freq);
den = zeros(1,freq);
for R = 1:freq
[num(R), den(R)]=butter(4, [0.1 0.9]);
end
I thought it was quite trivial but once I run it, I get:
In an assignment A(I) = B, the number of elements in B and I must be the same.
What am I doing wrong?
What you are doing wrong is that both num and den will contain multiple coefficients:
[b,a] = butter(n,Wn) returns the transfer function coefficients of an nth-order lowpass digital Butterworth filter with normalized cutoff frequency Wn.
b,a — Transfer function coefficients
row vectors
As copied from the documentation
The way to get your code working would be to either set num and den to a matrix, or to a cell array:
num = zeros(freq,4);
den = zeros(freq,4);
for R = 1:freq
[num(R,:), den(R,:)]=butter(4, [A(R) B(R)]); % matrix
end
for R = 1:freq
[num{R}, den{R}]=butter(4, [A(R) B(R)]); % cell
end
Probably the matrix is better suited for your purposes.
I need to run a simple Monte Carlo varying coefficients on a system of equations. I need to record the solved coefficient of one of the variables each time.
The following gets me results from a single run:
syms alpha gamma Ps Pc beta lambda Pp Sp Ss Dp Ds;
eq1 = -Ss + alpha + 0.17*Ps - 1*Pc;
eq2 = -Sp + beta + 0.2*Pp;
eq3 = -Ds + gamma - 0.2*Ps + 1*Pp;
eq4 = -Dp + lambda - 0.17*Pp + 1*Ps;
eq5 = Ss - Ds;
eq6 = Sp - Dp;
ans1 = solve(eq1,eq2,eq3,eq4,eq5,eq6,'Ps','Pp','Ss','Ds','Sp','Dp');
disp('Ps')
vpa(ans1.Ps,3)
disp('Pp')
vpa(ans1.Pp,3)
disp('Ss')
vpa(ans1.Ss,3)
disp('Ds')
vpa(ans1.Ds,3)
disp('Sp')
vpa(ans1.Sp,3)
disp('Dp')
vpa(ans1.Dp,3)
I will be varying several of the variables (on Ps, Pp, and Pc), and recording the coefficient on Pc in each of the reduced form equations (i.e. the coefficient on Pc that shows up after vpa(ans1.xx)--so in the case above, it would be a 1x6 vector [-0.429,-1.16,-1.07,-1.07,-0.232,-0.429,-1.16]).
I'm very new to MATLAB, but I'm sure I can figure out how to implement the loop code to do the model iterations. What I can't figure out is how to record the vector of coefficients after each iteration. Is there some "accessor" that will give me just the one coefficient for each equation each time?
Something like vpa(ans1.ps.coef(pc)) (which is a total shot in the dark, and it's wrong, but hopefully you get the idea).
There's probably a better way to do this, but this is all I could come up to at this moment.
Step 1:
In order to obtain the coefficient of Pc as a double from ans1.Ps, you can use the subs function, as follows:
subs(ans1.Ps,{alpha,Pc,beta,gamma,lambda},{0,1,0,0,0});
Step 2a:
To get a vector of all coefficients per ans1 expression (say ans1.Ps) you can use something like this:
N=numel(symvar(ans1.Ps)); % obtain number of coefs
cp=num2cell(eye(N)); % create a cell array using unit matrix, so each iteration a different coef will be selected
for n=1:N;
coefs(n)=subs(ans1.Ps,{alpha,Pc,beta,gamma,lambda},cp(n,:));
end
Step 2b:
Alternatively, you want to get just Pc, but from all the ans1 expressions. If so then you can do the following:
SNames = fieldnames(ans1); % get names of ans1 expressions
for n = 1:numel(SNames)
expr = ans1.(SNames{n}); % get the expression itself
pc(n)=subs(expr,{alpha,Pc,beta,gamma,lambda},{0,1,0,0,0}); % obtain just pc
end
You can now combine the two if you want all info about the coefficients.
Edit:
To store the retrieved Pc per iteration you can do the following:
alpha=[3 1 4 6 7] % just a vector of values
beta = [6 7 8 5 2]
SNames = fieldnames(ans1); % get names of ans1 expressions
for n = 1:numel(SNames)
expr = ans1.(SNames{n}); % get the expression itself
for n1=1:numel(alpha)
for n2=1:numel(beta)
pc(n,n1,n2)=subs(expr,{alpha,Pc,beta,gamma,lambda},{alpha(n1),1,beta(n2),0,0})
end
end
end