sqrt(complex(x)) error

26 Ansichten (letzte 30 Tage)
Syahirah Eshah
Syahirah Eshah am 7 Feb. 2020
Kommentiert: Walter Roberson am 6 Feb. 2024
The simulink circuit that I built has two different codes. but the error tells me that I need to put sqrt(complex(x)) which I do not use in my code. The only sqrt that I used are used in command form,%. It should not effect the program overall.
Below are the codes where I think the error comes from
function [THETA_1, THETA_2, THETA_3, THETA_4] = fcn(Px,Py,Pz)
%determine THETA_1
test = 2;
if test == 1
Px = -0.02; Py = 0; Pz = 397.35;
elseif test ==2
Px = -224.52; Py = 156.22; Pz = 387.34;
elseif test == 3
Px = 63.37; Py = 14.74; Pz = 377.35;
elseif test == 4
Px = 50.73; Py = 56.5; Pz = 367.35;
elseif test == 5
Px = 247.56; Py = -93.02; Pz = 357.35;
elseif test == 6
Px = 7.04; Py = -140.77;Pz = 347.36;
elseif test == 7
Px = 24.77; Py = -25.69; Pz = 337.34;
elseif test == 8
Px = -41.65; Py = 65.76; Pz = 327.36;
elseif test == 9
Px = -60.48; Py = -49.01; Pz = 317.37;
elseif test == 10
Px = -37.99; Py = -67.95; Pz = 307.35;
end
THETA_1 = atand(Py/Px);
%how to get c?
c = (Px^2 + (300 -(Pz+72)^2))^(1/2);
b=250; a=160;
THETA_C = acosd((a^2 + b^2 - c^2)/ 2*a*b);
%THETA_c = real (THETA_C);
THETA_3 = 180 - THETA_C;
THETA_A = asind(a/c)*(sind(THETA_C));
%THETA_a = real (THETA_A);
THETA_B = asind(b/c)*(sind(THETA_C));
%THETA_b = real (THETA_B);
THETA_E = atand(300/Px);
%THETA_e = real (THETA_E);
THETA_G = 90 -(THETA_E);THETA_D = THETA_G;
%h = sqrt(complex(300^2 + Px^2));
%h/sind(THETA_H) = c/sind(THETA_G) = (Pz+72)/sind(THETA_F)
%(Pz+72)/sind(THETA_F)= c/sind(THETA_G);
% c *sind(THETA_F) = (Pz+72)* sind(THETA_G)
%sind(THETA_F)= (sind(THETA_G)*(Pz+72))/c;
THETA_F = asind((sind(THETA_G)*(Pz+72))/c);
%THETA_f = real (THETA_F)
THETA_2 = 180 - THETA_A - THETA_D - THETA_F;
THETA_H = 180 - THETA_G - THETA_F;
THETA_4 = 180 - THETA_B - THETA_H;
Below are the error that is shown in which I do not know where the error comes from. I really hope someone can point where my mistakes are. Thank you.
An error occurred while running the simulation and the simulation was terminated
Caused by:
Domain error. To compute complex results from real x, use 'sqrt(complex(x))'
  1 Kommentar
James Tursa
James Tursa am 7 Feb. 2020
The error is caused by having an argument to sqrt( ) that is negative. Or it can be caused by an argument to asin( ) or acos( ) that has magnitude greater than 1.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 7 Feb. 2020
Bearbeitet: Walter Roberson am 7 Feb. 2020
c = (Px^2 + (300 -(Pz+72)^2))^(1/2)
That is a square root.
You have 300 minus something that is squared. You want (300 minus something) squared if you want to be sure that you are are always taking the square root of a positive value.
  3 Kommentare
Lalitesh
Lalitesh am 6 Feb. 2024
Hello, I am not expert in programming and I have faced the same issue. How to avoid this error ? could you please reply me in detail ?
Walter Roberson
Walter Roberson am 6 Feb. 2024
c = (Px^2 + complex(300 -(Pz+72)^2))^(1/2)
would be the work-around for the case where you are deliberately allowing taking the square root of a negative value.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Denoising and Compression 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!

Translated by