Uploaded by Nitish Bhardwaj

Experiment 2 D and S

advertisement
%Initial conditions
t_0 = 0;
v_0 = [3.400; 2.500];
h_0 = [2.050; 1.200];
t_end = 200; %time upper limit
delta = 20.00; %step size
%Declaration of the variables
t = t_0:delta:t_end;
v = zeros(length(t), length(v_0));
h = zeros(length(t), length(v_0));
vdot = zeros(length(t), length(v_0)); %dv/dt
hdot = zeros(length(t), length(v_0)); %dh/dt
%Euler's method implementation
for i = 1:length(v_0)
v(1, i) = v_0(i);
h(1, i) = h_0(i);
for j = 1:length(t)-1
vdot(j, i) = 0.0107*h(j, i) - 0.00205*v(j, i)^2;
hdot(j, i) = 0.311 - 0.0624*v(j, i);
v(j+1, i) = v(j, i) + delta*vdot(j, i);
h(j+1, i) = h(j, i) + delta*hdot(j, i);
end
vdot(end, i) = 0.0107*h(end, i) - 0.00205*v(end, i)^2;
hdot(end, i) = 0.311 - 0.0624*v(end, i);
end
for i = 1:length(v_0)
table(t', v(:, i), h(:, i),'VariableNames',{'Time', 'Velocity', 'Height'})
end
ans = 11×3 table
Time
Velocity
Height
1
0
3.4000
2.0500
2
20
3.3647
4.0268
3
40
3.7623
6.0476
4
60
4.4761
7.5723
5
80
5.2751
8.2060
6
100
5.8903
7.8427
7
120
6.1461
6.7116
8
140
6.0336
5.2612
9
160
5.6669
3.9512
10
180
5.1958
3.0989
11
200
4.7521
2.8345
ans = 11×3 table
1
Time
Velocity
Height
1
0
2.5000
1.2000
2
20
2.5006
4.3000
3
40
3.1644
7.3993
4
60
4.3373
9.6702
5
80
5.6354
10.4772
6
100
6.5755
9.6642
7
120
6.8709
7.6780
8
140
6.5784
5.3232
9
160
5.9433
3.3333
10
180
5.2084
2.1361
11
200
4.5533
1.8560
for i = 1:length(v_0)
p = plot(t, v(:, i), "-");
p.Marker = "p";
hold on;
end
hold off;
title("Velocity vs Time");
xlabel("Time");
ylabel("Velocity");
legend("v_0 = " + v_0);
2
for i = 1:length(v_0)
p = plot(t, h(:, i), "-");
p.Marker = "p";
hold on;
end
hold off;
title("Height vs Time");
xlabel("Time");
ylabel("Height");
legend("h_0 = " + h_0);
3
4
Download