Plotting an equation raised to a variable.

1 回表示 (過去 30 日間)
Adam Hermon
Adam Hermon 2019 年 12 月 14 日
編集済み: Ridwan Alam 2019 年 12 月 17 日
Hello, I am trying to plot an equation that has a variable as an exponent, but I am getting errors that the matrix dimensions must agree. Is there a specific way in matlab that this has to be done for it to graph? Any help would be appreciated, thanks in advance.
n = 0:2:10;%first 6 non-zero derivatives
a_n = 12*(40).^n.*((-1).^(n/2))./(factorial(n));%first 6 non-zero terms
% Functions to plot
t = linspace(0,0.2,400);%select points in the range to plot
fun1 = a_n(1)*t.^0;%n=0
%Want to implement fun1 like this but that is having errors.
fun1 = a_n(1)*(t.^(n))
  2 件のコメント
Ridwan Alam
Ridwan Alam 2019 年 12 月 14 日
what is a_n(1) and t? I am assuming your n is the variable array, right?
Adam Hermon
Adam Hermon 2019 年 12 月 14 日
Hey, thanks for your reply, I should have made that clearer. a_n is a general expression and n is the array of values I need to use. I know the first function can just be written as a_n(1)*t^n, but it doesn't work as like when I use zero.
n = 0:2:10;%first 6 non-zero derivatives
a_n = 12*(40).^n.*((-1).^(n/2))./(factorial(n));%first 6 non-zero terms
% Functions to plot
t = linspace(0,0.2,400);%select points in the range to plot
fun1 = a_n(1)*t.^0;%n=0

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

回答 (1 件)

Ridwan Alam
Ridwan Alam 2019 年 12 月 15 日
編集済み: Ridwan Alam 2019 年 12 月 17 日
fun1 = a_n(1)*(t.^(n))
.^ is a pointwise (elementwise) operation, that means it operates on arrays of same length or one of the operator needs to be an scalar. In your code, length(n) is 6 and length(t) is 400. Hence, the error is thrown.
I assume you are looking for something like this:
fun1 = a_n(1)*(t.^(n(1)));
plot(t,fun1); hold on;
fun2 = a_n(2)*(t.^(n(2)));
plot(t,fun2);
But if you really want to variables on the right side of fun1, you can make t the same size of n:
t = linspace(0,0.2,length(n));
fun1 = a_n(1)*(t.^(n)); % now n and t are arrays of same size
Hope this helps.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by