trignonemetric second derivatives of a 2x20001 matrix
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Jan
on 22 Mar 2017
Edited: Jan
on 22 Mar 2017
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);
More Answers (1)
Guillaume
on 22 Mar 2017
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.
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?
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!