Effacer les filtres
Effacer les filtres

Why are complex values obtained for the below function (x) in the if condition?

1 vue (au cours des 30 derniers jours)
Navid
Navid le 14 Nov 2023
Modifié(e) : Matt J le 14 Nov 2023
Hi there! I'm facing an issue with the below function. The function is supposed to generate values for x that are not imaginary. However, I'm getting a warning: "Imaginary parts of complex X and/or Y arguments ignored." To be more specific, unlike the output shown in the figure, when y is equal to 0, the function should calculate x=0. Can you please assist me in finding out what could be causing this issue?
clc
close all
clear all
a = 280;
b = 580;
c = 0.35;
d =1.71E5;
e = 7;
f = 1+(3.5*(a/b));
g = d/(1+(0.002*e*(d/a)));
y = (0:2.5:b);
if any(y <= a)
x = (y/d)+(0.002*((y/a).^e));
if any(y > a)
x = 0.002+(a/d)+((y-a)/g)+(c.*(((y-a)/(b-a)).^f));
end
end
plot(x,y)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
xlabel('x')
ylabel('y')

Réponse acceptée

Matt J
Matt J le 14 Nov 2023
Because you are raising a negative number to a fractional power, e.g.,
(-2)^1.5
ans = 0.0000 - 2.8284i
  1 commentaire
Matt J
Matt J le 14 Nov 2023
Modifié(e) : Matt J le 14 Nov 2023
Perhaps,
a = 280;
b = 580;
c = 0.35;
d =1.71E5;
e = 7;
f = 1+(3.5*(a/b));
g = d/(1+(0.002*e*(d/a)));
y = (0:2.5:b);
x = (y/d)+(0.002*((y/a).^e));
r=(y>a);
x(r)= 0.002+(a/d)+((y(r)-a)/g)+(c.*(((y(r)-a)/(b-a)).^f));
plot(x,y)
xlabel('x')
ylabel('y')

Connectez-vous pour commenter.

Plus de réponses (1)

Torsten
Torsten le 14 Nov 2023
Maybe you mean
clc
close all
clear all
a = 280;
b = 580;
c = 0.35;
d =1.71E5;
e = 7;
f = 1+(3.5*(a/b));
g = d/(1+(0.002*e*(d/a)));
y = (0:2.5:b);
x = zeros(size(y));
idx = y<=a;
x(idx) = (y(idx)/d)+(0.002*((y(idx)/a).^e));
x(~idx) = 0.002+(a/d)+((y(~idx)-a)/g)+(c.*(((y(~idx)-a)/(b-a)).^f));
plot(x,y)
xlabel('x')
ylabel('y')

Catégories

En savoir plus sur Signal Generation, Manipulation, and Analysis dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by