Turning imaginary numbers into real numbers
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Colin Lynch
am 12 Feb. 2018
Kommentiert: Walter Roberson
am 12 Feb. 2018
Hello there!
I am attempting to find the points where n = S with the following equations. Whenever S goes into the imaginary plane though, I want it to convert to S = 1. Even though I have a check for that in this code, for some reason MATLAB seems to be ignoring that bit and converting S into 0 instead. How can I fix this?
for k = 1:1:100
n = (100 / k);
s = sqrt((((.25*(n-(.5*(100-n))))/(n))-.25)/(-((n - .5*(100-n)))/(n)));
if (0<=s) && (s<=1)
s = s;
elseif s > 1
s = 1;
elseif (isnan(s)==1)
s = 1;
elseif (isreal(s)==0)
s = 1;
else
s = 1;
end
RTDlineark{k} = [k];
RTDlinearkn{k} = [n];
RTDlinearks{k} = [s];
end
0 Kommentare
Akzeptierte Antwort
Birdman
am 12 Feb. 2018
To get rid of that, your code can be improved as follows:
for k = 1:1:100
n = (100 / k);
s = sqrt((((.25*(n-(.5*(100-n))))/(n))-.25)/(-((n - .5*(100-n)))/(n)));
if (0<s) && (s<=1)
s = s;
elseif s > 1
s = 1;
elseif (real(s)>=0) && imag(s)~=0
s = 1;
end
RTDlineark{k} = [k];
RTDlinearkn{k} = [n];
RTDlinearks{k} = [s];
end
1 Kommentar
Walter Roberson
am 12 Feb. 2018
This happens to work with the data, but still suffers from the problem of using < and <= operators on complex quantities, as those operators are defined to ignore the complex component.
Weitere Antworten (2)
Andrei Bobrov
am 12 Feb. 2018
k = 1:100;
n = 100 ./ k;
s = sqrt( (.25*(n-.5*(100-n))./n-.25)./-(n - .5*(100-n))./n );
t = abs(imag(s)) > 0;
s(t) = t(t) + 0;
0 Kommentare
Walter Roberson
am 12 Feb. 2018
The relative comparison operations ignore the imaginary component. You need to do the test for imaginary first.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!