for loop label/arguement

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)

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

sorry, i'm really confused with your answer, thanks ..
You need to explicitly include * between two terms if you want to multiply.
x = 5;
y = 3*x % works
y = 3x % does not work
z = xy % does not work
okay sure, my bad that makes sense.. but that's not what is effecting the error message here is it?
Yes it is. It is seeing variable(index)(index) and saying that you cannot do two () index operations in a row.
okay many thanks, now I am getting the error
" Unable to perform assignment because the left and right sides have a different number of elements.
Error in solution (line 25)
x(j)=mu*x(j-1)*(1-x(j-1));"
Your mu appears to be non-scalar.
We do not have your complete source code. But if j is non-scalar then x(j-1) would be non-scalar and x(j-1)*(1-x(j-1)) would be an error unless j-1 is a square matrix, in which case you would get a result the same size as j-1 . But that would be fine for storing into x(j) . So the problem is not the j, and must be the mu. For example mu might be empty.
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)
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 Particle & Nuclear Physics in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 18 Apr 2021

Commented:

on 22 Apr 2021

Community Treasure Hunt

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

Start Hunting!