So this is my code for a system of coupled oscillators
syms x1(t) x2(t) k1 k2 m Dx1 = diff(x1);D2x1 = diff(x1,2);Dx2 = diff(x2);D2x2 = diff(x2,2);Eq1 = D2x1 == (-(k1+k2)*x1+(k2)*x2)/m;Eq2 = D2x2 == ((k2*x1)+((k1+k2)*x2))/m;[V,Subs] = odeToVectorField(Eq1, Eq2);ftotal = matlabFunction(V, 'Vars',{'t','Y','k1','k2','m'}); interval = [0 5];y0 = [1 0; 0 0]; %initial conditions
ySol = ode45( @(t,Y)ftotal(t,Y,1,1,1),interval,y0); % ftotal(t,Y,k1,k2,m)
tValues = linspace(interval(1),interval(2),5);yValues = deval(ySol,tValues);plot(tValues,yValues) % plot(x,y)
I'm trying to numerically integrate and use the ode45 function to find the solution to this system of equations (Eq1 and Eq2). But somehow, my graphical solution is wrong and i don't get oscillations as expected.
Best Answer