solve matrix of symbolic equations without using loop - matlab

I have an equation denoted as 'a' in the code. I am trying to solve this equation for different values. When I use solve(eq), it does not solve the problem and gives an error. The solution must be in the form 4 by 1, I guess.
syms x y t
a = 3*x^2 + 2*y +7;
b = rand(4,2);
eq = subs(a,{x,y},{b(:,1)*t,b(:,2)*t,})
eq = (114796936454163404704655535315507*t^2)/81129638414606681695789005144064 + (7027663972423957*t)/4503599627370496 + 7
(2049112225923387932688645929427*t^2)/20282409603651670423947251286016 + (730715964868191*t)/4503599627370496 + 7
(8261891359675172983093065745827*t^2)/20282409603651670423947251286016 + (4185582312538139*t)/2251799813685248 + 7
(95262076673210081160325846940403*t^2)/81129638414606681695789005144064 + (1746749665163685*t)/1125899906842624 + 7
solve(eq)
PS: Since I run this code inside a different function, I am trying to save time here by not using loops.

Related

Simplify symbolic expression in matlab and get only the coeeficents

I'm looking for finding the coefficients from the Taylor's series in Matlab. The way that I'm doing is:
% Declare symbolic expression and function:
syms x;
f = exp(x);
% Calculate the taylor expansions in a concrete point:
T = taylor(f, x, 0.5);
% And finally I simplify the expression:
coefs = simplify(T)
But the returned expression is:
Yes, the expression it's simplified, but actually what I want is:
Where each term is multiplied by his coefficient. How can I this? Options suchs as simplify(f, x, 0.5, 10, where 10 refers to the simplification step,doesn't work in my case. Also I've been seeing this question and the problem is the same:
How get to simplify a symbolic and numeric mixed expression in Matlab
Depending on how many digits you want and in what exact format you want the result, here is an example:
>> c = double(coeffs(T))
c =
Columns 1 through 4
0.999966624859531 1.000395979357109 0.498051217190664 0.171741799031263
Columns 5 through 6
0.034348359806253 0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531
EDIT
Note that you could also use vpa( ) instead of converting to double first:
>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840, (233*exp(1/2))/384, (29*exp(1/2))/96, (5*exp(1/2))/48, exp(1/2)/48, exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

Trying to find a transfer function from the discrete system below

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

Matlab AR digital fiter computation w/o loops

I'm given the a(k) matrix and e(n) and I need to compute y(n) from the following eq:
y(n) = sum(k = 1 to 10 )( a(k)*y(n-k) ) + e(n).
I have the a matrix(filter coefficients) and the e matrix (the residual), therefore there is only 1 unknown: y, which is built by the previous 10 samples of y.
An example to this equation:
say e(0) (my first residual sample) = 3
and y(-10) to y(-1) = 0
then y(0), my first sample in the signal y, would just be e(0) = 3:
y(0) = a(1)*y(-1) + a(2)*y(-2) + .... + a(10)*y(-10) + e(0) = e(0) = 3
and if e(1) = 4, and a(1) = 5, then
y(1) = a(1)*y(0) + a(2)*y(-1) + a(3)&y(-2) + ... + a(10)*y(-9) + e(1) = 19
The problem is
I don't know how to do this without loops because, say, y(n) needs y(n-1), so I need to immediately append y(n-1) into my matrix in order to get y(n).
If n (the number of samples) = say, 10,000,000, then using a loop is not ideal.
What I've done so far
I have not implemented anything. The only thing I've done for this particular problem is research on what kind of Matlab functions I could use.
What I need
A Matlab function that, given an equation and or input matrix, computes the next y(n) and automatically appends that to the input matrix, and then computes the next y(n+1), and automatically append that to the input matrix and so on.
If there is anything regarding my approach
That seems like it's on the wrong track, or my question isn't clear enough, of if there is no such matlab function that exists, then I apologize in advance. Thank you for your time.

symbolic matlab matrix to latex

Consider the symbolic matlab expression
e = (a_1_1 + a_2_2)*(b_1_1 + b_2_2)
Using latex(e) this yields
\left({{a_{1}}}_{1} + {{a_{2}}}_{2}\right)\, \left({{b_{1}}}_{1} + {{b_{2}}}_{2}\right)
Is it possible to [somehow] use comma as separator between the indices, i.e. to get
\left(a_{1,1} + a_{2,2} \right)\,\left(b_{1,1} + b_{2,2}\right)
I'd be interested in an easy way to do it. An ugly way would be:
eqn = latex(e);
eqn1 = regexprep(eqn,'{{','');
eqn2 = regexprep(eqn1,'}}}_{',',');

how do I find the value of a parameter when others are given and integral is zero using matlab?

I have a function
function y = testf(x,F,phi,M,beta,alpha)
y = -((F+(1 + phi.*cos(2.*pi.*x))).*M.^3.*(cosh((1 + phi.*cos(2.*pi.*x)).*M)+M.*beta.*sinh((1 + phi.*cos(2.*pi.*x)).*M)))./((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M))- (alpha.*(M.^2.*(F+(1 + phi.*cos(2.*pi.*x))).*(-1+2.*(1 + phi.*cos(2.*pi.*x)).^2.*M.^2+ cosh(2.*(1 + phi.*cos(2.*pi.*x)).*M)-2.*(1 + phi.*cos(2.*pi.*x)).*M.*sinh(2.*(1 + phi.*cos(2.*pi.*x)).*M)))./(8.*((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M)).^2));
integrating with
q = quad(#(x) testf (x, F, phi,M, beta, alpha), 0, h);
when q = 0 and x,F,phi,M,beta, how do I find alpha and draw the streamline?
It would be great if you gave some numbers, but here is how you would start this. It assumes you are using a version of Matlab that has MuPad as the Symbolic Engine.
First of all, I wouldn't use quad because symbolic expressions will be involved, use int instead.
If I understood you correctly, have the values of x, F, phi, M, beta and you would like to solve for alpha when q = 0
%define the known variables first
syms alpha %defining symbolic object
Now, the following may not work because it's a huge function:
q = int(y,x,0,h)
If it did, all you have to do is solve and evaluate the results for alpha (this may not work as well):
alpha = eval( solve( 'q' , alpha ) )
If the above didn't achieve anything, you might what to look at the 'IgnoreAnalyticConstraints' option.