필터 지우기
필터 지우기

Matlab problem: How to calculate a rough, approximate, derivative vector by using the following formula?

조회 수: 2 (최근 30일)
Here is the complete task which I need to solve:
We know that the derivative is the coefficient k of a tangent line with mathematical expression, y=kx + m, that goes through a certain point, P, on a function curve. Lets start with a simple function, namely, y=x^2.
(a) Calculate and Plot this function for -10 < x < 10 with small enough step length to get a smooth curve.
(b) Now, In (a) you calculated two equally long vectors, one for the chosen x values and one with the calculated y values. From these two vectors, we can calculate a rough, approximate, derivative vector that we can call Yprimenum: Use a for-loop to calculate each element of Yprimenum according to the formula:
Yprimenum(i) = (y(i+1) – y(i)) / ∆x Where ∆x is the x step length, or equivallently x(i) – x(i-1)
NOTE: (a) is solved and for the (b) I wrote the following code but not working.
if true
x=-10:10;
for i=1: length(x);
for y=x.^2
Yprimenum=(y(i+1)-y(i))./(x(i)-x(i-1));
end;
end;
display Yprimenum;
end
  댓글 수: 3
John D'Errico
John D'Errico 2014년 5월 27일
Why do you think it necessary to add the "if true" conditional around your code? Silly.
Jabir Al Fatah
Jabir Al Fatah 2014년 5월 30일
ahh u should know that. if true was added automatically while pasting code here and i just forgot to modify that.

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

채택된 답변

George Papazafeiropoulos
George Papazafeiropoulos 2014년 5월 27일
편집: George Papazafeiropoulos 2014년 5월 27일
x=-10:10;
y=x.^2;
Yprimenum=diff(y)./diff(x)
  댓글 수: 5
Jabir Al Fatah
Jabir Al Fatah 2014년 5월 30일
But here this formula "Yprimenum(I-1)=(y(I)-y(I-1))/(x(I)-x(I-1))" doesn't follow mine. And also why it's (I-1) with Yprimenum? Moreover, I need to plot Yprimenum in the same figure as Y and Yprime. What is the ∆X value ? I am eagerly waiting for your update of your answer PLEASE! Thank you.
George Papazafeiropoulos
George Papazafeiropoulos 2014년 5월 31일
편집: George Papazafeiropoulos 2014년 5월 31일
Try this:
% 1st way: for loop
Yprimenum1=zeros(1,2*10);
x=-10:10;
y=x.^2;
for I=2:length(x)
Yprimenum1(I-1)=(y(I)-y(I-1))/(x(I)-x(I-1));
end
% 2nd way: diff
Yprimenum2=diff(y)./diff(x);
% test the results
all(Yprimenum1==Yprimenum2)
Be careful that the length of the approximate derivative is equal to the length of the initial vectors minus 1

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by