if the imaginary part of a complex number is zero
22 views (last 30 days)
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?
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);
z(2)=fsolve(@(z) F(z), 30+1i,opts);
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
tt(v) must be 1 for both roots of the equation.
Gargi Patil on 12 Nov 2021
My understanding is that you would like to detect imaginary values equal to zero at the following line:
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.