Second derivative of a function handle

6 ビュー (過去 30 日間)
Stanley Ka
Stanley Ka 2021 年 5 月 23 日
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 件のコメント
Stanley Ka
Stanley Ka 2021 年 5 月 23 日
Thanks a lot, I'll have a go with what you said.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2021 年 5 月 23 日
編集済み: Stephen23 2021 年 5 月 23 日
Tip for beginners: whenever you use EVAL you are doing something wrong.
With symbolic variables you do not need to define anonymous functions like that.
This should get you started, you probably need to fix the code in other ways (note that you will not get anything meaningful out of that plot when the data varies so much in amplitude). I recommend storing the data in a matrix and plotting after the loop (makes it easier to add a legend).
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:0.1:50;
for a = [10,5,1]
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);
zp0 = b*cos(a.*t) + c*sin(a.*t) + H;
zp1 = diff(zp0);
zp2 = diff(zp1);
Z = double(subs(zp2,t,T));
plot(T,Z)
hold on
end

その他の回答 (1 件)

Sulaymon Eshkabilov
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
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
Sulaymon Eshkabilov 2021 年 5 月 23 日
That is just copy and paste of the computed formulation from diff() above.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by