Second derivative of a function handle
이전 댓글 표시
Hi, I have a function that I want to differentiate twice and plot it against time for different values of a. I looked through some forum posts in which they recommend to use syms but I haven't learned about syms yet. I tried the following code but the system will return an error of "This statement is incomplete." Can someone have a look?
syms t
D = 5; % Damping factor
k = 37; % Spring constant
m = 0.5; % Quarter mass of the car
H = 0.1; % Height
L = 0.2; % Length
t = (0:1:50);
for a = [1, 5, 25, 50, 100]
b = (-(k - m*a^2)*k*H)/((k - m*a^2)^2 + (a*D)^2);
c = (-a*D*k*H)/((k-m*a^2)^2 + (a*D)^2);
zp = @(t)b*cos(a.*t) + c*sin(a.*t) + H;
zp1 = eval(['@(t)' char(diff(zp(t)))]);
zp2 = eval(['@(t)' char(diff(zp1(t)))]);
plot(t,zp2(t));
hold on
end
채택된 답변
추가 답변 (1개)
Sulaymon Eshkabilov
2021년 5월 23일
Hi,
Here is a corrected and more efficient solution to your exercise.
clc
clearvars
syms zp(t) a b c
D = 5; % Damping factor
k = 37; % Spring constant
m = 0.5; % Quarter mass of the car
H = 0.1; % Height
L = 0.2; % Length
zp(t) = b*cos(a.*t) + c*sin(a.*t) + H;
Dzp=diff(zp(t),t);
D2zp=diff(Dzp,t);
t = (0:.001:1); % t = [0:1:50] is not a good resolution and thus, t = [0, 1] is chosen
%%
a =double([1, 5, 25, 50, 100]);
b = (-(k - m*a.^2)*k*H)./((k - m*a.^2).^2 + (a*D).^2);
c = (-a*D*k*H)./((k-m*a.^2).^2 + (a*D).^2);
DC=(- a(:).^2.*b(:).*cos(a(:).*t) - (a(:).^2).*c(:).*sin(a(:).*t)); % This is the 2nd diff of zp(t)
plot(t, DC, '-')
댓글 수: 2
Torsten
2021년 5월 23일
And why do you calculate the second derivative using the diff command if finally you use a computation by hand ?
Sulaymon Eshkabilov
2021년 5월 23일
That is just copy and paste of the computed formulation from diff() above.
카테고리
도움말 센터 및 File Exchange에서 Elementary Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
