Assignment 4
Assignment 4
Assignment IV
Bhavya Garg
21ME02034
Question1:
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.
Approach:
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].
Plots:
Question 2:
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.
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);
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:
clc
clear all
syms y(t) omega
dy = diff(y,t);
d2y = diff(y,t,2);
% Initial conditions
conds = [y(0)==1, dy(0)==1];
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);
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.
%% 1. Define Parameters
h = 0.2; % Step size
t_values = 0:h:0.6; % Time points
y0 = 5; % Initial condition
%% 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
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
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)
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
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%.