Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate this Octave code to Julia?

Tags:

octave

julia

I want to make a logistical map in Julia and I am having difficulties. I already know how to do it in Octave, how could I turn this code to Julia? My difficulty is mainly in the "map [i,:]" part.

#Creates a vector of initial conditions and "r"
x=rand(150,1);
r=(2.51:.01:4);

#Transpose the r
r=r';

#Makes 300 times the product of each element of r for each element of x
for i=1:300
x=r.*x.*(1-x);
end

#Makes 200 times the product of each element of r for each element of x and stores each value of x on line i of the "map" matrix
for i=1:200
x=r.*x.*(1-x);
map(i,:)=x;
end

#Plots each column of the map for each element o r
plot(r,map,'.')
like image 601
João Vitor Vieira Flauzino Avatar asked May 10 '21 18:05

João Vitor Vieira Flauzino


Video Answer


2 Answers

Starting here:

#Creates a vector of initial conditions and "r"
x=rand(150,1);

This isn't a vector, but a matrix of size 150x1. If you want a vector, write:

x = rand(150)

As for this:

r=(2.51:.01:4);

#Transpose the r
r=r';

It's a bit unclear, but I think you want a vector of the same length as x, in that case you should not transpose it, because that turns it into a 1x150 matrix. So instead write

r = 2.51:0.01:4
# or, maybe better
r = range(2.51, 4; length=length(x))

Next:

#Makes 300 times the product of each element of r for each element of x
for i=1:300
x=r.*x.*(1-x);
end

Instead of creating a new variable x at each iteration, update x in-place, and remember to dot all the operators here:

for i in 1:300
    x .= r .* x .* (1 .- x) # dot everything
end

I don't really understand what's going on with your code here, it seems to just do more of the same, a bit arbitrarily. More importantly, you try to update map before you have defined it. Furthermore, you shouldn't really use the name map because that's the name of an important built-in function in Julia.

#Makes 200 times the product of each element of r for each element of x and stores each value of x on line i of the "map" matrix
for i=1:200
x=r.*x.*(1-x);
map(i,:)=x;
end

Alternative. Pre-define a matrix, call it something other than map, and store the information column-wise, not row-wise, since that it much more efficient, both in Matlab/Octave and Julia

M = similar(x, length(x), 200)
for i in 1:200
    x .= r .* x .* (1 .- x)
    M[:, i] .= x  # use square brackets, not parens
end

BTW: your code becomes much more readable if you put in spaces between operators. Cramming everything together gives a messy appearance. Also, use indentation for readability. And drop all the semicolons.

like image 177
DNF Avatar answered Oct 01 '22 02:10

DNF


I haven't written julia in a while so there may be more efficient ways of doing this now, but here is a more or less direct equivalent to your octave code.

using PyPlot

x = rand( 150, 1 );
r = reshape( 2.51:.01:4, (:, 1) );

for i in 1:300
    global x
    x = r .* x .* (1 .- x);
end

Map = Matrix{Float64}(undef, 200, 150);

for i in 1:200
    global x, Map
    x = r .* x .* (1 .- x);
    Map[i:i,:] .= transpose(x);
end

for i in 1:length(r)
   plot( r, Map[i,:], "." )
end
like image 40
Tasos Papastylianou Avatar answered Oct 01 '22 03:10

Tasos Papastylianou