Trouble Plotting Basic Function

1 回表示 (過去 30 日間)
Kevin
Kevin 2024 年 9 月 12 日
コメント済み: Sam Chak 2024 年 9 月 13 日
I'm having difficulty getting a function to plot correctly in Matlab. The function is:
f(x) = x^3 - sin(x) - e^x
I've tried using the below but the graph is not how it should appear:
X = -4:0.01:10;
Y = X.^3 - (sin(X)) - (exp(X));
plot(X,Y)
  2 件のコメント
Sam Chak
Sam Chak 2024 年 9 月 12 日
I suggest that you plot each component so that you can clearly see that the cubic function and the exponential function are unbounded, with the latter having a significant impact. Of course, it would be beneficial if you could provide a sketch of the expected plot.
X = -4:0.01:10;
Y1 = X.^3;
Y2 = sin(X);
Y3 = exp(X);
Y = Y1 - Y2 - Y3;
tl = tiledlayout(2, 3);
nexttile
plot(X, Y1), grid on, title('x^{3}')
nexttile
plot(X, Y2), grid on, title('sin(x)')
nexttile
plot(X, Y3), grid on, title('exp(x)')
nexttile([1 3])
plot(X, Y), grid on, title('y = x^{3} - sin(x) - exp(x)')
xlabel(tl, 'x')
Sam Chak
Sam Chak 2024 年 9 月 13 日
If is a Gaussian distribution function and all three components are appropriately scaled, then the contribution of each function can be observed at different intervals. In the range , the cubic function has little influence. The sine wave affects the overall sinusoidal pattern of the signal, while the Gaussian function has a strong presence at .
X = -4:0.01:10;
Y1 = 0.002*X.^3;
Y2 = sin(X);
Y3 = exp(-X.^2);
Y = Y1 - Y2 - Y3;
tl = tiledlayout(2, 3);
nexttile
plot(X, Y1), grid on, title('0.001 x^{3}')
nexttile
plot(X, Y2), grid on, title('sin(x)')
nexttile
plot(X, Y3), grid on, title('exp(-x^{2})')
nexttile([1 3])
plot(X, Y), grid on, title('y = 0.001 x^{3} - sin(x) - exp(-x^{2})')
xlabel(tl, 'x')

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

回答 (1 件)

Star Strider
Star Strider 2024 年 9 月 12 日
It appears to be coded correctly.
How is it supposed to appear?
X = -4:0.01:10;
Y = X.^3 - (sin(X)) - (exp(X));
figure
plot(X,Y)
Y = X.^3 - (sin(2*pi*X)) - (exp(X));
figure
plot(X,Y)
I thought about multiplying the sin argument by , however that doesn’t siignificantly change the reesul.
.

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by