I am looking into how to create a function that would calculate the first derivative of a data set using the finite difference methods

조회 수: 15 (최근 30일)
Hi I have a project where I need to complete the following problems. I am having issues with my function and having it run with the data given so that I can plot it. Any suggestions would be great! I have attached the exp.txt as well.
  댓글 수: 1
Sarah Elena Aiko Johnson
Sarah Elena Aiko Johnson 2022년 11월 14일
Correction: The error that keeps popping up is saying it is undefined due to the type being 'double' however I tried to change it to single but still did not work.

댓글을 달려면 로그인하십시오.

답변 (1개)

William Rose
William Rose 2022년 11월 14일
You have posted images which are screenshots of your code. It is more helpful if you cite and paste the code into your posting, and then highlight it and click the "code" button to make it executable code in your answer. Then you can run it in the answer window, and the error massage will appear, and it will be easier for others to examn ethe code and make helpful edits.
You have written your own script "importdata.m". importdata() is already the name of a widely used Matlab function. I would not use that name for my own script, to avoid confusion, and so that I can use Matlab's function of that name.
When importdata() ran, Matlab could not find the function difffunc(). Either it is in a directory that is not on the Matlab path (google "Matlab path", if you are not sure what that means, and to learn how to add a directory to the path), or you saved the function with a different name (diffunc.m perhaps?).
difffunc() has issues.
  1. It has 2 inputs, x and y. You use x in the denominator of the derivative and y in the numerator. This makes sense if you have data sampled with uneven step size. But if your data are sampled with equal steps, then this is an unnecessary complication.
  2. Related to issue 1. You call difffunc(DATA,DATA). This will return a derivative of unity since the numerator and denominator are the same with such a call. Or it will cause an error since DATA is an array and not a vector. You could call it with difffunc(DATA(:,1),DATA(:,2)), so that column 1 and column 2 play the role of x and y.
  3. Does your importdata() reverse the rows and columns? The text file has two long columns. If your importdata() does NOT reverse the rows and columns, then DATA will also have two long columns. And if it does , then the command plot(DATA(1,:),DATA(2,:),'r') will not work as desired. You should use plot(DATA(:,1),DATA(:,2),'r').
  4. difffunc() computes the derivative three ways, but the first two ways will get overwritten by the last way, so there is no reason to do the first two.
  5. The forward and reverse methods will return the same vector of values. The difference comes in what x-values you choose to associate with the elements of the returned vector.
  6. The formula in the the centered difference loop has an error, if you want it to work for uneven step sizes. And if you assume the step size is constant, then your fomulas can be simplified, for all three loops.
  7. The denominator in the backward difference formula looks forward when it should look backward, like the numerator. This will cause an "array of of bounds" error when you get to the last element of the array.
  8. The loop counters for the forward and backward methods will not work as desired. For forward, you want: for h=1:j-1, {commands} end. For backward, you want: for h=2:j, {commands} end.
  9. You don't need to use a for loop to do forward or centered or backward differences. For example, in the case of forward or backward differences, you could write the following, without a loop:
f_derivative=(y(2:end)-y(1:end-1))./(x(2:end)-x(1:end-1));
That gives you some ideas to work on. Good luck.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by