Maths 5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment 5

1. Given finite difference formula:

'' −2 f j−1−3 f j +6 f j +1−f j +2


f(x )≈j
6h
Now, applying Taylor’s theorem around x j :
4
' ' '' (s 2)h
2 3
' '' h '' ' h
f j−1=f j −f j h+f j −f j +f j
2 6 24
4
' '' ' ( s 2)h
2 3
' '' h '' ' h
f j +1=f j +f j h+f j +f j +f j
2 6 24
4
' ' '' 2(s2 ) h
2 3
2h
' ' '' 4 h
''
f j +2=f j +2 f h+ f j +f j j +f j
2 6 24

Now, sub in the corrected expanded values into the finite difference formula:

( )
'' 2 ''' 3 ' '' ' 4 '' 2
f ( x j ) h f ( x j ) h f ( s 1) h
' ' f ( x j) h f ' ' ' (x j )h3 f
f (x j )≈ −2 f ( x j )−f ( x j ) h+ − + −3 f ( x j ) +6 (f ( x j) + f ( x j ) h+ +¿ +
''
2 6 24 2 6
¿

After simplifying this:

'' h
f ( x j ) ≈ ¿) = O(h^2)
3
2a)

function y = odesRK3(fun, t, y0)


n = length(t);
y = nan(length(y0), n);
y(:, 1) = y0(:);
h = diff(t);

for k = 1:n-1
F1 = h(k) * fun(t(k), y(:, k));
F2 = h(k) * fun(t(k) + (8/15) * h(k), y(:, k) + (8/15) * F1);
F3 = h(k) * fun(t(k) + (2/3) * h(k), y(:, k) + (1/4) * F1 + (5/12) * F2);
y(:, k + 1) = y(:, k) + (F1 + 3 * F3) / 4;
end
end

2b)
Given the problem with mass ratio µ:

Let y(t) = (y1(t), y2(t), y3(t), y4(t)) = (x(t), y(t), x’(t), y’(t))

We want to express the equations of motion as a system of ODEs, y' = F(y; µ).

The ODEs are as follows:

y1'(t) = y3(t)
y2'(t) = y4(t)
y3'(t) = -[(1 - µ) * (y1(t) + µ) / r^3 + µ * (y1(t) - 1 + µ) / r1^3]
y4'(t) = -[(1 - µ) * y2(t) / r^3 + µ * y2(t) / r1^3]

Where:

y1(t) represents the x-coordinate of the spacecraft.


y2(t) represents the y-coordinate of the spacecraft.
y3(t) represents the x-velocity of the spacecraft.
y4(t) represents the y-velocity of the spacecraft.
r is the distance between the spacecraft and the Earth, given by r = sqrt(y1(t)^2 + y2(t)^2).

r1 is the distance between the spacecraft and the Moon, given by r1 = sqrt((y1(t) + µ)^2 +
y2(t)^2).

µ is the mass ratio between the Earth and the Moon.

These ODEs describe the motion of the spacecraft in a non-dimensionalized rotating coordinate
system and are used to simulate its trajectory.

c) submitted to grader. Also, can be seen here:

function dydt = orbitRates(y, mu)


% Unpack the state vector y
y1 = y(1);
y2 = y(2);

% Compute the derivatives using the provided equations


r = sqrt(y1^2 + y2^2);
r1 = sqrt((y1 + mu)^2 + y2^2); % Corrected this line

y3 = y(3);
y4 = y(4);

% Pack the derivatives into the output vector dydt


dydt = [y3; y4; -((1 - mu) * (y1 + mu) / r^3 + mu * (y1 - 1 + mu) / r1^3); -((1 - mu) * y2 / r^3
+ mu * y2 / r1^3)];
end
d)

clear all
close all

% Define time values


t = linspace(0, 2 * pi, 100);

% Define initial conditions


y0 = [1; 0; 0; 1];

% Solve the ODEs using odesEuler


[t1, y1] = odesEuler(@(t, y) orbitRates(y, 0), t, y0);

% Solve the ODEs using odesRK3


[t2, y2] = odesRK3(@(t, y) orbitRates(y, 0), t, y0);

% Solve the ODEs using ode45 with custom error tolerances


opts = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
[t3, y3] = ode45(@(t, y) orbitRates(y, 0), [t(1) t(end)], y0, opts);

% Plot the trajectory


figure
plot(y1(:,1), y1(:,2), 'r', 'LineWidth', 1.5, 'DisplayName', 'odesEuler');
hold on
plot(y2(:,1), y2(:,2), 'g', 'LineWidth', 1.5, 'DisplayName', 'odesRK3');
plot(y3(:,1), y3(:,2), 'b--', 'LineWidth', 1.5, 'DisplayName', 'ode45');
xlabel('x(t)');
ylabel('y(t)');
axis equal
grid on
legend('Location', 'best');
title('Orbit Trajectory for \mu = 0');
e)

f)
% Define the initial conditions for the free-return trajectory
y0_free_return = [-0.0233267; -0.0125213; 8.03118; -7.16836];
t_free_return = linspace(0, 1.36569, 1000); % Adjust time span and number of points

% Solve the ODEs for the free-return trajectory using one of the solvers
[t_free, y_free] = odesRK3(@(t, y) orbitRates(y, 1.21506e-2), t_free_return, y0_free_return);
% You can use any of the solvers, but odesRK3 is a good choice for high accuracy.

% Plot the free-return trajectory


figure
plot(y_free(:,1), y_free(:,2), 'k', 'LineWidth', 1.5, 'DisplayName', 'Free-Return Trajectory');
hold on
plot([-1.21506e-2, 1-1.21506e-2], [0, 0], 'ro', 'MarkerSize', 10, 'DisplayName', 'Earth and Moon');
xlabel('x(t)');
ylabel('y(t)');
axis equal
grid on
legend('Location', 'best');
title('Free-Return Trajectory for \mu = 1.21506 \times 10^{-2}');
g)

You might also like