Jenni asked . 2024-02-14

How to fix a syntax in the oscillators plot code?

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

 

plotting , figure , subplot , code

Expert Answer

John Williams answered . 2024-05-17 20:42:49

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


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.