Any ideas on locating complex numbers in a matrix and changing their value to zero?

5 views (last 30 days)
Hello dear Mathworks world. I am having a little problem using the surf function in order to plot an energy surface with respect to two independent compositions (in a ternary system).
The problem seems to lie with the way I generate the solution, called Z. For some combinations of compositions, Z is is complex (which makes sense because they would be impossible in real life). However, the z matrix is quite large and changing these by hand would be intense (I cant even find them to be honest).
I was thinking of using something like: find(GS==nan) = 0; but this did not seem to work. Is there any way to set an elegant constraint on GS to not permit complex numbers in the solution space? I am still pretty new to the world of matlab so any advice is much appreciated.
Thank you! :)

Accepted Answer

Roger Stafford
Roger Stafford on 30 Nov 2013
Z(imag(Z)~=0) = 0;

More Answers (2)

Walter Roberson
Walter Roberson on 30 Nov 2013
GS(isnan(GS)) = 0;
  1 Comment
Alyssa
Alyssa on 30 Nov 2013
Hi Walter,
Thank you so very much for your response.
I gave it a shot and I still get "Error using surf (line 75) X, Y, Z, and C cannot be complex".
Do all complex numbers only get referred to as NaN? Is it possible they exist in some other way and cannot be found by calling for Nans?
I realized it may in fact be beneficial to post my code so far (in case I failed to see a more glaring error). I apologize for not doing that initially.
%Free Energy Data For Al-Zn-Sn %Note: All temperatures must be in Kelvin Tmal = 933.52;% melting temperature of Al Tmsn = 505.08; %" " " %Sn Tmzn = 692.68; %" " " %Zn %Czn =0; %at fraction of Zn %Csn =0;%at fraction of Sn %Cal=1-Czn-Csn; T=700; %K
Csn = 0.001:0.001:1; Czn= 0.001:0.001:1; C = 1 - Csn - Csn; [X, Y] = meshgrid(Csn,Czn); pairs = [Csn(:),Czn(:)]; Csn= X; Czn= Y; %Ideal Mixing: Gideal = 8.314.*T.*((1-Czn-Csn).*log((1-Czn-Csn))+Czn.*log(Czn)+Csn.*log(Csn));
%Liquid Phase: Pure Zn and Sn in Liquid standard state GrefalL = 10711.04.*(1-T/(Tmal)); GrefsnL = 0; GrefznL = 0; GrefL=(GrefalL).*(1-Czn-Csn)+(GrefznL).*Czn+(GrefsnL).*Csn;
%Binary Term for Liquid Phase: abin=16329.85; bbin=-3.39259; cbin=0; Ksnal0 = abin+bbin.*T+cbin.*T.*log(T);
a1=4111.97; b1=-1.15145; c1=0; Ksnal1 = a1+b1.*T+c1.*T.*log(T);
a2=1765.43; b2=-0.5739; c2=0; Ksnal2 = a2+b2.*T+c2.*T.*log(T);
a0=10465.55; b0=-3.39259; c0=0; Kznal0 = a0+b0.*T+c0.*T.*log(T);
a3=19314.64; b3=-75.89949; c3=8.751396; Kznsn0 = a3+b3.*T+c3.*T.*log(T);
a4=5696.28; b4=-4.20198; c4=0; Kznsn1 = a4+b4.*T+c4.*T.*log(T);
a5=1037.22; b5=0.98362; c5=0; Kznsn2 = a5+b5.*T+c5.*T.*log(T);
GbinaryL=Czn.*Csn.*(Kznsn0.*(Czn-Csn).^0+Kznsn1.*(Czn-Csn).^1+Kznsn2.*(Czn-Csn).^2)+Czn.*(1-Czn-Csn).*(Kznal0.*(Czn-(1-Czn-Csn)).^0)+Csn.*(1-Czn-Csn).*(Ksnal0.*(Csn-(1-Czn-Csn)).^0+Ksnal1.*(Csn-(1-Czn-Csn)).^1+Ksnal2.*(Csn-(1-Czn-Csn)).^2);
%Ternary Term: aal=-2777.03; bal=0.59427; azn=15225.63; bzn=-3.25821; asn=-16198.13; bsn=3.46632;
GternaryL=Czn.*Csn.*(1-Czn-Csn).*((aal+bal.*T).*(1-Czn-Csn)+(azn+bzn.*T).*Czn+(asn+bsn.*T).*Csn);
%Free Energy of Liquid Phase: GL = GrefL+Gideal+GbinaryL+GternaryL;
%------------------------------------------------------------------------------------------------------------------------------------ %Solid alpha (Al solid solution)--- standard state for Pure Solvent (Al): GrefalS=0; GrefsnS= -7029.12.*(1-T/(Tmsn)); GrefznS=-7322.*(1-T/(Tmzn)); GrefS = (GrefalS).*(1-Czn-Csn)+(GrefsnS).*Csn+(GrefznS).*Czn;
%Binary Term: Kznal0s = 7297.48+0.47512.*T; Kznal1s = 6612.88-4.59110.*T; Kznal2s = -3097.19+3.30635.*T; Kznsn0s = 33433.94-11.14466.*T; Ksnal0s = 45297.84+8.39814.*T;
GbinaryS = Czn.*Csn.*(Kznsn0s.*(Czn-Csn).^0)+Czn.*(1-Czn-Csn).*(Kznal0s.*(Czn-(1-Czn-Csn)).^0+Kznal1s.*(Czn-(1-Czn-Csn)).^1+Kznal2s.*(Czn-(1-Czn-Csn)).^2)+Csn.*(1-Czn-Csn).*(Ksnal0s.*(Csn-(1-Czn-Csn)).^0);
GS = GrefS+Gideal+GbinaryS; GS(isnan(GS)) = 0;
surf(Csn,Czn,GS)

Sign in to comment.


Jos (10584)
Jos (10584) on 30 Nov 2013
Take a look at isreal.
Z(~isreal(Z)) = 0 ;

Categories

Find more on Particle & Nuclear Physics 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!