Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(MATLAB) Problem 2 (15 Points) Given the logistic equation: x(n1)r*x(n)-[1- x(n)

ID: 3282644 • Letter: #

Question

(MATLAB)

Problem 2 (15 Points) Given the logistic equation: x(n1)r*x(n)-[1- x(n)] Write a code that performs the following: Asks the user for an initial condition o Make sure that value is between 0 and 1 Create an array that has all the values that r can take. This array is meant to start at 0, finish at 4, and have 100 values. Calculates the first 100 values of x (including the initial condition given by the user. Creates a “movie" that shows the plot x(n) vs. n for all the values of r · o The value of r needs to be displayed in the title of the plot o For this step, you will be using the functions pause0 and clf. Google them and ask questions if needed.

Explanation / Answer

Please find the matlab code logisticAnimation.m below:

% implementing the logistic equation
% x(n + 1) = r x(n) (1 - x(n))

% initial condition
x0 = input('Please enter the initial condition x(0): ');

% initial condition check 0 < x0 < 1
if x0 <= 0 || x0 >= 1
    error('Initial condition should be between 0 and 1, both exclusive')
end

% row vector of r
r = linspace(0, 4, 100);

% row vector of x
x = zeros(1, 100);
x(1) = x0;

for iterR = 1: length(r)
  
    for iterX = 1: length(x)-1
        x(iterX + 1) = r(iterR) * x(iterX) * (1 - x(iterX));
    end
  
    % plots x(n) vs n
    graph = plot(1: 100, x, 'LineWidth', 1.2);
    xlim([1, 100])
    xlabel('n')
    ylabel('x(n)')
    str = sprintf('r = %0.2f', r(iterR));
    title(str)
  
    grid on
    grid minor
  
    drawnow
    frame(iterR) = getframe(gcf);
  
    % pause execution for 0.3 s
    pause(0.3);
    delete(graph)
end

% forms video movie.avi

video = VideoWriter('movie.avi', 'Uncompressed AVI' );
open(video)
writeVideo(video, frame)
close(video)
close all