# MethodError: Cannot \`convert\` an object of type Array{Float64,1} to an object of type Float64

**URL:** <https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527>\
**Category:** New to Julia\
**Tags:** question, matlab\
**Created:** [July 4, 2020, 3:57pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527 "2020-07-04T15:57:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [July 4, 2020, 3:57pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/1 "2020-07-04T15:57:34Z")

</div>

‘’’  
I am using julia 1.4.2

```julia
nx = 60 ; # try changing this number from 41 to 81 and Run All ... what happens?
dx = 2/(nx-1);
nt = 25 ; #nt is the number of timesteps we want to calculate
dt = .025 ; #dt is the amount of time each timestep covers (delta t)
s=convert(Int64,round(0.5/dx,digits=0))
e=convert(Int64,round(1/dx,digits=0))
u=ones(nx)
u[s:e].=2.0

for n in 1:nt

           un=copy(u)

           for i in 2:nx-1

               u[i]=un[i].-un[i].*dt./dx .*(un[i].- un[i-1].+ un .* dt./dt.^2 .*(un[i+1].- 2 .* un[i].+un[i-1]))

           end

           u[1] =un[1].-un[1].*dt./dx .*(un[1].-un[end-1].+un.*dt./dt.^2 .*(un[2].-2*un[1].+un[end-1]))

           u[end] =un[end].-un[end].*dt./dx .*(un[end].-un[end-1].+nu.*dt./dt.^2 .*(un[1].-2*un[end].+un[end-1]))

       end

```

‘’’  
ERROR: MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64  
Closest candidates are:  
convert(::Type{T}, ::T) where T\<:Number at number.jl:6  
convert(::Type{T}, ::Number) where T\<:Number at number.jl:7  
convert(::Type{T}, ::Base.TwicePrecision) where T\<:Number at twiceprecision.jl:250  
…  
Stacktrace:  
[1] setindex!(::Array{Float64,1}, ::Array{Float64,1}, ::Int64) at .\array.jl:826  
[2] top-level scope at .\none:7

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 4, 2020, 4:04pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/2 "2020-07-04T16:04:15Z")

</div>

It would be best if you share a reproducible code because some of the variables are not defined in the code(`s`,`e`,`nu`). My guess is that one of the rhs is giving an array but lhs is single element in an array.

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [July 4, 2020, 4:13pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/3 "2020-07-04T16:13:56Z")

</div>

o sorry , I have editted ,

```julia
s=convert(Int64,round(0.5/dx,digits=0))
e=convert(Int64,round(1/dx,digits=0))

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 4, 2020, 4:33pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/4 "2020-07-04T16:33:17Z")

</div>

> [@hafez\_ahmad](#):
>
> ```julia
> for i in 2:nx-1
> 
> u[i]=un[i].-un[i].*dt./dx .*(un[i].- un[i-1].+ un .* dt./dt.^2 .*(un[i+1].- 2 .* un[i].+un[i-1]))
> 
> end
> 
> ```

In rhs, `... un .* dt ...` produces an array. Are you sure about the code? It would be better split the code into several lines to reason about it well. You do not need broadcasting when you use scalar operations. Also, there is `nu` that is not defined in lower lines.

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [July 4, 2020, 4:40pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/5 "2020-07-04T16:40:59Z")

</div>

thank for reply . that’s my mistake . nu will be un

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [July 4, 2020, 4:43pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/6 "2020-07-04T16:43:40Z")

</div>

```julia
for i in 2:nx-1
un=copy(u)
 u[i]=un[i].-un[i].*dt./dx .*(un[i].- un[i-1].+ un .* dt./dt.^2 .*(un[i+1].- 2 .* un[i].+un[i-1]))
 end

```

ERROR: MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64  
Closest candidates are:  
convert(::Type{T}, ::T) where T\<:Number at number.jl:6  
convert(::Type{T}, ::Number) where T\<:Number at number.jl:7  
convert(::Type{T}, ::Base.TwicePrecision) where T\<:Number at twiceprecision.jl:250  
…  
Stacktrace:  
[1] setindex!(::Array{Float64,1}, ::Array{Float64,1}, ::Int64) at .\array.jl:826  
[2] top-level scope at .\none:3

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [July 4, 2020, 4:50pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/7 "2020-07-04T16:50:48Z")

</div>

actually , I am trying to convert Matlab into Julia

```julia
clc
clear all
syms x nu t % Using the symbolic math toolbox
phi = exp(-(x-4*t)^2/(4*nu*(t+1))) + exp(-(x-4*t-2*pi)^2/(4*nu*(t+1)));
phiprime=diff(phi,x); %differentiate with symbolic math enabled
usym = -2*nu*(phiprime/phi)+4; 
ufunc=matlabFunction(usym); %Convert back to Matlab Function
nx = 101;
nt = 100; %nt is the number of timesteps we want to calculate
nu = 0.07;
dx = 2*pi/(nx-1);
dt = dx*nu; %dt is the amount of time each timestep covers (delta t)
x=linspace(0,2*pi,nx);
un=zeros(nx);
t=0;

u=ufunc(nu,t,x);

un = zeros(1,nx); %initialize our placeholder array un, to hold the time-stepped solution

for n=1:1:nt; %iterate through time
    un = u; %%copy the existing values of u into un
    for i=2:1:nx-1 %%now we'll iterate through the u array
    
     %%%This is the line from Step 1, copied exactly. Edit it for our new equation.
     %%%then uncomment it and run the cell to evaluate Step 2   
      
          % u(i) = un(i)-c*dt/dx*(un(i)-un(i-1)) 
u(i)=un(i)- un(i) * dt/dx *(un(i)-un(i-1))+nu* dt/dx^2 * (un(i+1)-2*un(i) +un(i-1));

    end
u(1) =un(1)- un(1) * dt/dx *(un(1)-un(nx-1))+nu* dt/dx^2 * (un(2)-2*un(1) +un(nx-1)) %Anfangswert 
u(nx)=u(1) %Endwert
    plot(x,u) %%Plot the results
    pause(0.001)
    hold on
    end
    u_analytical = ufunc(nu,n*dt,x);

    plot(x,u_analytical)
    pause(0.001)

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 4, 2020, 4:53pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-array-float64-1-to-an-object-of-type-float64/42527/8 "2020-07-04T16:53:13Z")

</div>

Then there is `nu` as constant and that `un` should be `nu` actually.
