In: Mechanical Engineering
This is a numerical methods question using MATLAB.
Which of the following code snippets finds the forward difference 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 correct answer.
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 |
x length is 5 and y length is also 5 there for obtained derivative array consist 4 value.
option a)
derivative array solved by using x and y arrays and stored in derivative array from 1 to 4 elements and non(NaN) value stored in derivative array as a 5th element.
option c)
derivative array solved by using x and y arrays and stored in derivative array from 2 to 5 elements and non(NaN) value stored in derivative array as a 1st element.
option d)
derivative array solved by using x and y arrays and stored in derivative array from 2 to 5 elements and non(NaN) value stored in derivative array as a 1st element.
finally
option b)
when we calculate the derivative array (for loop k=1 2 3 4 5)at 5th iteration means k=5
==>d(5)=(y(6)-y(5)/x(6)-x(5)) error occured due to there is no 6th element in x and y
option b is wrong answer remaing option are right
a and c,d are same but dealy is there that's why graph will shift right side one unit