How to fix a syntax in the oscillator's plot code?
이전 댓글 표시
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
Thank you for your help!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
