# Using matlab to plot solutions to a difference equation

It was suggested to me to use matlab for a term project for my ODE class, I'd like to take a look at several bio models, mostly population growth. The problem is that I have no idea how matlab works.

For instance, I'd like to plot the first X terms of the solutions to an equation, such as $$x_{n+1}=rx_n(1-x_n)$$, $$x_0=0.1$$, where $$r = 0.9, 1.5, 3, 4$$ and then draw cobweb maps of each case.

Any pointers on how to code this or if there are any quick and easy sources on how to figure out how to do this, it would be greatly appreciated. I took one look at the program and couldn't figure anything out, and it being so early in the semester, I haven't had any experience with this program, I'm not sure if we'll even be using it, but I wanted to get a head start in figuring out the program.

This is the code to generate your $$x_n$$ estimates in a matrix x, the $$n^{th}$$ row corresponds to the value of $$x_{n-1}$$ for a different value of $$r$$, specified by the column index of $$x$$. I just plotted a normal plot so you can see the evolution.

X = 100 % first X terms
R = [0.9,1.5,3,4]; % these are the r values
x = zeros(X,length(R)); % initialise x to all zeros
x(1,:) = 0.1 % this is x0
k=1; % set a counter to 1
for r=R
for n = 1:X
x(n+1,k) = r*x(n)*(1-x(n));% for each r and n update x
end
k=k+1;
end
plot(x)% plot them all
legend('r=0.9','r=1.5','r=3','r=4')


You should be able to see this

For a cobweb plot, you need to generate a subplot for each $$r$$, i.e.

figure
for k = 1:length(R)
subplot(2,2,k)
for n = 1:(X-1)
plot([x(n,k),x(n,k)],[x(n,k),x(n+1,k)])
hold on
plot([x(n,k),x(n+1,k)],[x(n+1,k),x(n+1,k)])
end
title(['Cobweb for r = ', num2str(R(k))])
end


Now, you should see this:

This shows you that for all gives $$r$$ values, you're going to $$x_n \rightarrow 0$$