here, x is a range, so you need to broadcast your subtraction like
x .- T
as you’re trying to subtract T from every element of x, not from x itself. The error tells you this - there is no method for subtracting a Float64 number from a StepRangeLen object.
Incidentally, please make sure to quote your code by using triple backticks ``` to make it easier to read.
would you please check it , I am still getting error
using PyPlot
using DifferentialEquations
function rhs(dudt,u,A::Array{Float64,1},t)
N=Integer(A[1]);x0=A[2];xN=A[3]; c=A[4];
x=range(x0,step=xN,stop=N);
dx=x[2]-x[1];
# Periodic boundary condition
dudt[1]=-c*(u[1]-u[N])/dx;
for n in range(2,N-1)
dudt[n] =- c*(u[n]-u[n-1])/dx;
end
end
N=100;x0=0.;xN=10.;c=1.;
T=0.5;
Parameters of ODE system
A=[N, x0 ,xN ,c];
x=range(x0,step=xN,stop=N);
Exact solution
uexc=exp.(-(x.-T.-2).*(x.-T.-2));
Initial value
u0=exp.(-(x.-2).*(x.-2));
Problem definition of ODE in Julia
tspan = (0.0,0.5)
prob = ODEProblem(rhs,u0,tspan,A)
Solving ODE
sol = solve(prob,saveat=0.5)
gcf()
PyPlot.plot(x,u0,label=“Initial value”)
PyPlot.plot(x,sol[end],label=string("Solution at t= ",T))
PyPlot.plot(x,uexc,label=string("Exact solution at t= ",T))
PyPlot.legend()
PyPlot.grid(linestyle=“dotted”)
PyPlot.xlabel(“x”)
PyPlot.ylabel(“u”)
Unfortunately I don’t have time to install all the required packages to check what is going wrong here - what is the error you’re getting? Can you reduce your post down to the area that is causing the actual problem?
Also, did you read my suggestion above about quoting your code in triple backticks (```) to make it more legible?
In the rhs function, you are indexing the 11-element array u at index N==100. Try something like this:
function rhs(dudt,u,A::Array{Float64,1},t)
N=Integer(A[1]);x0=A[2];xN=A[3]; c=A[4];
x=range(x0,step=xN,stop=N);
dx=x[2]-x[1];
# Periodic boundary condition
dudt[1]=-c*(first(u)-last(u))/dx;
for n in 2:length(u)
dudt[n] =- c*(u[n]-u[n-1])/dx;
end
end