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 CenterFile Exchange에서 2-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