This is a numerical methods question using MATLAB.
Which of the following code snippets finds the forwarddifference estimate of the derivative at each of the x values.Assume x and y have been previously defined, for example as
y=[10,20,25, 27.5, 30];
x = [0.3,0.5, 0.8, 0.9, 1];
(d is the derivative variable name)
Although not necessarily so, there may be more than one correctanswer.
a) | for k=1:length(y)-1 d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end d(k+1)=NaN |
b) | for k=1:length(y) d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end |
c) | d(1)=NaN; for k=1:length(y)-1 d(k+1)=(y(k+1)-y(k))/(x(k+1)-x(k)); end |
d) | d(1)=NaN; for k=2:length(y) d(k)=(y(k)-y(k-1))/(x(k)-x(k-1)); end |