Problem using DifferentialEquations.jl

diffeq

#1

Hi!

I am trying to create a function that I could use to solve a simple linear dynamic problem by using DifferentialEquations.jl.

using DifferentialEquations
using Plots

function s(f,KK,MM,al,be,n,u0,t)
    C=al*MM+be*KK
    A=[C*Mi K*Mi;eye(n) zeros(n,n)]
    b=[f(t);zeros(n)]
    g(u,p,t)=A*u+b
    prob=ODEProblem(g,u0,tspan)
    sol=solve(prob)
    h=plot(sol)
    display(h)
end

When I test the function with values something goes wrong e.g.

M=400; m=35
K=50000; k=200000
A=300

function f(t)
    [A*sin(t);0]
end

MM=M*0.5*eye(2)+0.5*m*eye(2)
KK=[K+k -K;-K  K]
Mi=inv(MM)
tspan=(0.0,2.0)
u0=zeros(4,1)
f(t)=[A*sin(t);0]
al=0.5; be=0.02; n=2
s(f,KK,MM,al,be,n,u0,tspan)

I keep getting this error message:

LoadError: e[91mMethodError: no method matching sin(::Tuple{Float64,Float64})e[0m
Closest candidates are:
sin(e[91m::Float64e[39m) at math.jl:419
sin(e[91m::Float32e[39m) at math.jl:420
sin(e[91m::Float16e[39m) at math.jl:950
...e[39m

Does anyone have any suggestions how I could try to fix this?


#2

Welcome to the forum!

tspan is a tuple and sin does not accept tuple inputs. Change b=[f(t);zeros(n)] to b=[f.(t);zeros(n)] might help? (actually, that doesn’t work, but you may get the gist?)


#3

Thank you for the tip! I don’t quite know what to do yet but I’ll keep working on it!


#4

I can’t seem to make this work. I fixed some mistakes I noticed in the matrices but I don’t know what I should do about the tuple.


#5

I’m not sure what you intend to do as your code is not self-explanatory, but below “works”. I would recommend to first test everything at the REPL, sans functions. You should also read up on arrays and vectors, you seem a bit confused about their shape, how to transform them, etc.

using DifferentialEquations
using Plots
pyplot() # otherwise you might run into world-age issues, at least if you also use pyplot

function s(f,KK,MM,al,be,n,u0,t)
    C=al*MM+be*KK
    A=[C*Mi K*Mi;eye(n) zeros(n,n)]
    b=[f.(t)..., zeros(n)...]
    g(u,p,t)=A*u+b
    prob=ODEProblem(g,u0,tspan)
    sol=solve(prob)
    h=plot(sol)
    display(h)
end

M=400; m=35
K=50000; k=200000
AA=300

MM=M*0.5*eye(2)+0.5*m*eye(2)
KK=[K+k -K;-K  K]
Mi=inv(MM)
tspan=(0.0,2.0)
u0=zeros(4)
f(t)=AA*sin(t)
al=0.5; be=0.02; n=2
s(f,KK,MM,al,be,n,u0,tspan)

#6

Sorry I wasn’t clearer! Thank you for helping!