Numerical differentiation and integration

226 views (last 30 days)
Left Terry
Left Terry on 12 Dec 2024 at 15:26
Edited: Torsten on 14 Dec 2024 at 21:36
Hello. Programmming is my weakest point and I have a problem regarding numerical analysis. I was given a problem regarding projectile motion with t and s(t) as data list where s(t) is projectile's trajectory in x-y plane and i need to:
  1. write a program that calculates the velocity v = ds/dt of the projectile in m/s with 2nd order accuracy for t = 5,15,…,115 s
  2. write a program that calculates the velocity using a second-order polynomial interpolation (either Lagrange or Newtonian) at the points t = 74,75,...95 from the values ​​v (t = 75),v (t = 85), and v (t = 95) found in the previous step. From the values ​​of the velocity at the interpolation points, find the time at which the projectile reaches the highest point of the trajectory, at which the velocity becomes minimum.
  3. Using the velocity values ​​from the first step of the task, write a program that calculates the integral from t = 0 s to t = 115 s using Simpson's rule. Estimate the error of Simpson's integration in this case and compare it with the deviation of s(t = 115) that you calculated from the table value. Which error dominates: the integration rule or the arithmetic derivation from which the velocity values ​​used in the integration are derived?
I tried to answer the 1st step but i think it's not the wanted result. My code for this is below (any help with the smallest possible code will be useful, thanx in advance) :
clear all, clc, close all, format short
% Data list
t = [0,5:10:115]; % in sec
s = [0, 4892.3, 14041.3, 22371.3, 29926.1, 36764.4, 42965.4, 48634.8, 53910.3, 58961.8, 63981.6, 69165.5, 74692.1]; % in m
v0 = 1000; % in m/sec
%-------------------------------------------------------------------------------------------------------------------
N = length(t);
v = zeros(1,N);
for i = 2:N
v(1,i) = (s(i) - s(i-1)) / (t(i) - t(i-1));
end
v(1) = v0;
disp('Velocity in m/s '), v
Velocity in m/s
v = 1×13
1.0e+03 * 1.0000 0.9785 0.9149 0.8330 0.7555 0.6838 0.6201 0.5669 0.5275 0.5051 0.5020 0.5184 0.5527
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
subplot(2,1,1)
plot(t,s)
title('s - t')
xlabel('t (s)')
ylabel('s (m)')
subplot(2,1,2)
plot(t,v)
title('v - t')
xlabel('t (s)')
ylabel('v (m/s)')
  2 Comments
Torsten
Torsten on 12 Dec 2024 at 19:03
You compute the velocity with 1st order accuracy. Do you know which formula to use for 2nd order accuracy ? Do you know how to treat the special cases t = 5 and t = 115 ?
Left Terry
Left Terry on 12 Dec 2024 at 19:11
Hello. Thanks for the reply. I think i made some progress. My new code regarding the 1st step is shown below (I am not sure if i treated the special cases right):
clear all, clc, close all, format short
% Data list
t = [0,5:10:115]; % in sec
s = [0, 4892.3, 14041.3, 22371.3, 29926.1, 36764.4, 42965.4, 48634.8, 53910.3, 58961.8, 63981.6, 69165.5, 74692.1]; % in m
v0 = 1000; % in m/sec
%-------------------------------------------------------------------------------------------------------------------
N = length(t);
h = 10;
%-------------------------------------------------------------------------------------------------------------------
% 1st order accuracy
v1 = zeros(1,N-1);
for i = 1:N-1
v1(1,i) = (s(i+1) - s(i)) / (t(i+1) - t(i));
end
v1(end) = v1(end-1);
disp('Velocity in m/s'), v1
Velocity in m/s
v1 = 1×12
978.4600 914.9000 833.0000 755.4800 683.8300 620.1000 566.9400 527.5500 505.1500 501.9800 518.3900 518.3900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%-----------------------------------------------------------------------------------------------------
% 2nd order accuracy
v2 = zeros(1,N-1);
for i = 1:N-2
if i == 1
v2(1,i) = (s(3) - s(1)) / 15;
else
v2(1,i) = (s(i+2) - s(i)) / (2*h);
end
end
v2(12) = (s(end) - s(end-1))/10;
disp('Velocity in m/s'), v2
Velocity in m/s
v2 = 1×12
936.0867 873.9500 794.2400 719.6550 651.9650 593.5200 547.2450 516.3500 503.5650 510.1850 535.5250 552.6600
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
subplot(2,1,1)
plot(t,s)
title('s - t')
xlabel('t (s)')
ylabel('s (m)')
subplot(2,1,2)
plot(t(2:end),v1)
title('v - t')
xlabel('t (s)')
ylabel('v1 (m/s)')
hold on
plot(t(2:end),v2)
legend('1st order','2nd order')

Sign in to comment.

Accepted Answer

Torsten
Torsten on 12 Dec 2024 at 20:18
Edited: Torsten on 12 Dec 2024 at 20:25
t = [0,5:10:115];
s = [0, 4892.3, 14041.3, 22371.3, 29926.1, 36764.4, 42965.4, 48634.8, 53910.3, 58961.8, 63981.6, 69165.5, 74692.1]; % in m
n = numel(t);
%1st order
v1(1) = 1000;
for i = 2:n
v1(1,i) = (s(i) - s(i-1)) / (t(i) - t(i-1));
end
%2nd order
v2(1) = 1000;
v2(2) = s(1)*(t(2)-t(3))/((t(1)-t(2))*(t(1)-t(3)))+...
s(2)*((t(2)-t(1))+(t(2)-t(3)))/((t(2)-t(1))*(t(2)-t(3)))+...
s(3)*(t(2)-t(1))/((t(3)-t(1))*(t(3)-t(2)));
for i = 3:n-1
v2(i) = (s(i+1)-s(i-1))/(t(i+1)-t(i-1));
end
v2(n) = s(n-2)*(t(n)-t(n-1))/((t(n-2)-t(n-1))*(t(n-2)-t(n)))+...
s(n-1)*(t(n)-t(n-2))/((t(n-1)-t(n-2))*(t(n-1)-t(n)))+...
s(n)*((t(n)-t(n-2))+(t(n)-t(n-1)))/((t(n)-t(n-2))*(t(n)-t(n-1)));
subplot(2,1,1)
plot(t,s)
title('s - t')
xlabel('t (s)')
ylabel('s (m)')
subplot(2,1,2)
plot(t,v1)
title('v - t')
xlabel('t (s)')
ylabel('v1 (m/s)')
hold on
plot(t,v2)
legend('1st order','2nd order')
  10 Comments
Left Terry
Left Terry on 14 Dec 2024 at 21:24
Edited: Left Terry on 14 Dec 2024 at 21:24
Thanks. I will check it out.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!