フィルターのクリア

how can I plot y = e^(-2x)Sin2x and its derivative on the same graph using two different colors?

10 ビュー (過去 30 日間)
I'm learning Matlab for the first time and I was able to get the first graph working but i'm having trouble getting the graph of the derivative to work. I keep getting an error message that "vectors must be the same lengths" and that i have an error in line 9.
x = (0:0.02:5);
y= exp(-2*x).*sin(2*x);
subplot(1,2,1)
plot(x,y),xlabel('x'),ylabel('y'), axis([0 5 -1 1])
hold on
diff(y) %differentiation
f = diff(y);
plot(x,f,'r'),xlabel('x'),ylabel('f'), axis([0 5 -1 1]) <-------line 9

採用された回答

Star Strider
Star Strider 2015 年 9 月 9 日
The easiest solution is to use the gradient function rather than diff. The gradient function produces a more accurate estimate and the output is the same length as the input, while the output of diff is one element shorter, owing to the different ways the two are calculated.
  3 件のコメント
bachir
bachir 2015 年 9 月 9 日
If v is a scalar, gradient(f,v) = diff(f,v). If v is an empty symbolic object, such as sym([]), then gradient returns an empty symbolic object.
Star Strider
Star Strider 2015 年 9 月 10 日
My pleasure.
The Symbolic Math Toolbox gradient function and the core MATLAB gradient function do somewhat different things. I was intending that you use the core MATLAB gradient function to calculate the numerical derivative of the vector y, essentially the equivalent of the Symbolic Math Toolbox diff function.

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

その他の回答 (1 件)

Gilang Setyawan Y.P.
Gilang Setyawan Y.P. 2022 年 3 月 7 日
x = (0:0.02:5);
y= exp(-2*x).*sin(2*x);
subplot(1,2,1)
plot(x,y),xlabel('x'),ylabel('y'), axis([0 5 -1 1])
hold on
diff(y) %differentiation
f = diff(y);
plot(x,f,'r'),xlabel('x'),ylabel('f'), axis([0 5 -1 1])

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by