Question: I have a working matlab code - % Parameters M = 1 ; K = 3 ; B = 0 . 5 ; x 0

I have a working matlab code -% Parameters
M =1;
K =3;
B =0.5;
x0=2;
v0=0;
t0=0;
tend =15;
h =0.01;
% Function for Euler equation solution
f1= @(t, x1, x2) x2;
f2= @(t, x1, x2)-(B/M)* x2-(K/M)* x1;
% Euler method
tn_euler = t0:h:tend;
x1_euler = zeros(size(tn_euler));
x2_euler = zeros(size(tn_euler));
x1_euler(1)= x0;
x2_euler(1)= v0;
fprintf('All Euler steps
');
for i =1:length(tn_euler)-1
t = tn_euler(i);
x1= x1_euler(i);
x2= x2_euler(i);
x1_euler(i+1)= x1+ h * f1(t, x1, x2);
x2_euler(i+1)= x2+ h * f2(t, x1, x2);
fprintf('%f %f %f
', tn_euler(i), x1_euler(i), x2_euler(i));
end
% Plotting numerical solution using Euler method
figure;
hold on;
plot(tn_euler, x1_euler, 'linewidth', 2);
plot(tn_euler, x2_euler, 'linewidth', 2);
plot(tn_euler, -0.5* x2_euler -3* x1_euler, 'linewidth', 2);
legend('Displacement', 'Velocity', 'Acceleration');
title('Solution using Euler method');
box on;
% Runge-Kutta 4 method
x1_rk4= zeros(size(tn_euler));
x2_rk4= zeros(size(tn_euler));
x1_rk4(1)= x0;
x2_rk4(1)= v0;
fprintf('All Runge Kutta 4 steps
');
for i =1:length(tn_euler)-1
t = tn_euler(i);
x1= x1_rk4(i);
x2= x2_rk4(i);
k1= h * f1(t, x1, x2);
l1= h * f2(t, x1, x2);
k2= h * f1(t + h/2, x1+ k1/2, x2+ l1/2);
l2= h * f2(t + h/2, x1+ k1/2, x2+ l1/2);
k3= h * f1(t + h/2, x1+ k2/2, x2+ l2/2);
l3= h * f2(t + h/2, x1+ k2/2, x2+ l2/2);
k4= h * f1(t + h, x1+ k3, x2+ l3);
l4= h * f2(t + h, x1+ k3, x2+ l3);
x1_rk4(i+1)= x1+(k1+2*k2+2*k3+ k4)/6;
x2_rk4(i+1)= x2+(l1+2*l2+2*l3+ l4)/6;
fprintf('%f %f %f
', tn_euler(i), x1_rk4(i), x2_rk4(i));
end
% Plotting numerical solution using Runge-Kutta 4 method
figure;
hold on;
ylim([-6,6]);
plot(tn_euler, x1_rk4, 'linewidth', 2);
plot(tn_euler, x2_rk4, 'linewidth', 2);
plot(tn_euler, -0.5* x2_rk4-3* x1_rk4, 'linewidth', 2);
legend('Displacement', 'Velocity', 'Acceleration');
title('Solution using Runge-Kutta 4 method');
box on; And I need to implement this to it, Turn off the sliding friction and set h =0.1, but keep all other parameters the same.
Integrate using the Euler Method. You will see that the oscillator is now unstable.
As one shrinks h, the oscillations become more convergent to steady state, however,
ideal behavior (y(t)= y(t + T), where T is the period of the oscillation) requires
infinite numerical precision.
Show what the behavior looks like with the IEM and the RK4 method for h =0.1
[DLV x 2].
Find the limit of h for the Euler Method (EM) such that the oscillations increase by
less than 1% over a period of 1 oscillation, i.e., y(t + T)<=1.01* y(t)[DLV].
For part II, you may have to change the Integration_Time to examine the stability.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!