Help with Numerical Differentiation

4 visualizzazioni (ultimi 30 giorni)
jacob booth
jacob booth il 23 Apr 2021
Modificato: Clayton Gotberg il 24 Apr 2021
Numerical Differentiation is pretty new to me i want to set the program to plot 2 graphs using first order forward,when i change the x=0:40:520 to x=0:20:520 i get the error index exceeds the number of array elements(26) how do i fix this error?
clear;
clc;
x=0:40:520;
F=[0 139 298 433 685 1026 1279 1373 1490 1634 1800 1986 2417 2651 2915 3303 3516 3860 4216 4630 5092 5612 6184 6760 7327 7581];
h=x(2)-x(1) ;
n=length(x) ;
xForward=x(1:n-1);
dFForward=(F(2:n)-F(1:n-1))/h;
xBackward=x(2:end);
dFBackward=(F(2:n)-F(1:n-1))/h;
xCenteral=x (2:n-1);
dFCentral=(F(3:n)-F(1:n-2))/(2*h);
hold on
plot (xCenteral,dFCentral,'r' )
plot (xForward,dFForward, 'k')
plot (xBackward,dFBackward,'g')
legend ('centeral','Forward','Backward')
title('NUMERICAL DIFFERENCING USING STEP SIZE 40s')
xlabel ('Time')
ylabel ('Acceleration')
  2 Commenti
Clayton Gotberg
Clayton Gotberg il 24 Apr 2021
Do you have a more specific programming question? I'm happy to help with logical trouble or understanding a function but I don't want just to give you code that you could turn in for an assignment.
jacob booth
jacob booth il 24 Apr 2021
i have edited the question i can figure out how to get rid of this error

Accedi per commentare.

Risposte (1)

Clayton Gotberg
Clayton Gotberg il 24 Apr 2021
Modificato: Clayton Gotberg il 24 Apr 2021
The error is because you have increased the number of elements in the time array x too high for the number of elements in the result array F. When you asked for x = 0:40:520 you got 14 elements to match the first 14 elements in F - note that F(14) is not the last F - but when you used x = 0:20:520 you got 27 time elements with only 26 result elements.
I think this raises two problems for you.
First, do you need to increase the number of results or decrease your time interval? For example, is it meant to stop at t = 500 instead?
Second, you're supposed to span the whole interval even with h = 40, so you need to find a way to make the number of results equal to the number of times. For example, you could pull out every other sample until you reach the end:
h = 20;
x = 0:h:520; %1x14
F = [0 139 298 433 685 1026 1279 1373 1490 1634 1800 1986 2417 2651 ...
2915 3303 3516 3860 4216 4630 5092 5612 6184 6760 7327 7581]; %1x26
% Ellipses let you separate lines for readability
n = length(x);
sampling_rate = ceil(40/h); % Here, 20 is 1/2 of 40 so you want every other sample.
% Note that this only works well when h is 1/n of 40 (n is an integer) because
% otherwise you're not skipping whole numbers.
F_sampled = F(1:sampling_rate:end); % Get every other value in F
plot(x,F_sampled)
% This code still won't work with the F vector provided here because it's
% one value short from what it expects. F_sampled is 1x13 but x is still
% 1x14.
  1 Commento
Clayton Gotberg
Clayton Gotberg il 24 Apr 2021
Apologies for the earlier code - I wrote it on mobile and it showed.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by