179k views
1 vote
Write a user written function that expects as its input argument a vector of x values and a vector of corresponding y-values. The function must return a matrix with 3 columns. The first column must be the x-vector that was provided by the user. The other columns must contain approximations for the first derivative (dy/dx). Column 2 must contain forward difference approximations, column 3 must contain backward difference approximations. Populate the matrix with NaN if the method cannot be computed correctly for a given point.

1 Answer

3 votes

Answer:

See attached Command window for answer

Step-by-step explanation:

function m=FirstDer(x,y)

m=zeros(length(x),3);

m(:,1)=x;

forward=(y(2:end)-y(1:end-1))./(x(2:end)-x(1:end-1));

forward=[forward;NaN];

backward=(y(2:end)-y(1:end-1))./(x(2:end)-x(1:end-1));

backward=[NaN;backward];

m(:,2)=forward;

m(:,3)=backward;

Write a user written function that expects as its input argument a vector of x values-example-1
User Whales
by
5.1k points