Error for reading function

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 个评论

Rik
Rik 2020-5-26
编辑:Rik 2020-5-26
Why did you feel the need to delete your comments and edit away your question? That is very rude. If you want private consulting, you should hire a private consultant.
Also, flagging this question as not appropriate doesn't do anything for you. Flags are a request for higher reputation users and site admins to look at a question.
Ö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.
(Answers Dev) Restored edit

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2020-5-24
编辑:dpb 2020-5-25
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 个评论

"...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>
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! :)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Profile and Improve Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by