My code is returning a complex array

3 visualizzazioni (ultimi 30 giorni)
Cameron
Cameron il 3 Nov 2022
Commentato: Torsten il 3 Nov 2022
x = -10:1:10;
for j = -10:1:10
y(j+11) = power(2.72,-.2j);
end
plot(x,y);
%im trying to plot the function e^(-.2x) so im trying to create an array to plot
  1 Commento
Torsten
Torsten il 3 Nov 2022
-.2j means -0.2*i where i is the imaginary unit (sqrt(-1)).

Accedi per commentare.

Risposta accettata

KSSV
KSSV il 3 Nov 2022
Modificato: KSSV il 3 Nov 2022
There is a typo in your code.
x = -10:1:10 ;
y = zeros(size(x)) ;
for j = 1:length(x)
y(j) = power(2.72,-0.2*j); %<---missed mulltiplication
end
plot(x,y);
No need to use loop.
x = -10:1:10;
y = exp(-0.2*x) ;
plot(x,y)

Più risposte (0)

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by