Raising a matrix to a power
Show older comments
I want to raise the Tr value to the power of 1/2. I keep getting the same error. 

Answers (2)
Guillaume
on 7 Sep 2019
0 votes
The problem is not the .^0.5, it's the ^2, which probably should be .^2. Note that x.^0.5 is the same as sqrt(x).
However, If Tr is a matrix, then it's unlikely that alpha(Tr) is a valid target for assignment.
4 Comments
Nathan Stawasz
on 8 Sep 2019
Walter Roberson
on 9 Sep 2019
As Guillaume indicated, used .^2 instead of ^2
However, if your Tr is a matrix that is not positive integers, then you would have problems assigning to alpha(Tr) . If that is happening to you, then look at
madhan ravi
on 9 Sep 2019
Also note don’t name variable in the name of MATLAB’s inbuilt function. alpha() is an inbuilt function.
Guillaume
on 9 Sep 2019
Yes, as said, the current error is trivially fixed by replacing ^2 by .^2. As for the alpha(Tr) =, it's not clear what you intended to do with that.
Bruno Luong
on 9 Sep 2019
% This script will generate a plot of pressure P (in bar) as a function of molar
% volume V (in cm^3/mol) for various values of temperature T (in K) for
% ethylene as described by the Peng-Robinso n equation of state.
T=[260;270;280;282.4;290;300;310];
V=[50:400];
R= 83.14; % cm^3bar/mol K
Tc= 282.4; %K
Tr= T./Tc;
a= 5.001*10^6;% barcm^6/mol^2
b= 36.24; % cm^3/mol
k= 0.5098;
alpha = (1+k*(1-((Tr).^0.5)).^2);
P= (((R*T)./(V-b))- (a*alpha)./((V.^2)+((2*b*V)-b.^2 ))) ;
surf(V,alpha,P)
7 Comments
madhan ravi
on 9 Sep 2019
Edited: Bruno Luong
on 9 Sep 2019
Never name a variable *alpha*.
Bruno Luong
on 9 Sep 2019
Edited: Bruno Luong
on 9 Sep 2019
Why not? If the name speaks to the physics notation for and function does not use alpha function I don't see any problem. I use it all the time, alpha, beta, gamma, theta, psi, ... never have problem.
Walter Roberson
on 9 Sep 2019
alpha is not typically used for its function, but I have observed some people getting caught trying to use it to control transparency when they had a variable named alpha; and I have also observed some people thinking they were referring to a variable named alpha in a context in which the flow of control did not happen to assign a value to the variable, so alpha() got interpreted as a function call that promptly confused them with a "Too many output arguments" error message.
An experienced MATLAB Programmer would probably recognize the problem quickly.
The problem is not as pronounced as with sum which a fair number of people accidentally use as a variable as well as trying to use it as the function.
Using alpha as a variable name is a risk, but it might be a managable risk for experienced programmers.
madhan ravi
on 10 Sep 2019
Edited: madhan ravi
on 10 Sep 2019
Exactly as sir Walter says, it may not be a problem for you but not for people who don‘t have enough experience with MATLAB.
Bruno Luong
on 10 Sep 2019
How many time you or Walter see people having problem with naming their variable ALPHA? Me 0.
Funny so your comment under my post applies for virtulally everyone, excepted me?
Not what I read: "Never name a variable *alpha*" comes accross like a dictate statement for everyone who read your comment, since you did not name who should follow this statement.
When I read "NEVER" it means never even for me.
So you might tone down your statement.
BTW I never had problem either to name my variable alpha when I started to learn MATLAB. Since I don't use ALPHA function but I set ALPHA property of the graphic objects.
madhan ravi
on 10 Sep 2019
Edited: madhan ravi
on 10 Sep 2019
The comment wasn’t meant to you , it was meant for the OP who reads it, if you took it that way then I apologise. As I said earlier it is not a problem for you but for others it might.
Walter Roberson
on 10 Sep 2019
I see posts from people having trouble with alpha roughly once a year.
Categories
Find more on Lengths and Angles 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!