Extract specific parts of a matlab/ octave symbolic expression? - matlab

How does one extract specific parts of an expression in Matlab/ Octave symbolic package? In XCAS, one can use indexing expressions, but I can't find anything similar in Octave/ Matlab.
For instance, with X = C*L*s**2 + C*R*s + 1, is there a way to get C*R*s by X(2) or the like?
It would be nice to do this with factors too. X = (alpha + s)*(beta**2 + s**2)*(C*R*s + 1), and have X(2) give (beta**2 + s**2).
Thanks!

children (MATLAB doc, Octave doc) does this but the order in which you write the expressions will not necessarily be the same. The order is also different in MATLAB and Octave.
Expanded Expression:
syms R L C s;
X1 = C*L*s^2 + C*R*s + 1;
partsX1 = children(X1);
In MATLAB:
>> X1
X1 =
C*L*s^2 + C*R*s + 1
>> partsX1
partsX1 =
[ C*R*s, C*L*s^2, 1]
In Octave:
octave:1> X1
X1 = (sym)
2
C⋅L⋅s + C⋅R⋅s + 1
octave:2> partsX1
partsX1 = (sym 1×3 matrix)
⎡ 2 ⎤
⎣1 C⋅L⋅s C⋅R⋅s⎦
Factorised Expression:
syms R C a beta s; %alpha is also a MATLAB function so don't shadow it with your variable
X2 = (a + s) * (beta^2 + s^2) * (C*R*s + 1);
partsX2 = children(X2);
In MATLAB:
>> X2
X2 =
(a + s)*(C*R*s + 1)*(beta^2 + s^2)
>> partsX2
partsX2 =
[ a + s, C*R*s + 1, beta^2 + s^2]
In Octave:
octave:3> X2
X2 = (sym)
⎛ 2 2⎞
(a + s)⋅⎝β + s ⎠⋅(C⋅R⋅s + 1)
octave:4> partsX2
partsX2 = (sym 1×3 matrix)
⎡ 2 2⎤
⎣C⋅R⋅s + 1 a + s β + s ⎦

Related

How to make the response of the solve function symbolic?

I am solving a fourth order equation in matlab using the solve function.My script looks like this:
syms m M I L Bp Bc g x
m = 0.127
M = 1.206
I = 0.001
L = 0.178
Bc = 5.4
Bp = 0.002
g = 9.8
eqn = ((m + M)*(I + m*L^2) - m^2*L^2)*x^4 + ((m + M)*Bp + (I + m*L^2)*Bc)*x^3 + ((m + M)*m*g*L + Bc*Bp)*x^2 + m*g*L*Bc*x == 0
S = solve(eqn, x)
In the answer, I should get 4 roots, but instead I get such strange expressions:
S =
0
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 1)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 2)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 3)
The first root, which is 0, is displayed clearly. Is it possible to make the other three roots appear as numbers as well? I looked for something about this in the documentation for the solve function, but did not find it.

How to solve the following problem on MATLAB

a(x) here is a type of equation used in signal
Let
y(x) = x^3 + x^2 + x + 1
a(x) is inputed by the user so for example if user inputs
a(x) = D2 + D + 6
y(x)*a(x) = D2(x^3 + x^2 + x + 1) + D(x^3 + x^2 + x + 1) + 6(x^3 + x^2 + x + 1)
here D2(x^3 + x^2 + x + 1) = 6x + 2 and D(x^3 + x^2 + x + 1) = 3x^2 + 2x + 1
So D is differentiation and D2 is double differential
So i want to know how i would do something like this on MATLAB
Use Matlab symbolic computation toolbox https://www.mathworks.com/products/symbolic.html
Calculus
Evaluate exact analytical solutions for definite or indefinite integral,
calculate derivatives of symbolic expressions or functions,
and approximate functions using series expansions.

How to convert a symbolic expression to a Octave function from the Symbolic Package?

How to convert a symbolic expression to a Octave function from the Symbolic Package?
After installing the symbolic package on octave with pkg install -forge symbolic. Using the symbolic package on octave I may write this:
octave> pkg load symbolic;
octave> a = sym( "a" );
octave> int ( a^2 + csc(a) )
which will result in:
ans = (sym)
3
a log(cos(a) - 1) log(cos(a) + 1)
-- + --------------- - ---------------
3 2 2
But how to do this Integral (int(1)) symbolic result just above to became a valuable function like this below?
function x = f( x )
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
end
f(3)
# Which evaluates to: 11.6463 + 1.5708i
I want to take the symbolic result from int ( a^2 + csc(a) ), and call result(3), to compute it at 3, i.e., return the numeric value 11.6463 + 1.5708i, from the symbolic expression integral a^2 + csc(a). Basically, how to use the symbolic expression as numerically evaluable expressions? It is the as this other question for Matlab.
References:
http://octave.sourceforge.net/symbolic/index.html
How do I declare a symbolic matrix in Octave?
Octave symbolic expression
Julia: how do I convert a symbolic expression to a function?
What is symbolic computation?
You can use pretty.
syms x;
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2;
pretty(x)
which gives this:
3
log(cos(x) - 1) log(cos(x) + 1) x
--------------- - --------------- + --
2 2 3
Update (Since the question is edited):
Make this function:
function x = f(y)
syms a;
f(a) = int ( a^2 + csc(a) );
x = double(f(y));
end
Now when you call it using f(3), it gives:
ans =
11.6463 + 1.5708i
it seems like you answered your own question by linking to the other question about Matlab.
Octave has an implementation of matlabFunction which is a wrapper for function_handle in the symbolic toolbox.
>> pkg load symbolic;
>> syms x;
>> y = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
y = (sym)
3
x log(cos(x) - 1) log(cos(x) + 1)
-- + --------------- - ---------------
3 2 2
>> testfun = matlabFunction(y)
testfun =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
testfun(3)
>> testfun(3)
ans = 11.6463 + 1.5708i
>> testfun([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
>> testfun2 = matlabFunction(int ( x^2 + csc(x) ))
testfun2 =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
>> testfun2(3)
ans = 11.6463 + 1.5708i
>> testfun2([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
I'm sure there are other ways you could implement this, but it may allow you to avoid hardcoding your equation in a function.

Matlab: Nonlinear equation solver

How do I solve these sets of equations and can matlab find a solution? I'm solving for x1,x2,x3,x4,c1,c2,c3,c4.
syms c1 c2 c3 c4 x1 x2 x3 x4;
eqn1 = c1 + c2 + c3 + c4 == 2;
eqn2 = c1*x1 + c2*x2 + c3*x3 + c4*x4 == 0;
eqn3 = c1*x1^2 + c2*x2^2 + c3*x3^2 + c4*x4^2 == 2/3;
eqn4 = c1*x1^3 + c2*x2^3 + c3*x3^3 + c4*x4^3 == 0;
eqn5 = c1*x1^4 + c2*x2^4 + c3*x3^4 + c4*x4^4 == 2/5;
eqn6 = c1*x1^5 + c2*x2^5 + c3*x3^5 + c4*x4^5 == 0;
eqn7 = c1*x1^6 + c2*x2^6 + c3*x3^6 + c4*x4^6 == 2/7;
eqn8 = c1*x1^7 + c2*x2^7 + c3*x3^7 + c4*x4^7 == 0;
From what I understand, matlab has fsolve, solve, and linsolve, but I'm uncertain how to use them.
You have a system of non-linear equations, so you can use fsolve to find a solution.
First of all you need to create a function, say fcn, of a variable x, where x is a vector with your initial point. The function defines an output vector, depending on the current vector x.
You have eight variables, so your vector x will consist of eight elements. Let's rename your variables in this way:
%x1 x(1) %c1 x(5)
%x2 x(2) %c2 x(6)
%x3 x(3) %c3 x(7)
%x4 x(4) %c4 x(8)
Your function will look like this:
function F = fcn(x)
F=[x(5) + x(6) + x(7) + x(8) - 2 ;
x(5)*x(1) + x(6)*x(2) + x(7)*x(3) + x(8)*x(4) ;
x(5)*x(1)^2 + x(6)*x(2)^2 + x(7)*x(3)^2 + x(8)*x(4)^2 - 2/3 ;
x(5)*x(1)^3 + x(6)*x(2)^3 + x(7)*x(3)^3 + x(8)*x(4)^3 ;
x(5)*x(1)^4 + x(6)*x(2)^4 + x(7)*x(3)^4 + x(8)*x(4)^4 - 2/5 ;
x(5)*x(1)^5 + x(6)*x(2)^5 + x(7)*x(3)^5 + x(8)*x(4)^5 ;
x(5)*x(1)^6 + x(6)*x(2)^6 + x(7)*x(3)^6 + x(8)*x(4)^6 - 2/7 ;
x(5)*x(1)^7 + x(6)*x(2)^7 + x(7)*x(3)^7 + x(8)*x(4)^7
];
end
You can evaluate your function with some initial value of x:
x0 = [1; 1; 1; 1; 1; 1; 1; 1];
F0 = fcn(x0);
Using x0 as initial point your function returns:
F0 =
2.0000
4.0000
3.3333
4.0000
3.6000
4.0000
3.7143
4.0000
Now you can start fsolve which will try to find some vector x, such as your function returns all zeros:
[x,fval]=fsolve(#fcn, x0);
You will get something like this:
x =
0.7224
0.7224
-0.1100
-0.7589
0.3599
0.3599
0.6794
0.5768
fval =
-0.0240
0.0075
0.0493
0.0183
-0.0126
-0.0036
-0.0733
-0.0097
As you can see, the function values are really close to zeros, but you probably noticed that the optimization algorithm was stopped because of the limited count of the function evaluation steps stored in options.MaxFunEvals (by default 800). Another possible reason is the limited number of iterations stored in MaxIter (by default 400).
Redefine these value using the parameter options:
options = optimset('MaxFunEvals',2000, 'MaxIter', 1000);
[x,fval]=fsolve(#fcn, x0, options);
Now your output is much better:
x =
0.7963
0.7963
-0.0049
-0.7987
0.2619
0.2619
0.9592
0.5165
fval =
-0.0005
-0.0000
-0.0050
0.0014
0.0208
-0.0001
-0.0181
-0.0007
Just play with different parameter values, in order to achieve a tolerable precision level for your problem.

How to separate the real and imaginary parts of a transfer function?

Here is a transfer function:
S = [tf([10 2 4],[1 95 2000 3450])];
How can I get real(S) and Imag(S)?
It sounds like you want the Fourier form of your transfer function. As far as I know, there's no builtin function for this so you'll need to use symbolic math:
num = [10 2 4];
den = [1 95 2000 3450];
syms s;
syms omega real; % Define as real-valued
f1 = poly2sym(num,s)/poly2sym(den,s)
f2 = subs(f1,s,1i*omega)
f2_real = simplify(real(f2))
f2_imag = simplify(imag(f2))
which returns
f1 =
(10*s^2 + 2*s + 4)/(s^3 + 95*s^2 + 2000*s + 3450)
f2 =
(- 10*omega^2 + omega*2i + 4)/(- omega^3*1i - 95*omega^2 + omega*2000i + 3450)
f2_real =
(4*(237*omega^4 - 7720*omega^2 + 3450))/(omega^6 + 5025*omega^4 + 3344500*omega^2 + 11902500)
f2_imag =
-(2*omega*(5*omega^4 - 9907*omega^2 + 550))/(omega^6 + 5025*omega^4 + 3344500*omega^2 + 11902500)
You can then use subs and vpa/double to evaluate these for a particular value of omega.