The output of solve(eqn, x) is still an equation instead of number

1 次查看(过去 30 天)
Hello,
The whole script is below:
syms G1 G2 C1 C2 K H a s
G=(a*K*G1*G2)/(s^2*C1*C2+s*(C2*(G1+G2)+C1*G2*(1-K))+G1*G2) %Transfer function
[G_num,G_den]=numden(G)
[G_den_coeffs,~]=coeffs(G_den,s)
G_num=G_num/(C1*C2)
G_den_coeffs=G_den_coeffs/(C1*C2)
G_num=poly2sym(G_num,s)
G_den=poly2sym(G_den_coeffs,s)
G=G_num/G_den
equation2=G_den_coeffs(1,3)==1
isolate(equation2,C2)
%I need to solve equation 1 for G2.
equation1=G_den_coeffs(1,2)==0.618
G1=1/6000;
C1=10^-6;
C2=10^-9;
K=100;
G2=solve(equation1,G2)
The output of G2 is still an equation not a number as needed.
The variables are defined so when I copy and paste the G2 equation I get the value.
Any idea how can I get the result of G2 without the need to copy and paste the equation.
  1 个评论
Sam Chak
Sam Chak 2023-11-6
It is possible to solve for , but the solution can only satisfy one of the two design requirements. . The step response is highly oscillatory, and the percentage overshoot is as high as 94%. I don't think this solution is acceptable. In my answer below, I proposed making a free parameter so that two equations can be solved simultaneously.
G1 = 1/6000;
C1 = 1e-6;
C2 = 1e-9;
K = 100;
a = 1/K;
sympref('AbbreviateOutput', false);
syms s G2
G = (a*K*G1*G2)/(s^2*C1*C2+s*(C2*(G1+G2)+C1*G2*(1-K))+G1*G2)
G = 
[G_num, G_den] = numden(G);
[G_den_coeffs, ~] = coeffs(G_den, s)
G_den_coeffs = 
eqn1 = G_den_coeffs(2)/G_den_coeffs(1) == 0.618;
sol = solve(eqn1, G2);
s = tf('s');
G2 = double(sol)
G2 = 1.6773e-09
G = (a*K*G1*G2)/(s^2*C1*C2 + s*(C2*(G1 + G2) + C1*G2*(1 - K)) + G1*G2); %Transfer function
G = minreal(G)
G = 279.5 --------------------- s^2 + 0.618 s + 279.5 Continuous-time transfer function.
step(G)
% Check if it returns 1
(G1*G2)/(C1*C2)
ans = 279.5460
S = stepinfo(G);
S.Overshoot
ans = 94.3583

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-11-6
syms G1 G2 C1 C2 K H a s
G=(a*K*G1*G2)/(s^2*C1*C2+s*(C2*(G1+G2)+C1*G2*(1-K))+G1*G2) %Transfer function
G = 
[G_num,G_den]=numden(G)
G_num = 
G_den = 
[G_den_coeffs,~]=coeffs(G_den,s)
G_den_coeffs = 
G_num=G_num/(C1*C2)
G_num = 
G_den_coeffs=G_den_coeffs/(C1*C2)
G_den_coeffs = 
G_num=poly2sym(G_num,s)
G_num = 
G_den=poly2sym(G_den_coeffs,s)
G_den = 
G=G_num/G_den
G = 
equation2=G_den_coeffs(1,3)==1
equation2 = 
isolate(equation2,C2)
ans = 
%I need to solve equation 1 for G2.
equation1=G_den_coeffs(1,2)==0.618
equation1 = 
G1=1/6000;
C1=10^-6;
C2=10^-9;
K=100;
G2 = solve( subs(equation1), G2)
G2 = 
  1 个评论
Walter Roberson
Walter Roberson 2023-11-6
Consider:
C1 = 3
C2 = C1*10 + 5
C1 = 7
If you now ask for the value of C2, what value are you expecting to get? Are you expecting to get 3*10+5 or are you expecting to get 7*10+5 ? So when you assign the result of an expression to a variable, do you expect MATLAB to remember the formula and to automatically update the result of the formula each time any of the components of the formula changes?
Likewise if you
syms C1
C2 = C1*10 + 5
C1 = 7
If you now ask for the value of C2, what value are you expecting to get? Are you expecting to get 3*10+5 or are you expecting to get 7*10+5 ? Yes, C2 has a formula -- but the formula that is remembered in C2 is "[internal symbolic variable named C1]" times 10 plus 5 -- and when you update C1=7 you are not changing that "internal symbolic variable named C1" so asking to display C2 at this point will continue to know "internal symbolic variable named C1" times 10 plus 5.

请先登录,再进行评论。

更多回答(2 个)

Torsten
Torsten 2023-11-6
编辑:Torsten 2023-11-6
Use
G2 = double(solve(subs(equation1)))
instead of
G2=solve(equation1,G2)

Sam Chak
Sam Chak 2023-11-6
In the comment above, if is set as constant, then the parameter appears in two terms. can be found by solving either one of the terms, but it won't satisfy both design requirements. To address this issue, I selected as a second parameter. Solving two equations simultaneously will return the solution for both and .
format long g
% Constants
C1 = 1e-6;
C2 = 1e-9;
K = 100;
out = 1; % desired output (can be 1, 100, or pi, etc.)
a = out/K; % output coefficient
sympref('AbbreviateOutput', false);
% Declare parameters G1 and G2 that defines the transfer function
syms s G1 G2
assume(G1, "positive")
assume(G2, "positive")
% The transfer function
G = (a*K*G1*G2)/(s^2*C1*C2+s*(C2*(G1+G2)+C1*G2*(1-K))+G1*G2)
G = 
[G_num, G_den] = numden(G);
[G_den_coeffs, ~] = coeffs(G_den, s)
G_den_coeffs = 
% Solve simultaneous equations for both parameters G1 and G2
eqn1 = G_den_coeffs(2) == 0.618;
eqn2 = G_den_coeffs(3) == 1;
sol = solve([eqn1, eqn2], [G1, G2]);
G1 = double(sol.G1) % 1.02636211e-5
G1 =
1.0263621087716e-05
G2 = double(sol.G2) % 9.74315002e-11
G2 =
9.74315001941029e-11
%% if zeta = 0.618
% G1 = double(sol.G1) % 1.05869981e-5
% G2 = double(sol.G2) % 9.44554808e-11
%% Test
s = tf('s');
G = (a*K*G1*G2)/(s^2*C1*C2+s*(C2*(G1+G2)+C1*G2*(1-K))+G1*G2);
G = minreal(G) % check if design requirements are satisfied
G = 1 ----------------- s^2 + 0.618 s + 1 Continuous-time transfer function.
step(G), grid on % step response

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by