trignonemetric second derivatives of a 2x20001 matrix

2 次查看(过去 30 天)
thetad= zeros(2,length(qd));
for t=0:0.001:20
thetad(1,:)=1+sind(t)+sind(2*t);
thetad(2,:)=1+cosd(4*t)+cosd(6*t);
end
thetad_d=zeros(2,length(qd));
for t=0:0.001:20
thetad_d(1,:)=(diff(1+sind(t)+sind(2*t),t));
thetad_d(2,:)=(diff(1+cosd(4*t)+cosd(6*t),t));
end
thetad_dd=zeros(2,length(qd));
for t=0:0.001:20
thetad_dd(1,:)=(diff(diff(1+sind(t)+sind(2*t))),t);
thetad_dd(2,:)=(diff(diff(1+sind(t)+sind(2*t))),t);
end
When i run this i get the following error
Error using diff
Difference order N must be a positive
integer scalar.
Error in A4Q2 (line 42)
thetad_d(1,:)=(diff(1+sind(t)+sind(2*t),t));
Please can somebody help me fix this? Thank you

采纳的回答

Jan
Jan 2017-3-22
编辑:Jan 2017-3-22
Use a vectorized version instead of the loops:
tStep = 0.001;
t = 0:tStep:20
thetad = [1 + sind(t) + sind(2*t); ... % Perhaps sind() -> sin()?
1 + cosd(4*t) + cosd(6*t)];
thetad_d = diff(thetad, 1, 2) / tStep; % Not t as 2nd argument!
thetad_dd = diff(thetad, 2, 2) / (tStep ^ 2);
See: doc diff
diff reduces the number of elements. For the derivative this is smarter:
thetad_d = gradient(thetad, tStep);
thetad_dd = gradient(thetad_d, tStep);

更多回答(1 个)

Guillaume
Guillaume 2017-3-22
I'm not sure what you're trying to do. You are calling the diff function with a scalar as first argument and a non-integer as the second. Regardless of the second argument, diff of a scalar will give you an empty matrix. As the error message tells you, the second argument must be integer.
It does not look like you know what the diff function does, so read its documentation
Note 1: although it does not error, your first loop also doesn't work since you're constantly overwriting the values you've calculated in the previous iteration. I recommend you learn to debug your code and step through it line by line to see what it is actually doing.
Note 2:
something = (somethingelse);
Why the extra brackets that serve no purpose but just make it harder to read?

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by