I want my code to display all the five results after each iteration

I want my code to display all the five results after each iteration and also select the maximum among the 5 results and display it. When I run it I only get one output. All five results are not showing. I have an array of five elements each. I am writing a code to take an element and find the radius using the formula in my code at each of this values, I have 5 values in each array. Any help will be appreciated please. Attached is the code.

a=[0 2.5 -2.5 2 -2.5]
b=[0 0.3 0.25 -2.5 -2.25]
r=[4 8 9 8 8]
(x,y)=(0,0)

function radius(x,y,a,b,r)
n=Float64
for (i,m) in enumerate(a)
R=Float64

         push!(R, (((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] ) )

end
push!(n, R)

return n

end

Please format your code by using 3 backticks (i.e. ```) on the line before your code and 3 on the line after it.

The code that you posted throws an error, because it has two sources of error:

  1. You define R as a local vector inside the for loop, and then refer to it outside the loop.
  2. n is defined as a vector of Float64 (as well as R), but you push! vectors like R to n.

This modification, assigning the result of the formula to a scalar R, and pushing it to n inside the loop, does work:

function radius(x,y,a,b,r)
    n=Float64[]
    for (i,m) in enumerate(a)
        R = (((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] ) 
        push!(n, R)
    end
    return n
end
julia> radius(x,y,a,b,r)
5-element Vector{Float64}:
  4.0
 10.517935662402834
 11.512468905280222
 11.201562118716424
 11.363406011768427

But it’s more readable, elegant and flexible if you define the function to work on scalars, and then broadcast it (with the “dot notation”):

julia> radius(x,y,a,b,r) = ((a-x)^2 + (b-y)^2)^0.5 + r
radius (generic function with 1 method)

julia> radius.(x,y,a,b,r)
1×5 Matrix{Float64}:
 4.0  10.5179  11.5125  11.2016  11.3634

(Extra note: you are defining a, b, and r as row matrices; if your intention is using one-dimensional vectors, you should define them with commas between values, not spaces.)

2 Likes

What if I want only the maximum number out of the 5 values to display. How do I go about that to get only the maximum number displayed please?

Sounds like you are looking for maximum

Yes. How do I get only the maximum number out of the 5 solutions displayed please?

If a is a vector, maximum(a) will return its largest element. In this case, maximum(radius.( ... )) should do it.