My assignment would be to observe harmonic oscillator. I have created a function for the driven oscillator's equation of motion. My problem is that now my code is plotting the results of the differential equation, but I would like to plot the oscillator's place which is marked as x_2. So it should be that the figures should plot the place of the oscillator. The oscillator is starting from the rest in the beginning. Thus, In the figure 1: the place of the oscillator, which is starting from the rest. In the figure 2: the place of the oscillator with the values of the parameter ω ∈ {0.1, 1.0, 1.1}. The others parameters' valeus stay the same. So, my question is that how could I get the place out of the function for the driven oscillator? Here is my function for the driven oscillator: function dx=DrivenOsc(t,x,T0,omega,M,F,omega0)dx = [-1/T0*x(1)-omega0^2*x(2)+F/M*cos(omega*t);x(1)]; Here is my code for the plotting: clear all close all T0 = 10; omega0 = 1; M = 1; F = 1; omega = 1; t = 0; x_1 = 0; x_2 = 0; x = [x_1 x_2]; t=0:0.01:10*T0; options=[]; options=odeset(options,'RelTol',1e-7,'AbsTol',1e-7); [tout,yout]=ode45(@DrivenOsc,t,x,options,T0,omega,M,F,omega0); figure(1) plot(tout,yout); xlabel('Aika'); ylabel('Värähtelyt'); grid on figure(2) subplot(3,1,1) T0 = 10; omega0 = 1; M = 1; F = 1; omega = 0.1; t = 0; x_1 = 0; x_2 = 0; x = [x_1 x_2]; t=0:0.01:10*T0; options=[]; options=odeset(options,'RelTol',1e-7,'AbsTol',1e-7); [tout,yout]=ode45(@DrivenOsc,t,x,options,T0,omega,M,F,omega0); plot(tout,yout); xlabel('Aika'); ylabel('Värähtelyt'); grid on subplot(3,1,2) T0 = 10; omega0 = 1; M = 1; F = 1; omega = 1.0; t = 0; x_1 = 0; x_2 = 0; x = [x_1 x_2]; t=0:0.01:10*T0; options=[]; options=odeset(options,'RelTol',1e-7,'AbsTol',1e-7); [tout,yout]=ode45(@DrivenOsc,t,x,options,T0,omega,M,F,omega0); plot(tout,yout); xlabel('Aika'); ylabel('Värähtelyt'); grid on subplot(3,1,3) T0 = 10; omega0 = 1; M = 1; F = 1; omega = 1.1; t = 0; x_1 = 0; x_2 = 0; x = [x_1 x_2]; t=0:0.01:10*T0; options=[]; options=odeset(options,'RelTol',1e-7,'AbsTol',1e-7); [tout,yout]=ode45(@DrivenOsc,t,x,options,T0,omega,M,F,omega0); plot(tout,yout); xlabel('Aika'); ylabel('Värähtelyt'); grid on
John Williams answered .
2025-11-20
Like this (both x_1 and x_2 are contained in y)
T0 = 10;
end
function dx=DrivenOsc(t,x,T0,omega,M,F,omega0)
end