Problem multiplying exponential function with negative value

24 vues (au cours des 30 derniers jours)
Hasib Ryan Rahman
Hasib Ryan Rahman le 11 Fév 2022
Modifié(e) : Voss le 11 Fév 2022
I want to plot x(t) = t*(e−0.15t), − 20 ≤ t ≤ 20
I've tried:
t = -20:1:20;
x(t) = t.*(exp(-0.15*t));
Array indices must be positive integers or logical values.
plot(t,x(t))

Réponse acceptée

Voss
Voss le 11 Fév 2022
This statement
x(t) = t.*(exp(-0.15*t));
says to set the value of x at indexes given by t to the expression on the right-hand side. Indexes must be positive integers (1, 2, 3, ...), but t has some negative values and a zero value in it, so that's why the error happens.
In fact the syntax for what you want to do is slightly simpler. You can just say:
t = -20:1:20;
% no need to specify indexes into x, just set the whole thing:
x = t.*(exp(-0.15*t));
Then to plot:
plot(t,x)
  1 commentaire
Voss
Voss le 11 Fév 2022
Modifié(e) : Voss le 11 Fév 2022
That makes t and x each vectors. Alternatively, you can make x a function of t if you want, and do the same thing:
x = @(t)t.*(exp(-0.15*t));
In this case the syntax x(t) evaluates the function x at the values of t given in t. Then your original syntax in the plot statement would be correct:
t = -20:1:20;
plot(t,x(t));

Connectez-vous pour commenter.

Plus de réponses (1)

Yongjian Feng
Yongjian Feng le 11 Fév 2022
Try this:
t = -20:1:20;
x_t = t.*(exp(-0.15*t));
plot(t, x_t)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by