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

‘’’
I am using julia 1.4.2

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

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.

o sorry , I have editted ,

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

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.

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

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

actually , I am trying to convert Matlab into 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)

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

1 Like