Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated readme
  • Loading branch information
mjb13028 committed Dec 5, 2017
1 parent f71e054 commit 40d3520
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 23 deletions.
153 changes: 153 additions & 0 deletions #README.md#
@@ -0,0 +1,153 @@
# 06_initial_value_ode

## Problem 2

### Script:
```matlab
%Part a.
y_analytical = @(t) exp(-t);

%Part b.
dt = 0.01; %step size
a = figure(1);
set(0, 'defaultAxesFontsize', 16)
set(0, 'defaultTextFontsize', 16)
set(0, 'defaultLineLineWidth', 2)
t = [0:dt:3]';
y_n = zeros(size(t));
y_n(1) = 1; %initial condition
for i = 2:length(t)
dy = ode2(t, y_n(i-1));
y_n(i) = y_n(i-1) + dt*dy;
end
plot(t, y_analytical(t), 'k-', t, y_n, 'o-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler.png');

%Part c.
b = figure(2);
y_heun = heun_sol_order1(@(t,y) ode2(t,y), dt, 1, [0 3]);
plot(t, y_analytical(t), 'k-', t, y_heun, 'o-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun.png');
```

### Part b.
![Analytical vs. Euler](./euler2.png)

### Part c.
![Analytical vs. Heun](./heun2.png)

## Problem 3

### Script:
```matlab
%Part a.
y_analytical = @(t) cos(3*t);

%Part b.
dt = 0.001; %step size
a = figure(1);
set(0, 'defaultAxesFontsize', 16)
set(0, 'defaultTextFontsize', 16)
set(0, 'defaultLineLineWidth', 2)
t = [0:dt:3]';
y_n = zeros(length(t), 2);
y_n(1, :) = [1 0]; %initial condition
for i = 2:length(t)
dy = ode3(t, y_n(i-1, :));
y_n(i, :) = y_n(i-1, :) + dt*dy;
end
plot(t, y_analytical(t), 'k-', t, y_n(:,1), 'o-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler3.png');

%Part c.
b = figure(2);
dt = 0.001;
t = [0:dt:3];
y_heun = heun_sol_order2(@(t,y) ode3(t,y), dt, [1 0], [0 3]);
plot(t, y_analytical(t), 'k-', t, y_heun(:,1), 'o-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun3.png');
```

### Part b.
![Analytical vs. Euler](./euler3.png)

### Part c.
![Analytical vs. Heun](./heun3.png)

## Problem 4

### Script:
```matlab
%part a.
g = 9.81; %m/s^2
cd = 0.25; %kg/m
m = 60; %kg
v_term = sqrt(m*g/cd);
x_analytical = @(t) ((v_term^2)/g)*log(abs(cosh((g*t)/v_term))) + 100;

%part b.
dt = 0.01; %step size
a = figure(1);
set(0, 'defaultAxesFontsize', 16)
set(0, 'defaultTextFontsize', 16)
set(0, 'defaultLineLineWidth', 2)
t = [0:dt:12]';
y_n = zeros(length(t), 2);
y_n(1, :) = [100 0]; %initial condition
for i = 2:length(t)
dy = ode4(t, y_n(i-1, :));
y_n(i, :) = y_n(i-1, :) + dt*dy;
end
plot(t, x_analytical(t), 'k-', t, y_n(:,1), 'o-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler4.png');

%Part c.
b = figure(2);
dt = 0.01;
t = [0:dt:12];
y_heun = heun_sol_order2(@(t,y) ode4(t,y), dt, [100 0], [0 12]);
plot(t, x_analytical(t), 'k-', t, y_heun(:,1), 'o-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun4.png');
```

### Part b.
![Analytical vs. Euler](./euler4.png)

### Part c.
![Analytical vs. Heun](./heun4.png)

## Problem 5

### Part a.
```matlab
function dy = phugoid_ode(t,y)
%glider equations describing phugoid path
%y = [v, theta, x, y]
g = 9.81; %m/s^2
vt = 5.5; %m/s
cl = 5.2;
cd = 1;
dy = zeros(size(y));
dy(1) = -g*sin(y(2)) - cd/cl*g/vt^2*y(1)^2;
dy(2) = -(g/y(1))*cos(y(2)) + g/vt^2*y(1);
dy(3) = y(1)*cos(y(2));
dy(4) = y(1)*sin(y(2));
end
```

### Part b.
```matlab


4 changes: 2 additions & 2 deletions HW6Problem2.m
Expand Up @@ -14,15 +14,15 @@ for i = 2:length(t)
dy = ode2(t, y_n(i-1));
y_n(i) = y_n(i-1) + dt*dy;
end
plot(t, y_analytical(t), 'k-', t, y_n, 'o-');
plot(t, y_n, 'o', t, y_analytical(t), 'k-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler2.png');

%Part c.
b = figure(2);
y_heun = heun_sol_order1(@(t,y) ode2(t,y), dt, 1, [0 3]);
plot(t, y_analytical(t), 'k-', t, y_heun, 'o-');
plot(t, y_heun, 'o', t, y_analytical(t), 'k-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun2.png');
6 changes: 3 additions & 3 deletions HW6Problem3.m
Expand Up @@ -12,9 +12,9 @@ y_n = zeros(length(t), 2);
y_n(1, :) = [1 0]; %initial condition
for i = 2:length(t)
dy = ode3(t, y_n(i-1, :));
y_n(i, :) = y_n(i-1, :) + dt(k)*dy;
y_n(i, :) = y_n(i-1, :) + dt*dy;
end
plot(t, y_analytical(t), 'k-', t, y_n(:,1), 'o-');
plot(t, y_n(:,1), 'o', t, y_analytical(t), 'k-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler3.png');
Expand All @@ -24,7 +24,7 @@ b = figure(2);
dt = 0.001;
t = [0:dt:3];
y_heun = heun_sol_order2(@(t,y) ode3(t,y), dt, [1 0], [0 3]);
plot(t, y_analytical(t), 'k-', t, y_heun(:,1), 'o-');
plot(t, y_heun(:,1), 'o-', t, y_analytical(t), 'k-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun3.png');
6 changes: 3 additions & 3 deletions HW6Problem4.m
Expand Up @@ -16,9 +16,9 @@ y_n = zeros(length(t), 2);
y_n(1, :) = [100 0]; %initial condition
for i = 2:length(t)
dy = ode4(t, y_n(i-1, :));
y_n(i, :) = y_n(i-1, :) + dt(k)*dy;
y_n(i, :) = y_n(i-1, :) + dt*dy;
end
plot(t, x_analytical(t), 'k-', t, y_n(:,1), 'o-');
plot(t, y_n(:,1), 'o-', t, x_analytical(t), 'k-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler4.png');
Expand All @@ -28,7 +28,7 @@ b = figure(2);
dt = 0.01;
t = [0:dt:12];
y_heun = heun_sol_order2(@(t,y) ode4(t,y), dt, [100 0], [0 12]);
plot(t, x_analytical(t), 'k-', t, y_heun(:,1), 'o-');
plot(t, y_heun(:,1), 'o-', t, x_analytical(t), 'k-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun4.png');
44 changes: 29 additions & 15 deletions HW6Problem5.m
@@ -1,16 +1,30 @@
%Part b.
dy = ode3(0, [1;0]);
dt = [0.1 0.01];
figure()
hold on
for k = 1:length(dt)
t = [0:dt(k):20]';
y_n = zeros(length(t), 4);
y_n(1, :) = [10 0 0 2]; %initial condition
for i = 2:length(t)
dy = phugoid_ode(t(i-1), y_n(i-1, :));
y_n(i, :) = y_n(i-1, :) + dt(k)*dy;
end
[t23, y23] = ode23(@(t,y) phugoid_ode(t,y), [0,20], [10 0 0 2]);
plot(y_n(:,3), y_n(:, 4), '.', y23(:,3), y23(:,4), 's');
end
dt = 0.1;
t = [0:dt:20]';
y_n1 = zeros(length(t), 4);
y_n1(1, :) = [10 0 0 2]; %initial condition
for i = 2:length(t)
dy = phugoid_ode(t(i-1), y_n1(i-1, :));
y_n1(i, :) = y_n1(i-1, :) + dt*dy;
end

dt = 0.01;
t = [0:dt:20]';
y_n2 = zeros(length(t), 4);
y_n2(1, :) = [10 0 0 2]; %initial condition
for i = 2:length(t)
dy = phugoid_ode(t(i-1), y_n2(i-1, :));
y_n2(i, :) = y_n2(i-1, :) + dt*dy;
end

a = figure(1);
set(0, 'defaultAxesFontsize', 16)
set(0, 'defaultTextFontsize', 16)
set(0, 'defaultLineLineWidth', 2)
[t23, y23] = ode23(@(t,y) phugoid_ode(t,y), [0,20], [10 0 0 2]);
plot(y_n1(:,3), y_n1(:, 4), '.', y_n2(:,3), y_n2(:, 4), 'o', y23(:,3), y23(:,4), '-');
title('Height of Plane vs. Distance')
xlabel('Distance (m)')
ylabel('Height (m)')
legend('dt = 0.1', 'dt = 0.01', 'analytical', 'Location', 'Northeast')
saveas(a, 'problem5.png');
Binary file added euler2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified euler3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified euler4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added heun2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified heun3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified heun4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40d3520

Please sign in to comment.