How to make a loop run multiple times for different values of a variable.

Thanks for taking the time to read this, I am new to matlab. This is the script I'm working on:
N=50
alpha=2.8
x1=0.2;
for i=1:N
x(i+1)= alpha*x1*(1-x1);
x1=x(i+1);
end
x1
I would like this script to run for numerous values of alpha and return the answers, so I can plug them into a function and graph them. Specifically I would like to have 120-1200 different values of alpha from 2.8 to 4.0. Right now I just want to know how to make the script run for multiple different values of alpha and I can try and figure out how to do the other part on my own. I tried to put alpha =linspace(2.8,4), but it gives me this error
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in lyanupov2 (line 5) x(i+1)= alpha*x1*(1-x1);
So I figure I'm just thinking of it wrong. I imagine this is a very simple problem for someone who knows Matlab even remotely well.

Answers (2)

hi, Tom
As you want to get 1200 differents values of alpha then you have to create an N by 1200 matrix :
N=50
alpha=linspace(2.8,4,1200);
x=zeros(N,length(alpha));
x1=0.2;
for j=1:1200
for i=1:N-1
x(i+1,j)= alpha(j)*x1*(1-x1);
x1=x(i+1,j);
end
end
Then you can plot only 10 graphs to see how it acts :
plot(x(:,1:10)) % plot 10 graphs for specific 10 alpha values
N = 50;
alpha = linspace(2.8, 4, 100);
x1 = 0.2;
x = zeros(N + 1, numel(alpha)); % Pre-allocate!!!
for i = 1:N
x(i+1, :) = alpha .* x1 .* (1 - x1);
x1 = x(i+1, :);
end
Here ".*" is the elementwise multiplication, instead of the matrix multiplication "*".

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Tags

Asked:

Tom
on 11 Feb 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!