if the imaginary part of a complex number is zero

조회 수: 45 (최근 30일)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 11월 9일
답변: Gargi Patil 2021년 11월 12일
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