if the imaginary part of a complex number is zero

24 次查看(过去 30 天)
Hello
Here is my code where I want to see if the imaginary part of a number is zero then int=int+1; however, the imaginary part is not considered as zero though using the command by matlab documentation. what should i do?
int=0
F = @(z)((z-20.6667)^2+((-0.5*(z-10))-14.6667)^2-18.1353^2);
opts = optimoptions('fsolve', 'Display', 'off');
z(1) = fsolve(@(z) F(z), 1+1i, opts);
w(1)=-0.5*(z(1)-10);
z(2)=fsolve(@(z) F(z), 30+1i,opts);
w(2)=-0.5*(z(2)-10);
for v=1:2
tt(v)=~any(imag(z(v)));
if (z(v)>10) && (z(v)<=20 || abs(z(v)-20)<1e-6 )&& tt(v)==1
if v==2 && abs(z(1)-z(2))<1e-6
int;
else
int=int+1;
Points(int,:)=[z(v) w(v)];
end
end
end
tt(v) must be 1 for both roots of the equation.

回答(1 个)

Gargi Patil
Gargi Patil 2021-11-12
Hi,
My understanding is that you would like to detect imaginary values equal to zero at the following line:
tt(v)=~any(imag(z(v)));
However, upon closest inspecting the imaginary values of z, they are not exactly zero. For example, the imaginary value of z(1) is 0.000000002392847.
To consider this value as zero, the round function can be used as follows:
nearestDecimalDigit = 4; %set the number of digits as required
tt(v)=~any(round(imag(z(v)), nearestDecimalDigit)); %round to the nearest 4 decimal digits
This sets both the values of tt as 1.

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by