Swopnil Ojha
Swopnil Ojha

Reputation: 11

Why is the for loop giving me weird results in julia?

I am getting weird results in the following nested for loop code. Can someone figure out what the problem is ?

function FAS(M::Float64,R::Float64,dij::Array{Float64,2},l::Float64, 
         w::Float64,i0::Int64,j0::Int64,pulsing_percent::Float64,β::Float64,
         ρ::Float64,σ::Float64,rad::Float64,k0::Float64,ϕ1::Float64,
         ϕ2::Float64,δ1::Float64,h::Float64)

    M0 = 10^((M+10.7)*(3./2.))*1.0e-7 #Nm 
    nw = size(dij)[1]
    nl = size(dij)[2]
    N = length(dij)
    Δl = l/nl 
    Δw = w/nw
    subfault_radius = sqrt(Δl*Δw/π)
    no_effective_subfaults = nl*pulsing_percent/100
    no_effective_subfaults = no_effective_subfaults/2
    vrup = 0.8*β

    if no_effective_subfaults < 1.
        no_effective_subfaults = 1.
    end     

    #declaration of variables
    dij_sum = sum(dij)
    t_end_max = 0.
    t-arrive_min = 10000.
    risetime_max = 0.0

    #initialization of variables    
    no_active_subfaults = zeros(Int64,nw,nl)
    f0ij = zeros(Float64,nw,nl)
    M0ij = zeros(Float64,nw,nl)
    risetimeij = zeros(Float64,nw,nl)
    subfault_distance = zeros(Float64,nw,nl)
    dur_subij = zeros(Float64,nw,nl)
    delay = zeros(Float64,nw,nl)


    for j in 1:nl
        for i in 1:nw            

        delay[i,j] = i-i0   

        end
    end   
    return delay
end

function main()
    location="F:\\Books\\Thesis\\Paper\\Scripts\\displacements.txt"
    dij = readdlm(location,',')
    M=7.9
    R = 60000.
    l = 195000.
    w = 150000.
    i0 = 6
    j0 = 6
    pulsing_percent = 50.0
    β = 3500.
    ρ = 28000.
    σ = 50*100000.
    rad = 0.55
    k0 = 0.05
    ϕ1 = 293.
    ϕ2 = 120.
    h = 15000.
    δ1 = 7.0
    value = FAS(M,R,dij,l,w,i0,j0,pulsing_percent,β,ρ,σ,rad,k0,ϕ1,ϕ2,δ1,h)
    return value
end

main()

I am getting this

 10000.0  10000.0  10000.0  10000.0  …  10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0  …  10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0

However if I do

delay[i,j] = i+(-1*i0)

I get

10×13 Array{Float64,2}:
 -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0
 -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0
 -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0
 -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0
  0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
  1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0
  2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0
  3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0
  4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0

which is the expected answer.

Can someone figure out what the problem is?

Upvotes: 0

Views: 93

Answers (2)

Swopnil Ojha
Swopnil Ojha

Reputation: 11

I figured out the problem. But don't know the reason behind this. Please figure out what is going on.

function FAS(nw,nl,i0,j0)
    delay = zeros(Float64,nw,nl)
    t-arrive_min= 1000
    for j in 1:nl
        for i in 1:nw

        delay[i,j] = i-i0

        end
    end
    return delay 
end

FAS(10,13,6,6)

Now if you are going to run this code then you are going to get

10000.0 10000.0 10000.0 10000.0 … 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 … 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0

Basically it is a typo there while declaring t-arrive_min= 1000 instead of t_arrive_min = 1000. and wherever the operator "-" occurred in the code it gives you the value to which the variable was set. It's a very unusual bug it should have reported error saying "-" operator not allowed while declaring variables.

Upvotes: 0

Chris Rackauckas
Chris Rackauckas

Reputation: 19132

Your question has user-error, i.e. what you think is the problem is not the problem. Most of your example is actually unnecessary. When I run the parts of the code which generate your return value:

nw = 10
nl = 10
delay = zeros(Float64,nw,nl)
i0 = 6
for j in 1:nl
    for i in 1:nw

    delay[i,j] = i-i0

    end
end

julia> delay
10×10 Array{Float64,2}:
 -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0
 -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0
 -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0
 -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0
  0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
  1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0
  2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0
  3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0
  4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0

it generates what you'd expect, so this code isn't the problem and you need to look elsewhere.

Upvotes: 1

Related Questions