1 view (last 30 days)

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?

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.

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
on 24 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! :)

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_864688

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_864688

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_865168

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_865168

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_877315

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/531988-error-for-reading-function#comment_877315

Sign in to comment.