Trouble Plotting Basic Function

8 views (last 30 days)
Kevin
Kevin on 12 Sep 2024
Commented: Sam Chak on 13 Sep 2024
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 Comments
Sam Chak
Sam Chak on 12 Sep 2024
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 on 13 Sep 2024
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')

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 12 Sep 2024
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.
.

Categories

Find more on Labels and Styling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!