How To Cycle Through an Array input through an Anonymous function
2 views (last 30 days)
Show older comments
Hi! I am tyring to create a script that will go through the x-values and y-values array below. I want it to output the joint angle at each x-value and y-value in ROBOTARMPT2. But I am not sure why it is not workng or how to do it. Please help! :)
%%ROBOT2.m
function [J, F] = robot2(x)
l1 = 5; % length of first arm
l2 = 6; % length of second arm
xval = linspace(3, 10, 20);
slope = (8-2)/(3-10);
yval = slope*xval ;
for i = 1:1:length(xval)
F = [l1*cos(x(1))+l2*cos(x(1)+x(2))-xval(i); l1*sin(x(1))+l2*sin(x(1)+x(2))-yval(i)]; % compute x coordinates; % compute y coordinates
J = [-5*sin(x(1))-7*sin(x(1)+x(2)) -7*sin(x(1)+x(2));5*cos(x(1))+7*cos(x(1)+x(2)) 7*cos(x(1)+x(2))];
end
end
%%ROBOTARMPT2.MLX
clear all
clc
disp('This function finds the joint angles for the points between (10,2) and (3,8))')
l1 = 5; % length of first arm
l2 = 6; % length of second arm
fprintf('The lengths of the robot arms are %2.1f and %2.1f', l1,l2)
[x, f, ea, iter] = newtmult(@robot,[.7;.7]);
fprintf('The joint angles are %4.2f and %4.2f radians', x(1), x(2))
angleInDegrees = rad2deg(x(1));
angleInDegrees2 = rad2deg(x(2));
fprintf('The joint angles are %4.2f and %4.2f degrees', angleInDegrees, angleInDegrees2)
%Checking if the joint angles will give the correct value for the
%endpoint
F = [l1*cos(x(1))+l2*cos(x(1)+x(2)); l1*sin(x(1))+l2*sin(x(1)+x(2))]
0 Comments
Answers (1)
Walter Roberson
on 23 Mar 2021
function [J, F] = robot2(x)
l1 = 5; % length of first arm
l2 = 6; % length of second arm
xval = linspace(3, 10, 20);
slope = (8-2)/(3-10);
yval = slope*xval ;
F = [l1*cos(x(1))+l2*cos(x(1)+x(2))-xval; l1*sin(x(1))+l2*sin(x(1)+x(2))-yval]; % compute x coordinates; % compute y coordinates
J = [-5*sin(x(1))-7*sin(x(1)+x(2)) -7*sin(x(1)+x(2));5*cos(x(1))+7*cos(x(1)+x(2)) 7*cos(x(1)+x(2))];
end
F will be a 2 x 20 array.
J will be a 2 x 2 array.
You talk about the joint angle at each xval and yval, but your J is independent of xval and yval, so it is not clear what you want there, unless you want to make a 2 x 2 x 20 array where all of the third dimension copies are the same as the first slice ?
3 Comments
Walter Roberson
on 23 Mar 2021
And??
What sizes are you expecting robot2() to return for J and f when given a particular x pair?
Is J intended to be a jacobian? As you have 2 inputs then it would be most normal for the Jacobian to return 2 x 2 (as robot2 is coded now), and because of the "x = x - dx" it follows that J\f must return something of length 2. If J is 2 x 2 and f is 2 x N then J\f is going to return 2 x N . As you need something of length 2, it follows that f must be 2 x 1. So you need to somehow combine the xval = linspace(3, 10, 20); results
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!