for loop label/arguement

6 views (last 30 days)
Sara Ismail-Sutton
Sara Ismail-Sutton on 18 Apr 2021
Commented: Walter Roberson on 22 Apr 2021
Need to iterate over the bifurcation map n_trans time (n_trans specified above), this is the relevant bit of the code for my question.. which is pretty basic.. what is wrong with the below? I get the error message
"Error in solution: Line: 25 Column: 20
Invalid array indexing."
(no longer line 25 ofc...)
many thanks
x(1)=0.5;
for j=2:(n_trans-1)
x(j+1)=mu*x(j)(1-x(j));
end

Answers (1)

Walter Roberson
Walter Roberson on 18 Apr 2021
That looks suspiciously like an Octave message rather than a MATLAB message...
x(j+1)=mu*x(j)(1-x(j));
^^
MATLAB has no implied multiplication, not anywhere -- not even inside the binary language of the moisture vaporators.
  10 Comments
Sara Ismail-Sutton
Sara Ismail-Sutton on 21 Apr 2021
ok, the task is bifurcation diagram for logistic map, the assignment provides a template, just the for loops that I have wrote.
So, from what you say, I believe mu is a no-scalar , overall, but this concerns the second loop, so here it will be fixed and a scalar?
Thanks
mu_min=2.4; mu_max=4; %range of mu values
n_mu=500; %number of mu pixels
n_x=400; %number of x pixels
mu_edges=linspace(mu_min,mu_max,n_mu+1); %edges of mu pixels
mu=(mu_edges(1:n_mu)+mu_edges(2:n_mu+1))/2; %values of mu on which to perform computation
x_edges=linspace(0,1,n_x+1); %edges of x pixels
n_trans=200000; %transient iterations
n_data=100000; %number of x values per mu value
x_data=zeros(n_data,n_mu); %x-data used to construct figure
x_0=0.5; %initial condition
% WRITE THE COMPUTATIONAL ENGINE OF THE CODE.
% USE THE ALREADY DEFINED PARAMETERS AND VARIABLES: n_mu, mu, x_0, n_trans, n_data.
% YOUR FINAL RESULT WILL BE THE VARIABLE x_data, and this variable will be assessed.
for i=mu_min:mu_max
x(1)=0.5;
for j=2:n_trans
x(j)=mu*x(j-1)*(1-x(j-1));
end
x(1)= x(n_trans)
for k=2:n_data
x(k+1)=mu*x(k)*(1-x(k));
end
end
%%%%% bin data and plot image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_histogram=zeros(n_x,n_mu); %binned values of x
for i=1:n_mu
x_histogram(:,i)=histcounts(x_data(:,i),x_edges);
x_histogram(:,i)=255*x_histogram(:,i)/max(x_histogram(:,i));
end
colormap(flipud(gray(256))); brighten(-0.8); cmap=colormap;
im=image([mu_edges(1) mu_edges(end)], [x_edges(1) x_edges(end)], x_histogram);
set(gca,'YDir','normal');
xlabel('$\mu$','Interpreter','latex','FontSize',14);
ylabel('$x\;\;$','Interpreter','latex','FontSize',14);
title('Logistic Map Bifurcation Diagram','Interpreter','latex','FontSize',16)
Walter Roberson
Walter Roberson on 22 Apr 2021
for i=mu_min:mu_max
Should be
for i=1:n_mu
and
x(j)=mu*x(j-1)*(1-x(j-1));
should refer to mu(i)
Possibly some output should stored indexed at i

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!