How to find the roots of a derivative
10 次查看(过去 30 天)
显示 更早的评论
Hi!
I'm trying to find the roots of the derivative.
clear all
m = 10; % kg
l = 5; % m
k = 40; % N/m
g = 9.81;
theta = linspace(0,80,200);
V = 0.5*k*(l^2)*(sind(theta).^2)+0.5*m*g*l*cosd(theta);
dtheta = diff(theta);
dV = diff(V);
deriv = dV./dtheta;
newTheta = theta(1:length(theta)-1);
plot(newTheta, deriv)
guess = input('Make a guess: ');
v = fzero(@(newTheta)(deriv), guess)
But I get:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 308)
elseif ~isfinite(fx) || ~isreal(fx)
Error in StabilitetB (line 15)
v = fzero(@(nyTheta)(derivatan), guess)
Desperation brought me here, what am I doing wrong?! The plot of the derivative comes up perfectly fine. The issue here has to do with fzero and the anonymous function and I don't understand why.
0 个评论
回答(2 个)
Roger Stafford
2014-10-25
Rather than tell you what is wrong with your method, I prefer to tell you how I think you should approach the problem. The derivative of V with respect to theta in degrees, using the principles of calculus is:
dV/dtheta = (0.5*k*L^2*2*sind(theta)*cosd(theta) - 0.5*m*g*L*sind(theta))*pi/180
= 0.5*L*pi/180*sind(theta) * (2*k*L*cosd(theta)-m*g)
Expressed in this factored form it is quickly evident that the derivative is zero whenever sind(theta) = 0 or when cosd(theta) = m*g/(2*k*L). Hence its roots are:
theta = 180*n
for any integer n, along with
theta = acosd(m*g/(2*k*L)) or 360-acosd(m*g/(2*k*L))
together with any integral multiple of 360 degrees added or subtracted from these. Using that method is very much more satisfactory than resorting to an iterative approach. (By the way, it's a lot easier to deal with derivatives of the trigonometric functions if you use radians rather than degrees.)
3 个评论
Roger Stafford
2014-10-25
Well, in answer to that, you have defined only a discrete approximation to the derivative of your function by using 'linspace'. This will not work for 'fzero' which expects to be able to assign arbitrary values to the arguments of the functions for which it is finding roots. If you wish to use 'fzero', at the very least you will have to use matlab's symbolic form of 'diff' to find an accurate formula for the derivative function. Having done so, it seems a shame to use 'fzero' when the roots are so obvious at that point.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!