# How to fix a syntax in the oscillator's plot code?

4 views (last 30 days)
Jenni on 14 Feb 2024
Commented: Jenni on 14 Feb 2024
Hi!
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

Alan Stevens on 14 Feb 2024
Like this (both x_1 and x_2 are contained in y)
T0 = 10;
omega0 = 1;
M = 1;
F = 1;
x_1 = 0;
x_2 = 0;
x_0 = [x_1 x_2];
tspan=0:0.01:10*T0;
options=[];
options=odeset(options,'RelTol',1e-7,'AbsTol',1e-7);
omega = [0.1, 1, 1.1];
figure
hold on
for i = 1:numel(omega)
[tout,yout]=ode45(@(t,y)DrivenOsc(t,y,T0,omega(i),M,F,omega0),tspan,x_0,options);
subplot(3,1,i)
plot(tout,yout); % If you want just x_2 then plot(tout,yout(:,2))
title(['\omega = ' num2str(omega(i))])
xlabel('Aika');
ylabel('Värähtelyt');
legend('Nopeus','asema') % Nopeus = Velocity, asema = position according to Google translate.
grid on
end
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)];
end
Jenni on 14 Feb 2024
Excellent, thank you so much @Alan Stevens!
P.S I truly appreciate for googling Finnish translations! :D

### Categories

Find more on Subplots in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!