Replacing the variable that are generated through loops ?
Show older comments
I have a code that generate the equation through loops :
Eqn(1)=4*K_T*P(1, 0)*P(4, 3) - 4*K_L*P(1, 0)*P(5, 4) + 4*K_T*P(2, 1)*P(3, 2) - 4*K_T*P(1, 0)*P(5, 4)
Eqn(2)= 2*K_T*P(3, 2)^2 + 4*K_T*P(1, 0)*P(5, 4) + 4*K_T*P(2, 1)*P(4, 3)
Please Note - In P(i,j) - i and j are not specifying any MATRIX position. They are numbers which are generated through some equation.P(i,j) are variables. There are many such variables. Now I want to replace them with some other variables.
So, my question is - Is it possible that the variables get changed to my desired variables, which i would specify ?
like P(1,0)==C(1)
P(3,2)==C(4)
thanks !!
3 Comments
madhan ravi
on 20 Dec 2018
subs()?
Walter Roberson
on 20 Dec 2018
To confirm, P(1,0) designates not P indexed at row 1 column 0, but rather a distinquished scalar variable P_1_0 and P(5,4) is a distinguished scalar variable P_5_4 rather than P indexed at row 5 column 4?
P K
on 20 Dec 2018
Answers (1)
Walter Roberson
on 20 Dec 2018
B = 1;
P = sym('P', [B+5 B+4]);
Eqn(1) = 4*K_T*P(B+1, B+0)*P(B+4, B+3) - 4*K_L*P(B+1, B+0)*P(B+5, B+4) + 4*K_T*P(B+2, B+1)*P(B+3, B+2) - 4*K_T*P(B+1, B+0)*P(B+5, B+4);
Eqn(2) = 2*K_T*P(B+3, B+2)^2 + 4*K_T*P(B+1, B+0)*P(B+5, B+4) + 4*K_T*P(B+2, B+1)*P(B+4, B+3);
subs(Eqn, [P(B+1, B+0), P(B+3,B+2)], [C(1), C(4)])
2 Comments
P K
on 20 Dec 2018
Walter Roberson
on 20 Dec 2018
Edited: Walter Roberson
on 20 Dec 2018
It does not replace P(2,1) as output: it adjusts indexing bases from 0 to 1.
It would, however, replace P(1,0) with P_2_1 because P = sym('P', [B+5 B+4]); is one based. You can get around that and have P10 appear instead if you use
temp_nn = sym('P%d%d', [5, 4]);
temp_0n = sym({'P00', 'P01', 'P02', 'P03', 'P04'});
temp_n0 = sym({'P10'; 'P20'; 'P30'; 'P40'; 'P50'});
P = [temp_0n; [temp_n0, temp_nn]];
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!