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

24 visualizaciones (últimos 30 días)
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)

Respuestas (3)

埃博拉酱
埃博拉酱 hace alrededor de 18 horas
Use coeffs to get coefficients of U_sr and multiply it with U_sr.

Paul
Paul hace alrededor de 18 horas
Editada: Paul hace alrededor de 18 horas
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)

Umar
Umar hace alrededor de 9 horas

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!

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by