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일

0 개 추천

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일

0 개 추천

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 ?
That is just copy and paste of the computed formulation from diff() above.

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

카테고리

도움말 센터File Exchange에서 Elementary Math에 대해 자세히 알아보기

질문:

2021년 5월 23일

댓글:

2021년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by