-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.asv
78 lines (65 loc) · 1.56 KB
/
Examples.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
%% Eulers Method
f = @(x,y) y+2*x-1;
yInitial = 1;
step = 0.05;
upperBound = 1;
lowerBound = 0;
EulersMethod(yInitial, step,lowerBound, upperBound, f);
%% Heuns Method
f = @(x,y) -5*y+exp(-2*x);
yInitial = 1;
step = 0.2;
upperBound = 2;
lowerBound = 0;
HeunsMethod(yInitial,step,lowerBound,upperBound,f);
%% 2nd Order Runge Kutta
f = @(t,I) (-20/50)*I+10/50;
yInitial = 0;
step = 0.0001;
upperBound = 10;
lowerBound = 0;
x = transpose(lowerBound:step:(upperBound-step));
yAnalytical = (exp(-2*x)+2*exp(-5*x));
a = RungeKutta2(yInitial, step, lowerBound, upperBound, f);
b = RungeKutta4(yInitial, step, lowerBound, upperBound, f);
sum((yAnalytical - a).^2)
sum((yAnalytical - b).^2)
plot(x,yAnalytical);
plot(x,a);
plot(x,b);
%% Assignment 8 No 3
f = @(t,v)9.8-(0.203*v^2)/80;
vInitial = 0;
step = 0.0005;
upperBound = 20;
lowerBound = 0;
v = RungeKutta2(vInitial,step,lowerBound,upperBound,f);
t = (lowerBound:step:(upperBound-step));
vAnalytical = (62.1455.*exp(0.315389.*t)-62.1455)./(exp(0.315389.*t)+1);
figure
plot(t,v);
hold on
plot(t,vAnalytical)
hold off
stepInverse = 1/step;
g = @(t,y)v(int32(((t+step)*stepInverse)),1);
yInitial = 0;
upperBound = 20;
lowerBound = 0;
y = RungeKutta2(yInitial,step,lowerBound,upperBound,g);
yAnalytical = -62.1455.*t+394.089.*log(exp(0.315389.*t)+1)-273.161;
for i=1:size(y,1)
if y(i,1) >= 500
solRight = y(i,1);
solLeft = y(i-1,1);
solRightIndex = i;
solLeftIndex = i-1;
break
end
end
tSol = (t(1,solLeftIndex)+t(1,solRightIndex))/2;
figure
plot(t,y)
hold on
plot(t,yAnalytical);
hold off