Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
problem 4
  • Loading branch information
nin13001 committed Dec 6, 2017
1 parent f97cd0a commit a3249ee
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Expand Up @@ -93,3 +93,47 @@ function y = prob3ptC(prob3_ode, dt, y0,tlimits)
end
```
![](assets/README-fcbec9a1.png)

# Problem 4
## Part A
The analytical solution to v'=g-cd/m v*v is

## Part B
```
cd = 0.25;
m = 60;
g = 9.81;
dt = 0.1;
t = [0:dt:12]';
x = zeros(length(t),2);
x(1,:) = [1000, 0];
for i = 2:length(t)
dx = prob4_ode(t,x(i-1,:));
x(i,:) = x(i-1,:)+dt*dx;
end
```

## Part C
```
function x = prob4ptC(prob4_ode, dt, x0,tlimits)
t = [tlimits(1):dt:tlimits(2)];
x = zeros(length(t),length(x0));
x(1,:) = x0;
for i = 2:length(t)
dx = prob4_ode(t(i-1),x(i-1,:));
x(i,:) = x(i-1,:)+dt*dx;
dxc = (dx + prob4_ode(t(i),x(i,:)))/2;
x(i,:) = x(i-1,:)+dt*dxc;
n = 1;
while (1)
n = n+1;
yold = x(i,:);
dxc = (dx+prob4_ode(t(i),x(i,:)))/2;
x(i,:) = x(i-1,:)+dt*dxc;
ea = abs(x(i,:)- yold)./x(i,:)*100;
if max(ea) < 0.0001 || n>100; break, end
end
end
end
```
![](assets/README-ac6c7a25.png)
Binary file added assets/README-ac6c7a25.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions prob4_ode.m
@@ -0,0 +1,8 @@
function dx = ode4(t,x)
g = 9.81;
cd = 0.25;
m = 60;
dx = zeros(size(x));
dx(1) = x(2);
dx(2) = g -(cd/m)*x(2)^2;
end
20 changes: 20 additions & 0 deletions prob4plot.m
@@ -0,0 +1,20 @@
set(0, 'defaultAxesFontSize', 16)
set(0,'defaultTextFontSize',14)
set(0,'defaultLineLineWidth',3)
cd = 0.25;
m = 60;
g = 9.81;
dt = 0.1;
t = [0:dt:12]';
x = zeros(length(t),2);
x(1,:) = [1000, 0];
for i = 2:length(t)
dx = prob4_ode(t,x(i-1,:));
x(i,:) = x(i-1,:)+dt*dx;
end
c = figure(3);
x_heun = prob4ptC (@(t,x) prob4_ode(t,x), 0.1, [1000, 0], [0 12]);
plot(t, m/cd*log(cosh(sqrt((g*cd)/m)*t))+1000,t,x(:,1),t,x_heun(:,1));
legend('Analytical','Euler','Heuns','Location','Northeast')
xlabel('Time')
ylabel('Position')
20 changes: 20 additions & 0 deletions prob4ptC.m
@@ -0,0 +1,20 @@
function x = prob4ptC(prob4_ode, dt, x0,tlimits)
t = [tlimits(1):dt:tlimits(2)];
x = zeros(length(t),length(x0));
x(1,:) = x0;
for i = 2:length(t)
dx = prob4_ode(t(i-1),x(i-1,:));
x(i,:) = x(i-1,:)+dt*dx;
dxc = (dx + prob4_ode(t(i),x(i,:)))/2;
x(i,:) = x(i-1,:)+dt*dxc;
n = 1;
while (1)
n = n+1;
yold = x(i,:);
dxc = (dx+prob4_ode(t(i),x(i,:)))/2;
x(i,:) = x(i-1,:)+dt*dxc;
ea = abs(x(i,:)- yold)./x(i,:)*100;
if max(ea) < 0.0001 || n>100; break, end
end
end
end

0 comments on commit a3249ee

Please sign in to comment.