Error for reading function

1 view (last 30 days)
Özgü Dönmez
Özgü Dönmez on 24 May 2020
Commented: Rena Berman on 31 May 2020
sumenergy=((tetha_1)*(Cp_1)*((T_isoth)-(T_1)))+ ((tetha_2)*(Cp_2)*((T_isoth)-(T_2)))+((tetha_3)*(Cp_3)*((T_isoth)-(T_3));
T_ad=350;
delta_H_T_ref=adiabatic(T_ad);
sumenergy2= ((delta_H_T_ref)+ ((delta_Cp)*((T_isoth)-(T_ref)));
In the code, the function adiabatic is working very well. But, when I call the function in sumenergy2 equality it says 'Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.' for both sumenergy and sumenergy2. What does mean this?
  3 Comments
Rik
Rik on 26 May 2020
Özgü Dönmez responded to my comment by email claiming the following: "I asked 2 questions on Matlab. But, the parts of the questions are deleted due to my internet connection. I don't know why. They were not deleted by me."
Although this would be an unlikely explanation for the questions themself, I don't see how this could result in the comments being deleted as well.
Rena Berman
Rena Berman on 31 May 2020
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

dpb
dpb on 24 May 2020
Edited: dpb on 25 May 2020
You certainly do throw the parentheses around... :)
sumenergy2= ((delta_H_T_ref)+ ((delta_Cp)*((T_isoth)-(T_ref)));
12 3 45 6 78 9 1 111
0 123
shows there are 13(!!!) in the expression so there's one unbalanced pair -- as usual, MATLAB is right. :)
Superfluous () make it much harder to read the code--for the humans, anyways...
Let's see if we can clean up a little and find the problem...
sumenergy2= (delta_H_T_ref + (delta_Cp*(T_isoth-T_ref));
is left after removing the innermost pairs around the individual variables. They serve no purpose at all
That leaves five(5) ; the next step would be
sumenergy2= delta_H_T_ref + (delta_Cp*(T_isoth-T_ref);
removing the outermost pair that are also of no need/use; the sum is a quantity w/o encapsulating further as there are no other operations on the compound object other than assignment to the LHS.
Now it's easy to see the extra one in front of the second term in the addends.
sumenergy2= delta_H_T_ref + delta_Cp*(T_isoth-T_ref);
is now balanced and is much simpler to read and computes the desired result with the only needed grouping on the temperature difference to be multiplied by the heat capacity.
I'd suggest a cleanup of the rest of the code by the same logic -- it will become much easier to read and maintain.
sumenergy=(tetha_1*Cp_1*(T_isoth-T_1))+ (tetha_2*Cp_2*(T_isoth-T_2))+(tetha_3*Cp_3*(T_isoth-T_3));
sumenergy=tetha_1*Cp_1*(T_isoth-T_1) + tetha_2*Cp_2*(T_isoth-T_2) + tetha_3*Cp_3*(T_isoth-T_3);
I'd also note a stylistic form in using MATLAB -- if the constants Cp and theta (tetha?) and the variable T were arrays instead of named variables, then you could use the builtin array operations within MATLAB and write:
sumenergy=sum(theta.*Cp.*(T_isoth-T));
where each of theta, Cp, T_isoth are 3x1 (or 1x3) vectors.
  2 Comments
dpb
dpb on 24 May 2020
"...and computes the desired result with the only needed grouping on the temperature difference to be multiplied by the heat capacity."
I didn't think to comment explicitly, but the above is true and reproducible result for the expression of the form a + b*c because the multiplication operators * and / have higher precedence than + and -.
This is the full reference link <MATLAB Precedence Order>
dpb
dpb on 24 May 2020
Edited: dpb on 25 May 2020
OBTW, on debugging stuff like this at command line...a different way of counting you can do as balance check is--
sumenergy2= ((delta_H_T_ref)+ ((delta_Cp)*((T_isoth)-(T_ref)));
12 1 23 2 34 3 2 10?
which counts nesting depth -- if you don't come out at zero in the end, you're not balanced. Here the final count would be -1 which indicates are either missing an opening "(" or have one too many closing ")".
Of course, you could fix the above error by inserting #14 to make the expression balance, but I strongly recommend against that solution! :)

Sign in to comment.

Categories

Find more on Programming 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!