Array Indices error while plotting an exponential function.

I am tasked to plot a function f(t) = exp(10*t(t-1))*sin(20*pi*t) with 0 < t <= 1. Here is the code I made:
t=0:0.1:1;
f(t)=exp(10*t(t-1))*sin(20*pi*t);
plot(t,f(t))
But I get the error message:
Array indices must be positive integers or logical values.
Error in Homework_Assignment_2 (line 6)
f(t)=exp(10*t(t-1))*sin(20*pi*t);
During the lecture my Professor had no issue with having "x" or "t" increments starting with 0. Is there something wrong with my code?

3 件のコメント

dpb
dpb 2025 年 8 月 31 日
"Array indices must be positive integers..."
Your instructor used the value of t as the variable, not as an index into an array as you've written...
But, it's not at all clear to me just what was/is intended in the original equation of
f(t) = exp(10*t(t-1))*sin(20*pi*t)
with the t(t-1) expression.
Janeth
Janeth 2025 年 8 月 31 日

Here's the homework question. I simply typed the function f(t) into Matlab.

Janeth
Janeth 2025 年 8 月 31 日

I thought I could place f(t) as the variable name, as you would label a function on paper. But after viewing yours and James comment I see that it's not the same on Matlab.

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2025 年 8 月 31 日
編集済み: James Tursa 2025 年 8 月 31 日
Maybe this is what you want. Use f instead of f(t), and use .* (element-wise multiply) instead of * (matrix multiply).
The syntax t(t-1) is telling MATLAB you want to index into the t variable usine t-1 as indexes. That is not what you want. You have to multiply them together. In this case, since t is an array, you also can't just do t*(t-1) because in MATLAB the * operator is matrix multiply. You want to do element-wise multiplication, which is the.* operator. And you need the .* operator in other places as well because you want to do element-wise multiplication between arrays and not matrix multiply.
The f(t) syntax is also not what you want. That would typically be used if you are defining a symbolic function f with symbolic variable t. In this case, you are simply creating an array of numeric values so just use the simple variable name f without any (t) part.
t=0:0.1:1;
f = exp(10*t.*(t-1)).*sin(20*pi*t);
plot(t,f)
grid on

1 件のコメント

Janeth
Janeth 2025 年 8 月 31 日

Thank you, I completely forgot about the element-wise multiplication.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2025 年 8 月 31 日

コメント済み:

2025 年 8 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by