plot y = (x^n * exp(-x)/(n!) in matlab

To plot the function y = (x^n * exp(-x)/(n!) in MATLAB, you can follow these steps:

  1. Define the value of n that you want to use in your plot. For this example, let's set n = 5.
main.m
n = 5;
7 chars
2 lines
  1. Create a vector of values for x. You can use the linspace function to create a vector of 100 values from 0 to 10.
main.m
x = linspace(0, 10, 100);
26 chars
2 lines
  1. Calculate the value of y for each value of x using the formula for the function.
main.m
y = (x.^n .* exp(-x))./factorial(n);
37 chars
2 lines
  1. Plot the function using the plot function.
main.m
plot(x, y);
12 chars
2 lines
  1. Add axis labels and a title to your plot.
main.m
xlabel('x');
ylabel('y');
title(['y = (x^', num2str(n), ' * exp(-x))/(n!)']);
78 chars
4 lines

The complete code looks like this:

main.m
n = 5;
x = linspace(0, 10, 100);
y = (x.^n .* exp(-x))./factorial(n);
plot(x, y);
xlabel('x');
ylabel('y');
title(['y = (x^', num2str(n), ' * exp(-x))/(n!)']);
160 chars
8 lines

gistlibby LogSnag