Code covered by the BSD License

# Using Analytical Tools to Gain Insight and Speed-up Num. Analysis in MATLAB & Symbolic Math Toolbox

### Deepak Ramaswamy (view profile)

28 Jan 2013 (Updated )

files from the webinar

MLInverseKinematics.m
```%% Define and solve 2 link inverse kinematics equations in MATLAB
% Define variables
syms rho1 rho2 theta1 theta2 xd yd t1 t2 real;
assume(rho1 > 0);
assume(rho2 > 0);
assume(t1 <= 1);
assume(t2 <= 1);
% Define equations
eq1 = rho1*t1 + rho2*t2 - xd;
eq2 = rho1*sqrt(1-t1^2) + rho2*sqrt(1-t2^2) - yd;
display equation 1
pretty(eq1)
display equation 2
pretty(eq2)
% Solve the equations
sol = solve([eq1==0,eq2==0],t1,t2,'IgnoreAnalyticConstraints',true,'Real',true);
sol.t1
sol.t2

% Set up some values and perform a very crude animation
rho1Val = 0.8; rho2Val = 0.7;
for k=0:20
targetParam = k/20*pi/2;
xdVal = 0.5*cos(targetParam);
if targetParam < pi/3
ydVal = 0.5*sin(targetParam);
else
ydVal = 0.6*sin(pi/3)+1/15*(targetParam-pi/3);
end
% Substitute using subs() to get numerical values
t1Val = subs(sol.t1,[xd,yd,rho1,rho2],[xdVal,ydVal,rho1Val,rho2Val]);
theta1 = acos(sol.t1);
theta1Val = acos(t1Val);
t2Val = subs(sol.t2,[xd,yd,rho1,rho2],[xdVal,ydVal,rho1Val,rho2Val]);
theta2Val = acos(t2Val) - theta1Val;
x1Val = rho1Val*cos(theta1Val);
y1Val = rho1Val*sin(theta1Val);
clf;plot([0, x1Val(1)],[0, y1Val(1)],'r-');hold on;
plot([x1Val(1), xdVal],[y1Val(1), ydVal],'k-');axis('equal');
xlim([-0.7,0.5]);ylim([-0.1,0.9]);
pause(0.1);
end
%
% Copyright 2012 The MathWorks, Inc.```