data driven function approximation

58
Data Driven Function Approximation

Upload: lobo

Post on 19-Jan-2016

35 views

Category:

Documents


2 download

DESCRIPTION

Data Driven Function Approximation. One link. Given X t what is the joint angle P. Y. x t. P. x. Plot1d. Plot a function that maps joint angle P to tool position X t. clear all fstr=input('input a function: x.^2+cos(x) :','s'); fx=inline(fstr); range=2*pi; x=linspace(-range,range); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Driven Function Approximation

Data Driven Function Approximation

Page 2: Data Driven Function Approximation

One link

• Given Xt what is the joint angle P

x

Yxt

P

solutionposition forward

)cos(Plxt

Page 3: Data Driven Function Approximation

Plot1d

clear allfstr=input('input a function: x.^2+cos(x) :','s');fx=inline(fstr);range=2*pi;x=linspace(-range,range);y=fx(x);max_y=max(abs(y));plot(x,y/max_y);hold on;

Plot a function that maps joint angle P to tool position Xt

Page 4: Data Driven Function Approximation
Page 5: Data Driven Function Approximation

Black box

Black boxP Xt

range=2*pi;linspace(-range,range);

observation

N=input('keyin sample size:');x=rand(1,N)*2*range-range;n=rand(1,N)*0.1-0.05;y=fx(x)/max_y+n;figureplot(x,y,'.');

Page 6: Data Driven Function Approximation

Paired data

Page 7: Data Driven Function Approximation

Adaptive function

Input );( ixf

Desired output

Network output

error

Page 8: Data Driven Function Approximation

Network function

• Weight sum of hyper-tangent functions

}{}{}{

)tanh();( 01

mmm

M

mmmmi

rba

rbxarxf

Page 9: Data Driven Function Approximation

Network

}{}{}{

)tanh();( 01

mmm

M

mmmmi

rba

rbxarxf

tanh

tanh

tanh

…….x

1

a2

a1

aM

b2

b1

bM

1

r1

r2

rM

rM+1

Page 10: Data Driven Function Approximation

Procedure 1: MLP evaluation

1. Input r,a,b and x,M

2. y=r(M+1)

3. Set M to the length of a

4. For m=1:Ma. Add r(i)*tanh(x*a(i)+b(i)) to y

5. Return y

Page 11: Data Driven Function Approximation

Matlab code

function y=eval_MLP(x,r,a,b,M) y=r(M+1); for m=1:M y=y+r(m)*tanh(x.*a(m)+b(m)); endreturn

eval_MLP.m

Page 12: Data Driven Function Approximation

Mean square error

Given Find to minimize

,...,1 ),,( niyx ii

));((1

)( 2

1

n

iii xfy

nE

Page 13: Data Driven Function Approximation

Procedure 2: Mean square error

1. Input x,y,a,b,r2. Set E to zero3. Set n to the length of x4. For i=1:n

a) Calculate the square error of approximating y(i) by f(x;r,a,b)

b) Add the error to E

5. Return E

Page 14: Data Driven Function Approximation

Matlab code

function E=mean_square_error2(x,y,a,b,r)E=0;M=length(a);n=length(x);for i=1:n s_err=(y(i)-eval_MLP(x(i),r,a,b,M)).^2; E=E+s_err;endE=E/n;return

mean_square_error2.m

Page 15: Data Driven Function Approximation

Matlab code1 eval_MLP.m Evaluate an MLP function

2 mean_square_error2.m

Calculate E

3 learn_MLP.m Seek parameters

Page 16: Data Driven Function Approximation

Application

Black boxplot1d.m

P Xt

observation

Create paired datasampling.m

Input );( ixf

Desired output

Network output

error

Leaninglearning.m

Page 17: Data Driven Function Approximation

Matlab code

Function approximation for an arbitrary target functionfa1d.m

Page 18: Data Driven Function Approximation

x

Yxt

Pl

xP

l

x(P)

t

t

arccos

cos

Inverse kinematics

Page 19: Data Driven Function Approximation

Construction of inverse kinematics

• Procedure1. Input a 1d forward kinematics

2. Sampling to form paired data

3. Input-output swapping

4. Rescaling

5. Learning

Page 20: Data Driven Function Approximation

Black boxplot1d.m

P Xt

observation

Create paired datasampling.m

Data preparation

-4 -3 -2 -1 0 1 2 3 4-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-4 -3 -2 -1 0 1 2 3 4-1.5

-1

-0.5

0

0.5

1

1.5

temp=x;x=y;y=temp;

swapping

Rescaling

max_y=max(y);y=y/max_y;plot(x,y,'.')

-1.5 -1 -0.5 0 0.5 1 1.5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 21: Data Driven Function Approximation

Learning inverse kinematics

Input );( ixf

Desired output

Network output

error

Leaninglearning.m

-1.5 -1 -0.5 0 0.5 1 1.5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 22: Data Driven Function Approximation

Matlab code

fa1d_inv.m

MSE for training data 0.336461ME for training data 0.490247

Unacceptable training errors

Page 23: Data Driven Function Approximation

-1.5 -1 -0.5 0 0.5 1 1.5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Multiple outputs

• Inverse kinematics• Conflicts of I/O

relation• Two distinct

outcomes for the same input

Page 24: Data Driven Function Approximation

First order derivative

• Forward kinematics should be characterized by two outputs for construction of inverse kinematics

Black boxplot1d.m

P

Xt

Two observations

Derivative

Page 25: Data Driven Function Approximation

Symbolic differentiation

function demo_diff()% input a string to specify a function% plot its derivativess=input('function of x:','s');fx=inline(ss);x=sym('x');ss=['diff(' ss ')'];ss1=eval([sprintf(ss)]);fx1=inline(ss1)x=linspace(-pi,pi);plot(x,fx(x),'b');hold on;plot(x,fx1(x),'r');return

demo_diff.m

Page 26: Data Driven Function Approximation

Data preparation

Black boxplot1dd.m

P

Xt

Two observations

Derivative

Dt

Page 27: Data Driven Function Approximation

Reverse kinematics

Reverse KinematicsP

Xt

Derivative

Dt

plot3(y1,y2,ix)

Page 28: Data Driven Function Approximation

Reverse kinematics

Page 29: Data Driven Function Approximation

Matlab code

Page 30: Data Driven Function Approximation

Segmentation

• Procedure1. Input paired data,(x,y)

2. Sort data by y

Page 31: Data Driven Function Approximation
Page 32: Data Driven Function Approximation

• Write a matlab function to divide paired data to four segments

Page 33: Data Driven Function Approximation

Matlab code

Page 34: Data Driven Function Approximation

multiple solutions

l

xP

l

x(P)

Plx

t

t

t

arccos

cos

solutionposition forward

)cos(

o

tx

l

45

71arccos0.70P

feet 7071.0

foot one :

Page 35: Data Driven Function Approximation

Two links

x

Y

(xt yt)

P1

P2

1l

2l

Page 36: Data Driven Function Approximation

Forward kinematics of two-link robot

)sin()sin(

)cos()cos(

2211

2211

PlPly

PlPlx

t

t

21,PP

tt yx , tt yx ,

Page 37: Data Driven Function Approximation

Inverse kinematics of two-link robot

222tt yxB

)atan2(1t

t

y

xq

211

1

22

221

2 )2

acos(

qqP

Bl

lBlq

)2

acos(21

222

21

ll

Bll

)(12 PP

21,PP

tt yx ,

Page 38: Data Driven Function Approximation
Page 39: Data Driven Function Approximation

Reconstruction of forward Kinematics

Page 40: Data Driven Function Approximation

Learning forward kinematics

learn_MLP.meval_MLP2.m

fa2d.m

Page 41: Data Driven Function Approximation

Forward Kinematics for planar robot

)sin()sin()sin(

)cos()cos()cos(

332211

332211

PlPlPly

PlPlPlx

t

t

2P

1P

3P

1l

3l ),( tt yx

Page 42: Data Driven Function Approximation

223

212

11

180

180

1802

PP

PP

P

)sin()sin()sin(

)cos()cos()cos(

332211

332211

PlPlPly

PlPlPlx

t

t

t

t

yPlPlPlF

xPlPlPlF

)sin()sin()sin(),(

)cos()cos()cos(),(

332211212

332211211

Page 43: Data Driven Function Approximation

Inverse kinematics

At the position level, the problem is stated as, "Given the desired position of the robot's hand, what must be the angles at all of the robots joints? "

Page 44: Data Driven Function Approximation

Plot

Page 45: Data Driven Function Approximation

Exercise

• Write matlab functions to implement forward and inverse kinematics of a two-link robot

Page 46: Data Driven Function Approximation

Example

l1=1;l2=1;p1=pi/3;p2=pi/5;[x,y]=fkin(p1,p2,l1,l2)[p1,p2]=inverse_kin(x,y,l1,l2);p1/pip2/pi

Page 47: Data Driven Function Approximation

Nonlinear system

0

04

21

2

2

2

1

xx

xx

Page 48: Data Driven Function Approximation

A set of nonlinear functions

0),(

04),(

21212

2

2

2

1211

xxxxf

xxxxf

Page 49: Data Driven Function Approximation

x=linspace(-2,2);plot(x,x); hold on; plot(x,sqrt(4-x.^2))plot(x,-sqrt(4-x.^2))

Page 50: Data Driven Function Approximation

function F = myfun(x) F(1) = x(1).^2 + x(2)^2-4; F(2) = x(1) - x(2);return

Page 51: Data Driven Function Approximation

Least square of nonlinear functions

i

ixxxxf 2

21,),(min

21

Page 52: Data Driven Function Approximation

function demo_lsq()x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)y=myfun(x);sum(y.^2)returnfunction F = myfun(x) F(1) = x(1).^2 + x(2)^2 -4; F(2) = x(1) - x(2);return

Demo_lsq

Page 53: Data Driven Function Approximation

012),(

0242),(22

212

2

2

2

1211

21

xxxxf

xxxxf

Page 54: Data Driven Function Approximation

x=linspace(-5,5);plot(x,sqrt(24-2*x.^2),'r');hold on; plot(x,-sqrt(24-2*x.^2),'r')plot(x,sqrt(12+x.^2),'b')plot(x,-sqrt(12+x.^2),'b')

Page 55: Data Driven Function Approximation

Demo_lsq_2c

source code

Page 56: Data Driven Function Approximation

Nonlinear systems

)sin()sin(

)cos()cos(

2211

2211

PlPly

PlPlx

t

t

t

t

yPlPlPPF

xPlPlPPF

)sin()sin(),(

)cos()cos(),(

2211212

2211211

Page 57: Data Driven Function Approximation

)sin()sin(2

)cos()cos(2

21

21

PP

PP

2)sin()sin(),(

2)cos()cos(),(

21212

21211

PPPPF

PPPPF

Page 58: Data Driven Function Approximation

Exercise

• Write a malab function to solve the following nonlinear system

2)sin()sin(),(

2)cos()cos(),(

21212

21211

PPPPF

PPPPF