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.
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?
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?)
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)