Trouble Plotting Basic Function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
2024년 9월 12일
Hi @Kevin
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
2024년 9월 13일
Hi @Kevin
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
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1771355/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1771360/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1771365/image.png)
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
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1771345/image.png)
.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!