How to extract the term associated with a symbolic variable and assign to another symbolic variable?

8 visualizzazioni (ultimi 30 giorni)
Hi,
In the final expression shown in the result, how can I extract the entire term associated only with variable U_sr and assign it to another symbolic variable?
syms R_v L_v L_ph U_s U_i w s U_sr U_si real;
syms Z_v Z Y S;
U_s = U_sr + 1i*U_si;
U_i = 1 + 1i*0;
s = s + 1i*w;
Z_v = R_v + s*L_v;
Z = Z_v;
Y = 1/Z;
S = simplify(U_i*conj(Y*(U_i - U_s)));
S = subs(S, [U_i], [1+1i*0]);
S = subs(S, [U_s], [U_sr+1i*U_si]);
P = real(collect(S, [U_sr U_si]));
Q = imag(collect(S, [U_sr U_si]));
P = subs(P, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]);
Q = vpa(subs(Q, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]), 2)

Risposte (4)

埃博拉酱
埃博拉酱 il 23 Nov 2024
Use coeffs to get coefficients of U_sr and multiply it with U_sr.

Paul
Paul il 23 Nov 2024
Modificato: Paul il 23 Nov 2024
syms R_v L_v L_ph U_s U_i w s U_sr U_si real;
syms Z_v Z Y S;
U_s = U_sr + 1i*U_si;
U_i = 1 + 1i*0;
s = s + 1i*w;
Z_v = R_v + s*L_v;
Z = Z_v;
Y = 1/Z;
S = simplify(U_i*conj(Y*(U_i - U_s)));
S = subs(S, [U_i], [1+1i*0]);
S = subs(S, [U_s], [U_sr+1i*U_si]);
P = real(collect(S, [U_sr U_si]));
Q = imag(collect(S, [U_sr U_si]));
P = subs(P, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]);
Q = vpa(subs(Q, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]), 2),disp(char(Q))
20.0/((0.064*s + 0.2)^2 + 400.0) - (20.0*U_sr)/((0.064*s + 0.2)^2 + 400.0) + (U_si*(0.064*s + 0.2))/((0.064*s + 0.2)^2 + 400.0)
[c,t] = coeffs(Q,U_sr),disp(char(c)),disp(char(t))
[-20.0/((0.063661977241281419992446899414062*s + 0.20000000001164153218269348144531)^2 + 399.999999940395355224609375), 20.0/((0.063661977241281419992446899414062*s + 0.20000000001164153218269348144531)^2 + 399.999999940395355224609375) + (U_si*(0.063661977241281419992446899414062*s + 0.20000000001164153218269348144531))/((0.063661977241281419992446899414062*s + 0.20000000001164153218269348144531)^2 + 399.999999940395355224609375)] [U_sr, 1]
newvariable = c(1),disp(char(newvariable))
-20.0/((0.063661977241281419992446899414062*s + 0.20000000001164153218269348144531)^2 + 399.999999940395355224609375)
  2 Commenti
Hemanth
Hemanth il 29 Nov 2024
Hi ,
Thanks for the answer. I have used children command to extract the term associated with U_sr and U_si. Both children command and using coeffs manually extract the U_sr and U_si after looking at their position in P & Q expresssions. In the original question, if the order of multiplication is changed from S = simplify(U_i*conj(Y*(U_i - U_s))) to S = simplify(U_s*conj(Y*(U_s - U_i))), then U_sr and U_si positions change in P & Q. So, extraction has to be done again manually after observing their positions. Instead, if the term associated with U_sr and U_si can be extracted automatically without having to know their positions would be of great help.
Paul
Paul il 29 Nov 2024
These expressions are not the same
S = simplify(U_i*conj(Y*(U_i - U_s)))
S = simplify(U_s*conj(Y*(U_s - U_i)))
so it's not surprising that results change. In any case, coeffs shouldn't require any "manual" processing if the input expression is affine in U_sr (or U_si if that's the variable of interest).

Accedi per commentare.


Umar
Umar il 23 Nov 2024

Hi @Hemanth,

The process involves identifying the terms in the expression that contain U_sr and then assigning them to a new symbolic variable. Below is a detailed breakdown of the steps involved, along with the complete updated code.

Define Symbolic Variables: start by defining all necessary symbolic variables, including U_sr, U_si, and others.

Construct the Expression: The expression S is constructed using the defined variables, and perform substitutions to simplify it.

Collect Terms: use the collect function to group terms based on U_sr and U_si.

Extract Terms: To extract the terms associated with U_sr, use the coeffs function, which retrieves the coefficients of the polynomial in terms of U_sr.

Assign to New Variable: Finally, assign the extracted term to a new symbolic variable.

Here is the complete updated code with the extraction process included:

% Define symbolic variables
syms R_v L_v L_ph U_s U_i w s U_sr U_si real;
syms Z_v Z Y S;
% Define the complex voltage U_s
U_s = U_sr + 1i*U_si;
% Define the input voltage U_i
U_i = 1 + 1i*0;
% Define the complex frequency variable
s = s + 1i*w;
% Define the impedance Z_v
Z_v = R_v + s*L_v;
% Define the admittance Y
Z = Z_v;
Y = 1/Z;
% Define the expression S
S = simplify(U_i * conj(Y * (U_i - U_s)));
% Substitute U_i and U_s into S
S = subs(S, [U_i], [1 + 1i*0]);
S = subs(S, [U_s], [U_sr + 1i*U_si]);
% Collect real and imaginary parts
P = real(collect(S, [U_sr U_si]));
Q = imag(collect(S, [U_sr U_si]));
% Substitute values for L_v, R_v, and w
P = subs(P, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]);
Q = vpa(subs(Q, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]), 2);
% Extract the term associated with U_sr
% Collect terms in S with respect to U_sr
S_collected = collect(S, U_sr);
% Extract coefficients of U_sr
[coeffs_U_sr, terms_U_sr] = coeffs(S_collected, U_sr);
% Assign the entire term associated with U_sr to a new variable
U_sr_term = coeffs_U_sr * U_sr + terms_U_sr;
% Display the results
disp('Real Part P:');
disp(P);
disp('Imaginary Part Q:');
disp(Q);
disp('Term associated with U_sr:');
disp(U_sr_term);

For more information and guidance on the functions such as collect and coeffs, please click the links below.

https://www.mathworks.com/help/symbolic/sym.collect.html

https://www.mathworks.com/help/symbolic/sym.coeffs.html?s_tid=doc_ta

Please see attached.

The code begins by defining all necessary symbolic variables. The expression S is constructed using the defined variables and simplified. The substitutions for U_i and U_s are performed to prepare the expression for extraction. The collect function is used to group terms based on U_sr. The coeffs function retrieves the coefficients and terms associated with U_sr. The extracted term is assigned to U_sr_term, which can be used for further analysis or calculations.

This detailed approach allows for the effective extraction of terms associated with a specific variable in MATLAB. By following the outlined steps and utilizing the provided code, you can isolate and manipulate symbolic expressions with precision.

If you have any further questions or need additional assistance, feel free to ask!

  1 Commento
Hemanth
Hemanth il 29 Nov 2024
Hi,
Thanks for the answer. As can be seen in the output, the term associated with U_sr is not extracted from P and Q. Coeffs command was able to extract coefficients, only if they are numeric form in the polynomial. Instead, if the coefficients of a variable is a rational term expressed in terms of other variables, coeffs command is not working. For instance, how do you get the term associated with U_sr and U_si in below image without manually extracting them?

Accedi per commentare.


Star Strider
Star Strider il 29 Nov 2024
Useee theee function to find an expreessiion for
syms R_v L_v L_ph U_s U_i w s U_sr U_si real
syms Z_v Z Y S
sympref('AbbreviateOutput',false);
U_s = U_sr + 1i*U_si;
U_i = 1 + 1i*0;
s = s + 1i*w;
Z_v = R_v + s*L_v;
Z = Z_v;
Y = 1/Z;
S = simplify(U_i*conj(Y*(U_i - U_s)));
S = subs(S, [U_i], [1+1i*0]);
S = subs(S, [U_s], [U_sr+1i*U_si]);
P = real(collect(S, [U_sr U_si]));
Q = imag(collect(S, [U_sr U_si]));
P = subs(P, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]);
EqnP = P == vpa(P, 2);
P_U_sr = vpa(isolate(EqnP, U_sr), 2);
pretty(P_U_sr) % Show Result
2 2 3 3.4e+82 s - 7.2e+84 U_si + 4.0e+80 U_si s + 7.0e+79 U_si s - 9.8e+77 s - 1.1e+77 s + 1.0e+83 U_sr == ----------------------------------------------------------------------------------------------- 3 2 - 1.1e+77 s - 9.8e+77 s + 3.4e+82 s + 1.0e+83
% Q = subs(Q, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]);
EqnQ = vpa(Q == subs(Q, [L_v R_v w], [0.4/(2*pi) 0.2 2*pi*50]), 2);
pretty(EqnQ)
U_si (R_v + L_v s) L_v w 1.0 L_v U_sr w 20.0 20.0 U_sr U_si (0.064 s + 0.2) ------------------------ + ------------------------ - ------------------------ == ------------------------ - ------------------------ + ------------------------ 2 2 2 2 2 2 2 2 2 2 2 2 (R_v + L_v s) + L_v w (R_v + L_v s) + L_v w (R_v + L_v s) + L_v w (0.064 s + 0.2) + 400.0 (0.064 s + 0.2) + 400.0 (0.064 s + 0.2) + 400.0
EqnQ = Q == simplify(rhs(EqnQ), 500);
Q_U_sr = vpa(isolate(EqnQ, U_sr), 2);
pretty(Q_U_sr)
/ U_si (R_v + L_v s) (0.2 U_si + 0.064 U_si s + 20.0) 1.0 L_v w \ | ------------------------ - ------------------------------------ + ------------------------ | 1.0 | 2 2 2 2 2 2 2 | \ (R_v + L_v s) + L_v w 4.1e-3 s + 0.025 s + 400.0 (R_v + L_v s) + L_v w / U_sr == - -------------------------------------------------------------------------------------------------- 20.0 1.0 L_v w --------------------------- - ------------------------ 2 2 2 2 4.1e-3 s + 0.025 s + 400.0 (R_v + L_v s) + L_v w
Is something like this the desired result?
Note — You can create anonymous functions from ‘P_U_sr’ and ‘Q_U_Sr’ using matlabFunction:
P_U_sr_fcn = matlabFunction(rhs(P_U_sr))
Q_U_sr_fcn = matlabFunction(rhs(Q_U_sr))
respectively.
.

Prodotti


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by