How to fix a syntax in the oscillators plot code?

Technical Source
3 min readFeb 15, 2024

--

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

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source
Technical Source

Written by Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

No responses yet