Getting a single value out of vpasolve with growing matrix

12 views (last 30 days)
The equation "E" has two roots and I am only interested in the positive root. the function for To5 keeps growing to a predefined point, my trouble is that I'm getting this error occurring at the second line of code: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
E = (1+R)*(Ym*Rg)*To5(i,2) + fo/(1+R)*Qr + (1+R+fo/(1+R) == 0.12*(1+R))*Yc*Rg*To6;
R(i,2) = vpasolve(E,R, [0 Inf]); %Step 6
I believe the problem is due to my not using vpasolve correctly in getting just the positive value out of the function and then assigning it to the growing matrix R, know how I can fix this?

Accepted Answer

Star Strider
Star Strider on 16 Apr 2016
You didn’t post the rest of your code, and it doesn’t work with the code you previously posted, so I can only post an example:
syms x
R = vpasolve(x^2 - x - 4 == 0)
ReR = R(real(R) > 0)
R =
-1.561552812808830274910704927987
2.561552812808830274910704927987
ReR =
2.561552812808830274910704927987
It’s relatively easy to select the solution that is greater than zero, or any number of other conditions.
  38 Comments
Vidhan Malik
Vidhan Malik on 16 Apr 2016
I tried updating my code using that method but can't tell if it is more efficient or not.
clear; clc;
syms R To5 fo To6
Ya = 1.4/0.4;
Ym = 1.35/0.35;
Yc = 1.3/0.3;
Rg = 0.287;
Qr = 45e3;
T32 = 276;
To6(1) = 1600;
i = 1;
Eff(1) = 45.844052030815569938093624771305;
E = (1+R)*(Ym*Rg)*To5 + fo/(1+R)*Qr == (1+R+fo/(1+R) - 0.12*(1+R))*Yc*Rg*To6;
Rs = solve(E, R);
Rspos = simplify(Rs, 'steps',10);
Rr = matlabFunction(Rspos);
Rr = @(Qr,Rg,To6,To7,Yc,Ym) (sqrt(Rg.*(Qr-Rg.*To6.*Yc).*(To6.*Yc.*-2.2e1+To7.*Ym.*2.3e1+Ym.*((To7./To6).^(Yc.*(-2.5e1./2.3e1)).*1.111967234867441).^((1.0e1./9.0)./Ym).*5.52e2).*-5.005e3).*(5.0./2.86e2)+Rg.*To6.*Yc.*2.2e1-Rg.*To7.*Ym.*2.3e1-Rg.*Ym.*((To7./To6).^(Yc.*(-2.5e1./2.3e1)).*1.111967234867441).^((1.0e1./9.0)./Ym).*5.52e2)./(Rg.*To6.*Yc.*-2.2e1+Rg.*To7.*Ym.*2.3e1+Rg.*Ym.*((To7./To6).^(Yc.*(-2.5e1./2.3e1)).*1.111967234867441).^((1.0e1./9.0)./Ym).*5.52e2);
while i <300
i = i +1;
To6(i) = To6(i-1) + 1;
To6 = To6(i);
To7 = To6*(2/3);
PrT = (To7/To6)^(Yc/0.92); %Step 1
PrC = 1/(0.97*0.99^3*PrT*0.975*0.98); %Step 2
To4 = 276*PrC^(1/(0.9*Ym)); %Step 3
To5 = 0.92*(To7 - To4) + To4; %Step 4
fostoic = 14/(1.5*(32+3.76*28));
fo = 0.9*fostoic; %Step 5
Wnet(i) = (1+Rr(Qr,Rg,To6,To7,Yc,Ym)+fo/(1+Rr(Qr,Rg,To6,To7,Yc,Ym)) - 0.12*(1+Rr(Qr,Rg,To6,To7,Yc,Ym)))*Yc*Rg*(To6-To7) - (1+Rr(Qr,Rg,To6,To7,Yc,Ym))*Ym*Rg*(To4-T32); %Step 7
Qin(i) = (1+Rr(Qr,Rg,To6,To7,Yc,Ym)+fo/(1+Rr(Qr,Rg,To6,To7,Yc,Ym))-0.12*(1+Rr(Qr,Rg,To6,To7,Yc,Ym)))*(Yc*Rg*To6-Ym*Rg*To5);
Eff(i) = Wnet(i)/Qin(i)*100; %Step 8
disp(vpa(Rr(Qr,Rg,To6,To7,Yc,Ym)));
end
figure(1)
plot(To6(:,2), Eff(:,2))
grid on
Either way I am getting the error of Index exceeds matrix dimensions. in line 27:
To6(i) = To6(i-1) + 1;
I don't see where it exceeds the matrix dimension.
Star Strider
Star Strider on 16 Apr 2016
I do!
Your ‘To6’ value is a scalar:
To6(1) = 1600;
. . .
To6(i) = To6(i-1) + 1; % <— DEFINED AS A VECTOR ...
To6 = To6(i); % <— ... THEN REDEFINED AS A SCALAR
For scalars, any subscript greater than 1 will throw the error you saw.
One solution is to define two different variables before the loop:
To6(1) = 1600;
To6v = To6;
... then within the loop:
To6v(i) = To6v(i-1) + 1;
To6 = To6v(i); % <— ADD THIS LINE
That will run. (I tested it.) I will leave you to determine if it produces the correct result.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!