Imaginary parts of complex X and/or Y arguments ignored

7 Ansichten (letzte 30 Tage)
Toghrul Nasirli
Toghrul Nasirli am 4 Apr. 2020
Bearbeitet: dpb am 7 Apr. 2020
Hi, I wrote this code for my homework and it works but I get this error.
Warning: Imaginary parts of complex X and/or Y arguments ignored
> In EthaneSatcurve (line 36)
Warning: Imaginary parts of complex X and/or Y arguments ignored
> In EthaneSatcurve (line 38)
I cannot solve this problem, could you please help me?
function EthaneSatcurve()
Pc = 706.5;% psi
Tc = 549.92;% R
Vc = 0.0783;% ft3/lbmole
alpha = [ 1.124 ];
R = 10.732; % psi ft3/(lbmole-R)
a = (0.42727*(R.^(2)).*(Tc.^(2)))./Pc;
b = (0.08664 * R * Tc) / Pc;
V = linspace(b*1.2,100,1000);
T= -10;
T = T + 460; % temperature in R
Psatguess=600;
Psat = fzero(@SRKLoopAreaDif, Psatguess);
fSRKEOSp = @(T,Vx,alpha)((R*T)./(Vx-b)) - (a*alpha./(Vx.*(Vx+b)));
P = fSRKEOSp(T,V,alpha);
plot(V,P);
xlim([0 Vc*100]);
ylim([0 500]);
xlabel('Volume, ft^3');
ylabel('Pressure, psi');
hold all;
plot(Vsat(1),Psat,'bo',Vsat(2),Psat,'bo',Vsat(3),Psat,'bo')
hold all;
plot([Vsat(1) Vsat(3)],[Psat Psat]);
function Z = SRKLoopAreaDif(Px)
SRKp = [Px -R*T +(a*alpha-b*R*T-b^(2)*Px) -(a*b*alpha)];
Vsat = roots(SRKp);
Vsat = sort(Vsat);
if any(imag(Vsat))
Z = 100;
return
end
fSRKEOSpvInt = @(Vx)((a.*log(b+Vx)-a.*log(Vx)+b.*R*T.^(3/2)*log(Vx-b))/(b.*T.^(1/2)));
i1 = fSRKEOSpvInt(Vsat(1));
i2 = fSRKEOSpvInt(Vsat(2));
i3 = fSRKEOSpvInt(Vsat(3));
j1 = (Vsat(2) - Vsat(1)) * Px;
j2 = (Vsat(3) - Vsat(2)) * Px;
AreaLeft = j1 - (i2 - i1);
AreaRight = (i3 - i2) - j2;
Z = AreaLeft - AreaRight;
end
end
  1 Kommentar
dpb
dpb am 4 Apr. 2020
Bearbeitet: dpb am 7 Apr. 2020
We can't run your code because
>> EthaneSatCurve
Error:File: EthaneSatCurve.m Line: 26 Column: 6
Identifier 'Vsat' is not a function or a shared variable. To share 'Vsat' with nested function,
initialize it in the current scope. For more
information, see Sharing Variables Between Parent and Nested Functions.
>>
and we don't know for certain which lines are 36 and 38 but
plot(Vsat(1),Psat,'bo',Vsat(2),Psat,'bo',Vsat(3),Psat,'bo')
hold all;
plot([Vsat(1) Vsat(3)],[Psat Psat]);
look like likely candidates; particularly when you have specific code
function Z = SRKLoopAreaDif(Px)
SRKp = [Px -R*T +(a*alpha-b*R*T-b^(2)*Px) -(a*b*alpha)];
Vsat = roots(SRKp);
Vsat = sort(Vsat);
if any(imag(Vsat))
Z = 100;
return
end
to fix up a problem that your solution to Vsat returns complex results. That can't be right besides the problem of the return value from the function that calculates Vsat doesn't return that result.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Harshendra Shah
Harshendra Shah am 7 Apr. 2020
Hi Toghrul,
This issue might be occurring because you are trying to plot complex numbers using the "plot" function.
To debug this issue, you can do the following:
  1. Stop MATLAB on the line where the warning occurs.
  2. Verify that the two vectors or matrices you pass to the PLOT function, are not complex.
  3. If you want to plot the real part of a vector versus the complex part, pass the vector as a single complex vector to the PLOT function.
  4. If you want to plot the magnitude of the elements, use the ABS function on the vector before passing it to the PLOT function.
You can refere to the following MATLAB Answers link for the same:

Community Treasure Hunt

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

Start Hunting!

Translated by