Visualizing Logarithmic Growth with MATLAB

This tutorial guides you through creating a MATLAB script called 'thisPlot' that compares the logarithmic growth of factorials to a linear function.

Here's what the script does:

  1. User Input: Prompts the user to enter a positive integer greater than 5.
  2. Factorial Calculation: Calculates the factorial for each number from 1 to the entered value.
  3. Logarithmic Plot: Generates a plot displaying the logarithmic values of the calculated factorials.
  4. Linear Comparison: Adds a linear line representing the equation y = x to the graph.
  5. Dual-Axis Display: Utilizes the 'plotyy' function to display the linear values on a separate y-axis for enhanced clarity.

Here's the MATLAB code for the 'thisPlot' script:

% Step a: Ask the user to enter a positive number N
N = input('Enter a positive number greater than 5: ');

% Step b: Calculate the factorial for each number from 1 to N
factorials = factorial(1:N);

% Step c: Display a graph of logarithmic growth
figure;
yyaxis left;
plot(1:N, log(factorials));
title('Logarithmic Growth');
xlabel('Number');
ylabel('Log Factorial');

% Step d: Add a linear line to the graph
hold on;
yyaxis right;
plot(1:N, 1:N, 'r--');
ylabel('Linear Line');

% Step e: Use plotyy for plotting linear values on the right-hand axis
ax = gca;
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = 'r';

% Adjust the plotyy appearance
yyaxis left;
ax.YAxis(1).Label.Color = 'k';
yyaxis right;
ax.YAxis(2).Label.Color = 'r';

Explanation:

  • User Input (Step a): The 'input' function prompts the user to enter a value and stores it in the variable 'N'.
  • Factorial Calculation (Step b): The 'factorial' function calculates the factorial of each number from 1 to 'N' and stores the results in the 'factorials' vector.
  • Logarithmic Plot (Step c):
    • 'figure' creates a new figure window for the plot.
    • 'yyaxis left' sets the left y-axis as the active axis.
    • 'plot(1:N, log(factorials))' plots the logarithm of each factorial value against its corresponding number.
    • 'title', 'xlabel', and 'ylabel' set the title, x-axis label, and y-axis label, respectively.
  • Linear Comparison (Step d):
    • 'hold on' retains the current plot and allows for adding new plots on top.
    • 'yyaxis right' sets the right y-axis as the active axis.
    • 'plot(1:N, 1:N, 'r--')' plots a red dashed line representing the equation y=x.
  • Dual-Axis Display (Step e):
    • 'gca' gets the current axes handle.
    • 'ax.YAxis(1).Color' and 'ax.YAxis(2).Color' set the colors of the left and right y-axes to black and red, respectively.
    • The remaining lines further customize the appearance of the y-axis labels.

This script provides a clear and visually appealing comparison between the logarithmic growth of factorials and a linear function, making it a useful tool for understanding the differences in their growth rates.

MATLAB Plotting Tutorial: Visualizing Logarithmic Growth with Linear Comparison

原文地址: https://www.cveoy.top/t/topic/k5T 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录