solve() question: both substitution and symbolic solutions
1 次查看(过去 30 天)
显示 更早的评论
I have a simple equation consisting of symbolic variables and want to solve it.
>> eqn(1) = d1 == 0
eqn =
2*C11*G13*p13*q11 + 2*C13*G11*p11*q13 - 2*C8*G20*p20*q8 - 2*C20*G8*p8*q20 == 0
>> solve(eqn(1), C(11))
ans =
(2*C8*G20*p20*q8 - 2*C13*G11*p11*q13 + 2*C20*G8*p8*q20)/(2*G13*p13*q11)
However, I want the q and p values be only 0 or 1. So, at the end I want to MATLAB to substitute 0 or 1 for the q and p variables and obtain symbolic solution for C and G variables. I looked at subs but it did not work for this case. Normally, I defined symbolic variables previously using assume etc. but I did not give what I wanted
y = sym('y',[1 21]);
q = sym('q',[1 21]);
assume(q==0 | q==1)
C = sym('C',[1 21], {'positive', 'rational'});
p = sym('p',[1 21]);
assume(p==0 | p==1)
G = sym('G',[1 21], {'positive', 'rational'});
>> assumptions(p)
ans =
[p1 == 0 | p1 == 1, p2 == 0 | p2 == 1, p3 == 0 | p3 == 1, p4 == 0 | p4 == 1, p5 == 0 | p5 == 1, p6 == 0 | p6 == 1, p7 == 0 | p7 == 1, p8 == 0 | p8 == 1, p9 == 0 | p9 == 1, p10 == 0 | p10 == 1, p11 == 0 | p11 == 1, p12 == 0 | p12 == 1, p13 == 0 | p13 == 1, p14 == 0 | p14 == 1, p15 == 0 | p15 == 1, p16 == 0 | p16 == 1, p17 == 0 | p17 == 1, p18 == 0 | p18 == 1, p19 == 0 | p19 == 1, p20 == 0 | p20 == 1, p21 == 0 | p21 == 1]
I can see my assumptions using assumptions() command but ...
By the way, with the solve() command above I get conditions but I ccould not figure out completely
>> aaa = solve(eqn(1), C(11), 'ReturnConditions', true)
aaa =
struct with fields:
C11: [1×1 sym]
parameters: [1×0 sym]
conditions: [1×1 sym]
>> aaa.conditions
ans =
0 < p13*q11*(C8*G20*p20*q8 - C13*G11*p11*q13 + C20*G8*p8*q20)
I want to obtain solutions like that: p8=0, q13=1, C11= (C8*G20/G8) etc.Can you help me?
0 个评论
回答(1 个)
Walter iacomacci
2020-11-16
Hi Serdar maybe you are looking for something like this
I've setted everything Symbolic since i dont have real values to recreate the code. You wanted to set values for q and p after solving the equation, this might be what you need.
syms C11 G13 p13 q11 C13 G11 p11 q13 C8 G20 p20 q8 C20 G8 p8 q20
eqn = 2*C11*G13*p13*q11 + 2*C13*G11*p11*q13 - 2*C8*G20*p20*q8 - 2*C20*G8*p8*q20 == 0;
C11_sol=solve(eqn,C11);
%Variables to Replace %New values Respectively
C11_sol=subs(C11_sol,[p13 q11 p11 q13 p20 q8 p8 q20],[1 1 1 1 1 1 1 1])
This will return the following:
C11_sol =
(2*C8*G20 - 2*C13*G11 + 2*C20*G8)/(2*G13)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!