0% found this document useful (0 votes)
2 views

Assignment 4

The document outlines a series of assignments related to mechanical systems simulation using MATLAB, focusing on symbolic integration, differentiation, and solving differential equations. It includes detailed explanations and approaches for various mathematical problems, such as analyzing oscillatory behavior, resonance in spring-mass-damper systems, and the challenges of nonlinear equations. The document also discusses numerical methods like Euler's and Runge-Kutta for solving first-order linear differential equations and compares the results with exact solutions.

Uploaded by

BhavyaGarg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 4

The document outlines a series of assignments related to mechanical systems simulation using MATLAB, focusing on symbolic integration, differentiation, and solving differential equations. It includes detailed explanations and approaches for various mathematical problems, such as analyzing oscillatory behavior, resonance in spring-mass-damper systems, and the challenges of nonlinear equations. The document also discusses numerical methods like Euler's and Runge-Kutta for solving first-order linear differential equations and compares the results with exact solutions.

Uploaded by

BhavyaGarg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MECHANICAL SYSTEMS SIMULATION LAB II (ME6P003)

Assignment IV

Bhavya Garg
21ME02034
Question1:

Explanation and Approach:

The first part of this question involves symbolic integration and plotting. The function f=
√ 9−t 2 represents the upper half of a circle with a radius of 3. Using int(f), MATLAB
computes the indefinite integral of f. When we use int(f, -3, 3), MATLAB computes the
definite integral, which represents the area under the semicircle from t=−3 to t = 3. Finally,
ezplot(f, [-3,3]) generates a graph of the function, showing only the upper semicircle.

The second part deals with symbolic differentiation. We define g=t*arccos(t) and compute
its first, second, and third derivatives using diff(g), diff(g,2), and diff(g,3). The pretty(ans)
command formats the output in a readable way. Finally, ezplot(g, [-1,1]) plots g(t) over the
interval [−1,1] illustrating how the function behaves across that domain.

% 1a: Symbolic Integration and Plotting of the Upper Half-Circle


syms t
f = sqrt(9 - t.^2); % Define the function representing the upper half-
circle
int(f); % Compute the indefinite integral of f
int(f, -3, 3); % Compute the definite integral from -3 to 3
figure;
ezplot(f, [-3, 3]); % Plot the function within the given interval
ylim([-1.5, 3.1]); % Adjust y-axis limits as specified
grid on;

% 1b: Symbolic Differentiation and Plotting


clear all; clc;
syms t
g = t * (acos(t)); % Define the function g(t)
diff(g); % Compute the first derivative of g
pretty(ans); % Display formatted output
diff(g, 2); % Compute the second derivative
pretty(ans);
diff(g, 3); % Compute the third derivative
pretty(ans);
figure;
ezplot(g, [-1,1]); % Plot g(t) over the interval [-1,1]
grid on;

% 1c: Solving the Second-Order Differential Equation y'' = sec(y')


clear all;
clc;
syms y(t)
dy = diff(y,t); % First derivative y'
d2y = diff(y,t,2); % Second derivative y''
ode = d2y == sec(dy); % Define the ODE y'' = sec(y')
ysol = dsolve(ode); % Solve the ODE symbolically
disp(ysol); % Display the general solution

% Solving with Initial Conditions (Tangent to t-axis at Origin)


conds = [y(0) == 0, dy(0) == 0]; % y(0) = 0 and y'(0) = 0
ysol2 = dsolve(ode, conds);
disp(ysol2); % Display the particular solution

% Plot the particular solution over [-1,1]


figure;
fplot(ysol2, [-1,1]);
% ylim([-1.5,3.1]); % Adjust y-axis limits
grid on;

1c: Solving the Second-Order ODE y′′=sec(y′)

Approach:

This part involves solving the nonlinear second-order differential equation:

y′′=sec(y′)

Using MATLAB's dsolve, we find the general solution. To obtain a particular solution, we
apply initial conditions y(0) and y′(0)=0, which ensures the function is tangent to the t-axis at
the origin. Finally, we plot the function over the interval [-1,1].

Output: The general solution is displayed in symbolic form.The particular solution is


computed based on initial conditions. The plot will shows the behavior of the function over
[-1,1].

Plots:
Question 2:

Explanation & Approach

In this question, we solve three different first-order differential equations symbolically using
MATLAB's dsolve function and compare their solutions. The first equation, y ′=−0.1y, is a
simple exponential decay model where y decreases over time. The second equation, y ′=sint,
models an oscillatory function where the derivative follows the sine wave. The third
equation, y′=sint - 0.1y, combines both behaviors—showing oscillations while gradually
decaying over time due to the negative damping term.

% 2a: Solving y' = -0.1y with Initial Condition y(0) = 1


syms y(t)
y = dsolve(diff(y) == -0.1 * y, y(0) == 1); % Solve ODE symbolically
figure;
ezplot(y, [-20, 20]); % Plot the solution over the interval [-20, 20]
title('Solution of dy/dt = -0.1y');
grid on;
% 2b: Solving y' = sin(t) with Initial Condition y(0) = 1
syms y(t)
y = dsolve(diff(y) == sin(t), y(0) == 1); % Solve ODE symbolically
figure;
ezplot(y, [-20, 20]); % Plot the solution over [-20, 20]
title('Solution of dy/dt = sin(t)');
grid on;

% 2c: Solving y' = sin(t) - 0.1y with Initial Condition y(0) = 1


syms y(t)
y = dsolve(diff(y) == sin(t) - 0.1 * y , y(0) == 1); % Solve ODE
symbolically
figure;
fplot(y, [-20, 20]); % Plot using fplot for smoother curve
title('Solution of dy/dt = sin(t) - 0.1y');
grid on;

Output:
By plotting these equations over the interval [−20,20] we can observe clear differences in
their behavior. The first equation’s solution is an exponentially decreasing function. The
second equation’s solution is an integrated sine function, leading to an oscillating behavior.
The third equation exhibits damped oscillations, meaning the amplitude decreases as time
progresses due to the presence of −0.1y.

Question3:

Explanation: In this question, we are tasked with solving three different second-order
differential equations using the dsolve function in MATLAB. The equations have the same
structure but different coefficients and forcing terms. Each equation has initial conditions:
y(0)=1 and y′(0)=1. These initial conditions help define a specific solution for each equation,
as the general solution to a second-order differential equation typically involves two
arbitrary constants, which can be determined by initial conditions. We will compare the
solutions to observe how the changes in coefficients and forcing terms affect the behavior of
the system.

Approach: The approach involves using the dsolve function to solve each equation
symbolically. For each equation, we first define the unknown function y(t) and its derivatives.
We then set up each differential equation and use the initial conditions to find the specific
solution for each case. After obtaining the solutions, we display them using the pretty
function to show them in a readable format.

clc
clear all
syms y(t)
dy = diff(y,t);
d2y = diff(y,t,2);

% Define the differential equations


ode = d2y + y == sin(t); % First equation
ode2 = d2y + 0.1*dy + y == sin(t); % Second equation
ode3 = d2y + 0.1*dy + y == 0; % Third equation
% Initial conditions
conds = [y(0) == 1, dy(0) == 1];

% Solve the equations using dsolve


ysol = dsolve(ode,conds);
ysol2 = dsolve(ode2,conds);
ysol3 = dsolve(ode3,conds);

% Display the solutions


disp('Solving 1st')
pretty(ysol);
disp('Solving 2nd')
pretty(ysol2);
disp('Solving 3rd')
pretty(ysol3);

Output: The output consists of the symbolic solutions for each of the three differential
equations, displayed in a simplified form using the pretty function. These solutions represent
the time evolution of y(t) for each system. The solutions will differ depending on the nature
of the forcing term and damping term. The first two equations will have sinusoidal behavior
influenced by the damping and forcing terms, while the third equation represents a simple
harmonic oscillator with no external forcing, leading to a purely oscillatory solution.
Question4:

Explanation:

This question involves solving a second-order differential equation representing a spring-


mass-damper system driven by a sinusoidal input. The equation 2y ′′+0.5y ′+5y=sin(ωt)
models the system, where y(t) is the displacement of the mass, and the parameters ω
represent the angular frequency of the driving force. The initial conditions are y(0)=1and y
′(0)=1. The goal is to solve the system for different values of ω and analyze how the
amplitude of the solution varies with frequency, especially identifying the resonance
frequency, which maximizes the system's amplitude.

clc
clear all
syms y(t) omega
dy = diff(y,t);
d2y = diff(y,t,2);

% Define the spring-mass-damper equation


ode = 2*d2y + 0.5 * dy + 5*y == sin(omega*t);

% Initial conditions
conds = [y(0)==1, dy(0)==1];

% Solve the equation symbolically


ysol = dsolve(ode,conds);
disp('The solution of the given ODE is');
pretty(ysol);

% Substitute different values of omega and plot the solutions


y1 = subs(ysol,omega,1);
ezplot(y1, [0,50]);

% Try different omega values


omega_values = [0.5,0.8,1,1.2,1.5,2];
figure;
hold on;
for w_val = omega_values
y_w = subs(ysol,omega,w_val);
fplot(y_w,[0,50]);
end
hold off;
grid on;
xlabel('t'); ylabel('y');
title('Spring-Mass-Damper Response for different omega');
legend('w=0.5','w=0.8', 'w=1.0', 'w=1.2','w=1.5','w=2.0');

% Test for resonance frequency


w_resonance = 1.55;
y_resonance = subs(ysol,omega,w_resonance);
figure;
fplot(y_resonance,[0,50]);
xlabel('t'); ylabel('y');
title('Resonance Plot');
grid on;

Approach:

We begin by solving the spring-mass-damper equation using dsolve for symbolic solutions.
The initial conditions are applied to get the specific solution. Then, we substitute different
y1 = subs(ysol,omega,1);
ezplot(y1, [0,50]);
values of ω into the solution and plot the displacement y(t) over time for each frequency.
We look for a frequency that results in a maximal amplitude, which will correspond to the
resonance frequency. We hypothesize that the resonance frequency corresponds to the
system's natural frequency, and we verify this by testing the system behavior at that
frequency.
Output:

The output includes plots of the displacement y(t) for various values of ω, showing how the
system's response changes with frequency. We identify the resonance frequency by
observing the maximum amplitude in the plots. The system's behavior at the resonance
frequency is confirmed by a significant increase in amplitude at a frequency of 1.5 from the
graph. The plots will show the system's displacement for different driving frequencies, and
the resonance plot will show a dramatic response at the natural frequency.

Question 5:

Explanation:

This question consists of two parts: solving a simple second-order differential equation y′′
+y=0 and solving a nonlinear differential equation y′′−y+y 3=0. The first equation is a simple
harmonic oscillator with constant coefficients and no external forcing. The second equation
is nonlinear due to the y3 term, which makes it more challenging to solve symbolically. We
are also tasked with varying initial conditions and analyzing the effect on the solution.
MATLAB is unable to solve the nonlinear equation symbolically.
clc
clear all
syms y(t)
dy = diff(y,t);
d2y = diff(y,t,2);
d3y = diff(y,t,3);

% Solve the linear equation y'' + y = 0


conds = [y(0) == 2, dy(0) == 2];
ode = d2y + y == 0;
ysol = dsolve(ode,conds);
disp('Solution of y with y(0) == 2 and dy(0) == 2');
pretty(ysol);

% Plot the solution


figure;
fplot(ysol,[-10,10]);
title('Plot of 2cos(t) + 2sin(t)');

% Change initial conditions


conds2 = [y(0) == 0.2, dy(0) == 0.2];
ysol2 = dsolve(ode,conds2);
disp('Solution of y with y(0) == 0.2 and dy(0) == 0.2');
pretty(ysol2);

% Solve the nonlinear equation y'' - y + y^3 = 0


ode2 = d2y - dy + d3y == 0;
conds3 = [y(0) == 1, dy(0) == 1];
ysol3 = dsolve(ode2,conds3);
disp('Solution of d2y - dy + d3y with y(0) == 1 and dy(0) == 1');
pretty(ysol3);

Approach:

We begin by solving the linear equation y′′+y=0 using dsolve with initial conditions y(0)=2
and y′(0)=2, then plot the solution. The solution will have an initial displacement of 2 and
initial velocity of 2, resulting in oscillations with a relatively large amplitude. The motion will
start with a higher initial velocity, making the oscillations more prominent at the start.

Next, we change the initial conditions to y(0)=0.2 and y ′(0)=0.2, and observe the effect on
the solution. The amplitude of the oscillations will decrease. The system will oscillate with
smaller displacement and velocity.

For the nonlinear equation, we use dsolve and observe that MATLAB cannot find a symbolic
solution, which we attribute to the nonlinearity introduced by the y 3 term.
Output:

The output includes the solutions for the linear equation with different initial conditions and
their corresponding plots. The nonlinear equation will trigger an error or return a message
indicating that MATLAB cannot solve it symbolically or it shows the solution in terms of t and
C1. This behavior highlights the challenges in solving nonlinear differential equations
analytically. The plots will show oscillations for the linear equation and an error for the
nonlinear equation. MATLAB may give a general solution involving arbitrary constants (like
C1) because it cannot find an explicit solution that satisfies the equation in a simple
analytical form.
Question 6:

Explanation:

In this problem, we are tasked with solving the first-order linear differential equation

dy/dx −e−2x+3y=0

using two numerical methods: Euler's method and the Runge-Kutta second-order (Midpoint)
method. The exact solution is given by y(x)= e−2x+4e−3x, and we are to compute the value of
y(0.6) using a step size h=0.2. Additionally, we will calculate the percentage error between
the exact solution and the results from both methods and present the results in a table.

clc; clear; close all;

%% 1. Define Parameters
h = 0.2; % Step size
t_values = 0:h:0.6; % Time points
y0 = 5; % Initial condition

% Define the function dy/dt = f(y, t)


f = @(y, t) exp(-2*t) - 3*y;

%% 2. Euler’s Method
y_euler = zeros(size(t_values));
y_euler(1) = y0;

for i = 2:length(t_values)
k1 = f(y_euler(i-1), t_values(i-1));
y_euler(i) = y_euler(i-1) + h * k1;
end

%% 3. Runge-Kutta Second Order (Midpoint) Method


y_rk2 = zeros(size(t_values));
y_rk2(1) = y0;

for i = 2:length(t_values)
k1 = f(y_rk2(i-1), t_values(i-1)); % Step 1
y_mid = y_rk2(i-1) + (h/2) * k1; % Step 2
k2 = f(y_mid, t_values(i-1) + h/2); % Step 3
y_rk2(i) = y_rk2(i-1) + h * k2; % Step 4
end

%% 4. Compute Exact Solution


syms y(t)
ode = diff(y, t) == exp(-2*t) - 3*y;
conds = y(0) == 5;
y_solution = dsolve(ode, conds);
% Compute the exact value at t = 0.6
y_exact_0_6 = double(subs(y_solution, t, 0.6));

%% 5. Compute Errors and Display in Table


error_euler = abs((y_euler(end) - y_exact_0_6) / y_exact_0_6) * 100;
error_rk2 = abs((y_rk2(end) - y_exact_0_6) / y_exact_0_6) * 100;

fprintf('\nMethod\t\t y(0.6) \t Exact y(0.6) \t Error (%%)\n');


fprintf('------------------------------------------------------\n');
fprintf('Euler\t\t %.4f \t %.4f \t %.2f%%\n', y_euler(end),
y_exact_0_6, error_euler);
fprintf('RK2 Midpoint\t %.4f \t %.4f \t %.2f%%\n', y_rk2(end),
y_exact_0_6, error_rk2);

%% 6. Plot the Solutions


t_plot = linspace(0, 0.6, 100);
y_exact_plot = double(subs(y_solution, t, t_plot));

figure; hold on;


plot(t_plot, y_exact_plot, 'g', 'LineWidth', 1.5, 'DisplayName', 'Exact
Solution');
plot(t_values, y_euler, 'bo-', 'LineWidth', 1.2, 'DisplayName', 'Euler
Method');
plot(t_values, y_rk2, 'ro-', 'LineWidth', 1.2, 'DisplayName', 'RK2
Midpoint');
xlabel('t'); ylabel('y'); grid on;
title('Numerical vs Exact Solution');
legend show;

Approach:

1. Euler’s Method: This is the simplest method where we approximate the solution at
each step using the formula:

yn+1=yn + h⋅f(yn,tn)

2. Runge-Kutta Second-Order Method (Midpoint): In this method, we improve the


approximation by considering the midpoint of the interval for a better estimate. The
steps for this method are:

 Compute k1
 Estimate the midpoint
 Compute k2
 Update the solution: yn+1

3. Exact Solution: The exact solution is derived by solving the differential equation
analytically using symbolic methods, and its value at x=0.6 is computed.
4. Error Calculation: For each method, the percentage error is calculated.

Output

From the results:

Euler’s method produces a solution that is significantly off from the exact value, with an
error of 48.51%.

Runge-Kutta (RK2 Midpoint) provides a much more accurate result, with a lower error of
14.03%.

You might also like