"str2sym" delete the element wise operation
2 views (last 30 days)
Show older comments
Tommaso Colamartino
on 6 Aug 2020
Commented: Tommaso Colamartino
on 11 Aug 2020
Hello everybody,
I've a problem, I have these symbolic functions written with element wise operations; I put them together obtaining a new symbolic function but here the operations aren't the element-wise type (as I wish). So, I tried to solve this problem using 'vectorize', but it gives me a string. So I changed it another time into a function with 'str2sym' but the element-wise operations are replaced with the simples ones (all the dots are deleted).
Down below there is my code; the part of interest is the last one, point 7. I would like to write the integral function in the form 'integral (f,H_t_rr,1)'; is it possible?
thanks to anyone who can help me.
% ---------- PRELIMINARY DATES ----------
eta_c=0.88; %adiabatic efficiency
P_ratio=1.3;
M1_rel=0.75;
Ut_a01=1.0; %first stage rotor tip speed divided for sound speed in an ideal gas
H_t_rr=0.6; %hub tip radius ratio
B=-0.4; %dimensionless swirl number: b1/(omega*rt^2), omega=angular speed, rt=radius tip
g=1.4; % ɣ, adiabatic dilatation gas coefficient (cp/cv)
%radiuses at which we want to obtain the preliminary design datas
% ---------- DESIGN PROCEDURE ----------
% 1. tip diameter work ratio: (delta_c_u/U) t
W_t=((P_ratio)^((g-1)/g)-1)/((g-1)*eta_c*(Ut_a01));
% 2. mean radius swirl velocity ratio at the entrance: (c_u1/U) m
y_m =(1+H_t_rr)/2;
SVR_m1 = 0.5*(1- 1/y_m^2 * W_t);
% 3. parameter A
A= SVR_m1*y_m - B/y_m;
% tip radius swirl ratio at the entrance: (c_u1/U) t
SVR_t1= A+B;
% 4. axial velocity ratio for the tip and the mid diameters at the entrance: (c_z1/U) t
AVR_tip1 = sqrt((M1_rel^2*((1/Ut_a01)^2-(0.5*(g-1))*(SVR_t1)^2)-(1-(SVR_t1))^2)/(1+((g-1)/2)*M1_rel^2));
AVR_mid1 = 1/y_m*sqrt((AVR_tip1)^2-2*A^2*log(y_m)-2*B*A*(1-1/y_m));
% 5. axial velocity ratios and swirl velocity ratios at the entrance and the exit of the stage at each radius (symbloic functions): (c_u1/U), (c_z1/U); (c_u2/U), (c_z2/U)
syms AVR_y1(y) AVR_y2(y) SVR_y1(y) SVR_y2(y) DensR(y);
AVR_y1(y)= 1./y.*sqrt((AVR_tip1)^2-2*A^2*log(y)-2*B*A*(1-1./y));
AVR_y2(y)= 1./y.*sqrt((AVR_mid1)^2*y_m^2-2*A^2*log(y/y_m)-2*A*(B+W_t)*(1/y_m-1./y));
SVR_y1(y)= A./y + B./y.^2;
SVR_y2(y)= A./y + (1./y).^2*(B+W_t);
% 6. density ratio for each radio: ro/ro01
DensR(y)= (1-0.5*(g-1)*Ut_a01^2*y.^2.*(SVR_y1.^2.+AVR_y1.^2)).^(1/(g-1));
% 7. correct mass flow rate
syms fun(y);
fun(y)=DensR(y).*AVR_y1(y).*y.^2;
f_string=vectorize(fun)
f=str2sym(f_string)
F=integral(@(y) y.*(1 - (y.^2.*((6861554695565405./(9007199254740992.*y) - 2./(5.*y.^2)).^2 - ((5227033566006029.*log(y))./4503599627370496 + 1372310939113081./(2251799813685248.*y) - 13252874358597121./18014398509481984)./y.^2))./5).^(5./2).*(13252874358597121./18014398509481984 - 1372310939113081./(2251799813685248.*y) - (5227033566006029.*log(y))./4503599627370496).^(1./2) ,H_t_rr,1)
m_correct=pi/2 *sqrt(g)*Ut_a01*F
0 Comments
Accepted Answer
More Answers (1)
Walter Roberson
on 6 Aug 2020
This is correct behaviour for symbolic expressions.
The symbolic toolbox assumes that any unresolved symbolic variable name will resolve to a scalar.
syms x y
x*y - y*x
will be scalar 0 because it assumes scalars and applies commutivity.
When you work with arrays in symbolic toolbox they must be fully sized ahead of time, and the expression is evaluated on those arrays generating an array of results, not the underlying expressions
syms A [2,2]
syms B [2,2]
C = A*B
C will not be stored as "variable named A times variable named B" : the matrix multiplication will be done and C will be stored as all the appropriate sub-expressions.
With the symbolic toolbox, there is no easy way to code "This variable stands in for any array that will be replaced with an actual array later and array operations are to be done on that array when it is resolved." The symbolic toolbox is not designed to permit that.
So when you str2sym then any symbol will be assumed to be scalar and operated on that way. The dot version of the operators are not used because all the operators that make it into symbolic expressions are operating on scalars.
Your difficulty is that you are trying to take the character version of the symbolic expression and use it as a matlab expression. However, the language used by the symbolic expressions is not matlab, and char() of symbolic expressions is only sometimes the matlab code that you would hope for. For example the symbolic beta function has its parameters reversed from the matlab function, and the expression 1 <= x & x <= 5 has char() that uses characters that are not valid in MATLAB.
Madhan already pointed you to the solution: use matlabFunction, which will generate correct matlab code (except when it fails, which it will if you had a vpaintegral call for example). My post here is to explain why you were seeing the problem.
3 Comments
Walter Roberson
on 9 Aug 2020
You can have a vector of symbolic expressions. What you cannot have is a normal vector of symbolic functions. When you have an operation that would look to create a vector of symbolic functions, matlab will instead convert it to a single symbolic function that computes a vector.
In order to store multiple symbolic functions in a single array, use a cell array.
The way that matlab handles the assignment
variable(symbolic_name) = value
is that it computes the value without caring that you are in an assignment, and then it does
variable = symfun(value, symbolic_name)
which wraps the entire value together in one calculation and makes that the result of calling a function.
... Mostly, I suggest that if you are working with vectors or matrices that you avoid defining symbolic functions, using symbolic expressions instead. However, there are a few obscure places where historically matlab would give a different result for a symbolic function than for a symbolic expression that should be equivalent, so if you are working with differential equations then use symbolic functions.
(I should go back and review the bug report I sent about the different results as I have long forgotten the details.)
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!