How to extract the term associated with a symbolic variable and assign to another symbolic variable?
0 Comments
Answers (3)
0 Comments
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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!