Aoife Healy
Aoife Healy

Reputation: 5

Julia MethodError: no method matching

I am running the folowing code:

function P_diastole(P0,t,R,C)
    t0 = t[1];
    P_diastole = P0*exp((-(t.-t0))./(R*C))
    return P_diastole
end

t = 0:0.1:5;
P0 = 80;
R = 700;
C = 2.56;

P = P_diastole(P0,t,R,C);

But am getting the following error:

MethodError: no method matching exp(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})

Closest candidates are:
  exp(!Matched::Union{Float16, Float32, Float64})
   @ Base special\exp.jl:325
  exp(!Matched::LinearAlgebra.Adjoint{T, <:AbstractMatrix} where T)
   @ LinearAlgebra C:\Users\aoife\AppData\Local\Programs\Julia-1.9.3\share\julia\stdlib\v1.9\LinearAlgebra\src\dense.jl:595
  exp(!Matched::LinearAlgebra.Transpose{T, <:AbstractMatrix} where T)
   @ LinearAlgebra C:\Users\aoife\AppData\Local\Programs\Julia-1.9.3\share\julia\stdlib\v1.9\LinearAlgebra\src\dense.jl:596
  ...


Stacktrace:
 [1] P_diastole(P0::Int64, t::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, R::Int64, C::Float64)
   @ Main c:\Users\aoife\Desktop\School\5 yr\Thesis\Julia\Windkessel.ipynb:3
 [2] top-level scope
   @ c:\Users\aoife\Desktop\School\5 yr\Thesis\Julia\Windkessel.ipynb:6

Any idea what the issue is?

When I initially got the error, I hadn't included the . before the - and the / in the P_diastole = P0exp((-(t.-t0))./(RC)) line. However, fixing this hasn't gotten rid of the error.

Upvotes: 0

Views: 1001

Answers (1)

JuliaR
JuliaR

Reputation: 26

When you look at the error, it says that it cannot find the exp method for StepRangeLen. That means that inside exp you put something else than you wanted to. This comes actually from your definition of t - the way you write it, it creates a range object (StepRangeLen). So you need to convert it to a vector first:

t = collect(0:0.1:5);

If you try running it then, you notice that exp cannot be called on a vector, so you need a dot there as well. So the correct version would be:

function P_diastole(P0,t,R,C)
  t0 = t[1];
  P_diastole = P0*exp.((-(t.-t0))./(R*C))
  return P_diastole
end

t = collect(0:0.1:5);
P0 = 80;
R = 700;
C = 2.56;

P = P_diastole(P0,t,R,C)

Upvotes: 1

Related Questions