Find the value of x when the first derivative is equal to 0

조회 수: 20 (최근 30일)
Angelavtc
Angelavtc 2022년 1월 10일
댓글: Angelavtc 2022년 1월 10일
Hello community,
I have the data x, y (.mat version) from which I compute its first derivative as follows :
dy = diff(y)./diff(x);
I need to find the points (x, y) such that the first derivative is 0. (In this case, there should be 2 points)
I have tried with this code, but it doesn't work, it tells me "Second argument must be a vector of symbolic variables"
solve(dy==0,x)
Any idea how to fix the problem?
Thanks in advance!

채택된 답변

Torsten
Torsten 2022년 1월 10일
편집: Torsten 2022년 1월 10일
x = -pi/2:0.01:pi/2;
y = cos(x);
dydx = diff(y)./diff(x);
[~,idx] = min(dydx.^2);
xmin = (x(idx+1)+x(idx))/2
xmin is the point with slope closest to 0.
  댓글 수: 3
Torsten
Torsten 2022년 1월 10일
편집: Torsten 2022년 1월 10일
function main
x = -3*pi/2:0.01:3*pi/2;
y = cos(x);
dydx = diff(y)./diff(x);
[B,I] = sort(dydx.^2);
for i=1:3
solx(i) = (x(I(i)) + x(I(i)+1))/2;
soly(i) = (y(I(i)) + y(I(i)+1))/2;
end
solx(1),soly(1)
solx(2),soly(2)
solx(3),soly(3)
end
gives you the three points in [-3/2*pi:3/2*pi] where cos(x) has least, second least and third least slope.
Instead of squaring dydx, you could also have used abs(dydx). Can you imagine why this is necessary ?
Angelavtc
Angelavtc 2022년 1월 10일
@Torsten I understand. With the squared derivative, the negative values become positive and the minimum is 0, the value we are looking for. Thank you very much :)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by