# How to plot an oscillator?

4 views (last 30 days)
Haya Ali on 12 Jan 2023
Commented: Haya Ali on 12 Jan 2023
I want to plot the equations given below. Which is generally of an 3 coupled oscillators. Can anyone please tell me if I am plotting it correctly or not? Below is my code
close all; clear all; clc;
%value of constants
a1=0.2;a2=0.3;a3=0.3;
omega1=5;omega2=4;omega3=5;
G=1;C12=0.01;C13=0.02;C21=0.03;C23=0.04;C31=0.05;C32=0.06;
dt=0.01; %step size
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1(1)=0.5;
y1(1)=0.5;
x2(1)=0.5;
y2(1)=0.5;
x3(1)=0.5;
y3(1)=0.5;
for i=2:1000
%Oscillator 1
x1(i) = x1(i-1) + ( a1*x1(i-1) - omega1*y1(i-1) + G*C12*( x2(i-1) - x1(i-1) ) + G*C13*( x3(i-1) - x1(i-1) ) )*dt;
y1(i) = y1(i-1) + ( a1*y1(i-1) + omega1*x1(i-1) + G*C12*( y2(i-1) - y1(i-1) ) + G*C13*( y3(i-1) - y1(i-1) ) )*dt;
%Oscillator 2
x2(i) = x2(i-1) + ( a2*x2(i-1) - omega2*y2(i-1) + G*C21*( x1(i-1) - x2(i-1) ) + G*C23*( x3(i-1) - x2(i-1) ) )*dt;
y2(i) = y2(i-1) + ( a2*y2(i-1) + omega2*x2(i-1) + G*C21*( y1(i-1) - y2(i-1) ) + G*C23*( y3(i-1) - y2(i-1) ) )*dt;
%Oscillator 3
x3(i) = x3(i-1) + ( a3*x3(i-1) - omega3*y3(i-1) + G*C31*( x1(i-1) - x3(i-1) ) + G*C32*( x2(i-1) - x3(i-1) ) )*dt;
y3(i) = y3(i-1) + ( a3*y3(i-1) + omega3*x3(i-1) + G*C31*( y1(i-1) - y3(i-1) ) + G*C32*( y2(i-1) - y3(i-1) ) )*dt;
end
plot (x1)
hold on
plot (x2)
plot (x3)

Stephan on 12 Jan 2023
You can plot it 2 ways - i dont know what is "correct" for your case... For me both are correct - depending on what you want to see:
%value of constants
a1=0.2;a2=0.3;a3=0.3;
omega1=5;omega2=4;omega3=5;
G=1;C12=0.01;C13=0.02;C21=0.03;C23=0.04;C31=0.05;C32=0.06;
dt=0.01; %step size
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1(1)=0.5;
y1(1)=0.5;
x2(1)=0.5;
y2(1)=0.5;
x3(1)=0.5;
y3(1)=0.5;
for i=2:1000
%Oscillator 1
x1(i) = x1(i-1) + ( a1*x1(i-1) - omega1*y1(i-1) + G*C12*( x2(i-1) - x1(i-1) ) + G*C13*( x3(i-1) - x1(i-1) ) )*dt;
y1(i) = y1(i-1) + ( a1*y1(i-1) + omega1*x1(i-1) + G*C12*( y2(i-1) - y1(i-1) ) + G*C13*( y3(i-1) - y1(i-1) ) )*dt;
%Oscillator 2
x2(i) = x2(i-1) + ( a2*x2(i-1) - omega2*y2(i-1) + G*C21*( x1(i-1) - x2(i-1) ) + G*C23*( x3(i-1) - x2(i-1) ) )*dt;
y2(i) = y2(i-1) + ( a2*y2(i-1) + omega2*x2(i-1) + G*C21*( y1(i-1) - y2(i-1) ) + G*C23*( y3(i-1) - y2(i-1) ) )*dt;
%Oscillator 3
x3(i) = x3(i-1) + ( a3*x3(i-1) - omega3*y3(i-1) + G*C31*( x1(i-1) - x3(i-1) ) + G*C32*( x2(i-1) - x3(i-1) ) )*dt;
y3(i) = y3(i-1) + ( a3*y3(i-1) + omega3*x3(i-1) + G*C31*( y1(i-1) - y3(i-1) ) + G*C32*( y2(i-1) - y3(i-1) ) )*dt;
end
figure(1)
plot (x1)
hold on
plot (x2)
plot (x3)
figure(2)
plot (x1,y1)
hold on
plot (x2,y2)
plot (x3,y3)
Haya Ali on 12 Jan 2023
thanks

### Categories

Find more on Interactive Control and Callbacks 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!

Translated by