1

My goal is to solve lorenz equations and plot them as it shows in the figure. I have two different initial conditions [x0, 1, 0] and x0= 0 then x0 =1* 10^-5 the two values of rho are ρ= 14 and ρ=28. I wrote this code in julia but I'm getting errors says "Got exception outside of a @test UndefVarError: u not defined lorenz_solver(x₀::Float64, ρ::Int64)" any idea on how to solve this issue enter image description here

using DifferentialEquations
using Plots
using LaTeXStrings

function lorenz!(t,p,u,du)
    x, y ,z = u
    sigma, ρ, beta = p
    du[1] = sigma*(u[2]-u[1])
    du[2] = u[1]*(ρ-u[3]) - u[2]
    du[3] = u[1]*u[2] - beta*u[3]
end

function lorenz_solver(x₀, ρ)
    u[0] = [x₀, 1, 0]
    tspan = (0.0, 100.0)
    p = [10, ρ, 8/3] 
    x₀, ρ = params
    prob = ODEProblem(lorenz!, u0, tspan, params)
    sol = solve(prob) 
    return sol
    #return the ODESolution
end

function lorenz_plot()
    params = [0, 14]
    sol1 = solve(prob)

    params = [1 * 10^-5 , 14]
    sol2 = solve(prob)

    p1 = plot(t, [sol1 sol2], ylabel=L"x1, x2", title="ρ = 14")
    p2 = plot(abs(sol1 - sol2), xlabel=L"t", ylabel=L"|x1 - x2|")
    p3 = plot(sol1, vars = (1,2,3), xlabel=L"x1", ylabel=L"z1")

    params = [0, 28]
    sol3= solve(prob)
   
    params = [1 * 10^-5, 28]
    sol4 = solve(prob)

    p4 = plot(t, [sol3 sol4], title="ρ = 28")
    p5 = plot(abs(sol3 - sol4), xlabel=L"t")
    p6 = plot(sol3, vars = (1,2,3), xlabel=L"x1")
    link=:all
    plot(p1, p2, p3, p4, p5, p6, layout(3, 2))
    #return the final plot
end

export lorenz_solver, lorenz_plot

end

I tied to execute this code in jupyter note to see what is the error but it shows no error

Hana Bachi
  • 11
  • 3
  • 1
    Welcome to StackOverflow! The very first line of `lorenz_solver` attempts to set the zeroth element of the vector `u` to a value, but `u` is undefined in this function. Did you mean to call that variable `u0`? Also, Jupyter has persistent state across cells, so there is no way to know if and how you might have defined `u` elsewhere in your notebook, which is why you might not be seeing an error when run in Jupyter. – PaSTE Mar 01 '23 at 14:47

0 Answers0