Plot the functions x, x^3, e^x, and e^x over the interval 0 < x < 4 in MATLAB.

Plot the functions x, x^3, e^x, and e^x over the interval 0 < x < 4 in MATLAB. a. Rectangular paper b. On semilog paper (logarithm on the y-axis) c. On log-log paper Be sure to use an appropriate mesh of x values to get a smooth set of curves! Hint: check the documentation for these functions: plot, subplot, semilogy, loglog.

The Correct Answer and Explanation is:

To plot the functions xxx, x3x^3×3, exe^xex, and exe^xex over the interval 0<x<40 < x < 40<x<4 in MATLAB with different plotting styles (rectangular paper, semilog paper, and log-log paper), you can use the following code:

matlabCopyEdit% Define the x values over the interval 0 < x < 4
x = linspace(0.1, 4, 100); % Use linspace for smooth curves

% Define the functions
y1 = x;            % x
y2 = x.^3;         % x^3
y3 = exp(x);       % e^x
y4 = exp(x);       % e^x (repeated for clarity)

% Create the plots
figure;

% a. Rectangular paper
subplot(3,1,1); % Creates a 3x1 grid of plots and uses the first position
plot(x, y1, 'r', x, y2, 'g', x, y3, 'b', x, y4, 'm');
title('Rectangular Paper');
xlabel('x');
ylabel('y');
legend('x', 'x^3', 'e^x', 'e^x');

% b. Semilog paper (logarithm on the y-axis)
subplot(3,1,2); % Creates the second plot
semilogy(x, y1, 'r', x, y2, 'g', x, y3, 'b', x, y4, 'm');
title('Semilog Paper (Logarithm on Y-axis)');
xlabel('x');
ylabel('log(y)');
legend('x', 'x^3', 'e^x', 'e^x');

% c. Log-log paper (logarithm on both axes)
subplot(3,1,3); % Creates the third plot
loglog(x, y1, 'r', x, y2, 'g', x, y3, 'b', x, y4, 'm');
title('Log-Log Paper');
xlabel('log(x)');
ylabel('log(y)');
legend('x', 'x^3', 'e^x', 'e^x');

Explanation:

  1. Rectangular Paper:
    • In this plot, all axes are linear. The plot() function is used to plot the functions over the specified range of x-values. The legend() function helps differentiate the curves.
  2. Semilog Paper:
    • The y-axis is scaled logarithmically, but the x-axis remains linear. The semilogy() function is used to make this plot. This type of plot is useful when comparing values that vary exponentially or over multiple orders of magnitude, such as exponential growth (like exe^xex).
  3. Log-log Paper:
    • Both the x and y-axes are scaled logarithmically. The loglog() function is used to create this plot. This type of plot is useful for comparing functions that exhibit power-law behavior, where both the x and y values span several orders of magnitude.

The linspace(0.1, 4, 100) command generates 100 evenly spaced points between 0.1 and 4 to ensure smooth curves, as suggested.

Scroll to Top