Tag Archives: code

Matlab Code for Numerical Hessian Matrix

Matlab code for numerical hessian matrix

In this post, I am sharing a Malab code calculating a numerical hessian matrix. 

You can download here.

You can use NumHessian.m  with this syntax.

function hf = NumHessian(f,x0,varargin)

You can understand how to use simply by reading these two simple examples.

Example 1

>> NumHessian(@cos,0)

ans =

-1.0000

Example 2

function y=test_func(x,a)
y=ax(1)x(2)*x(3);

>> NumHessian(@test_func,[1 2 3]’,2)

ans =

0 6.0000 4.0000
5.9999 -0.0001 1.9998
3.9999 2.0000 0

I hope this Matlab code for numerical Hessian matrix helps your projects

This Matlab code is based on another Matlab function, NumJacob, which calculates a numerical Jacobian matrix. If you are interested in this, visit here.

If you want to know the theory on Hessian matrix, please read this Wiki.

I acknowledge that this code is originally made by Parviz Khavari, a visitor of this blog, and I modified to post here.

 

Vertex angle from three points, Matlab code

I am sharing a Matlab code to get a vertex angle from three points.

This is the code, and you can download this one from here <Download>

 

function [ang] = get_vertex_ang_from_three_points(p0,p1,p2)

% get the vertex angle at p0 from three points p0,p1,p2
% p0, p1,p2 are two dim vector
%
%%% example
% p0=[0;0];p1=[1;0];p2=[0;1];
% get_vertex_ang_from_three_points(p0,p1,p2)
% answer = 1.57 (45 deg)
%%%

v1 = p1-p0;
v2 = p2-p0;
ang = acos(v1′v2/(norm(v1)norm(v2)));

It is very easy to use. you can get the vertex angle from three points, It is written in Matlab code

Good luck~~

 

MATLAB code for Numerical calculation of Jacobian matrix for a complex number

In this post, I share a MATLAB code for numerical calculation of Jacobian matrix for a complex number.

Download

This code was developed by Sithan Kanna, based on my code. If you are just interested in Jacobian of a real number, please see this post.

http://youngmok.com/numerical-jacobian-matrix-matlab/

I really thank to Sithan Kanna for sharing this nice code. If you have a question on this code please send an email to sithankanna a-t gmail d-o-t com

Download

 

This is a sample result of the code

>> h=@(x)[x(1) ; conj(x(1))]; % nonlinear equation

conjug = 0;
real = 0;
x_test = [0.5*1j];
H = NumJacob(h, x_test, conjug, real)

H =

1.0000
-0.0000

 

Have a nice day, and thank you for visiting my blog.

-Mok-

 

—————————————————————————————————

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————

 

Minimal c++ class for Maxon EPOS2

Minimal c++ class for Maxon EPOS2

I am sharing the minimal C++ class for Maxon EPOS2. It includes a basic “initialization”, “Move”, “Read position”, “Close device” functions. The program consists of a class called “cmaxonmotor”.  You can download the class and demo program here

<Download>

You can see a simple demo program here.

#include <stdio.h>
#include <iostream>
#include "cmaxonmotor.h"

using namespace std;

int main(int argc, char *argv[])
{
    CMaxonMotor motor("USB0",1);
    motor.initializeDevice(); // initialize EPOS2

    long TargetPosition = -200000;
    int CurrentPosition = 0;

    motor.Move(TargetPosition); // move to the target position

    cout << "Press <Enter> to stop and quit..." << endl;
    getchar();
    motor.GetCurrentPosition(CurrentPosition); // get the current position
    cout << "Current Position: " << CurrentPosition << endl;

    motor.closeDevice(); // close EPOS2

    return 0;
}

 

You can control one motor just via USB, and also able to control multiple motors via USB-CAN gateway. For this version, see this article.

I hope this helps your project.

 

 

-Mok-

Gaussian Kernel Bandwidth Optimization with Matlab Code

Gaussian Kernel Bandwidth Optimization with Matlab Code

In this article, I write on “Optimization of Gaussian Kernel Bandwidth” with Matlab Code.

First, I will briefly explain a methodology to optimize bandwidth values of Gaussian Kernel for regression problems. In other words, I will explain about “Cross validation Method.”

Then, I will share my Matlab code which optimizes the bandwidths of Gaussian Kernel for Gaussian Kernel Regression. For the theory and source code of the regression, read my previous posts <link for 1D input>, <link for multidimensional input>. This Matlab code can optimize bandwidths for multidimensional inputs. If you know the theory of cross validation, or if you don’t need to know the algorithm of my program, just download the zip file from the below link, then execute demo programs. Probably, you can use the program without big difficulties.

<Download>

1. Bandwidth optimization by a cross validation method

The most common way to optimize a regression parameter is to use a cross validation method. If you want to know about the cross validation deeply, I want to recommend to read this article. Here I will shortly explain about the cross validation method that I am using. This is just a way of cross validation.

1. Randomly sample 75% of the data set, and put into the training data set, and put the remaining part into the test set.

2. Using the training data set, build a regression model. Based on the model, predict the outputs of the test set.

3. Compare between the predicted output, and the actual output. Then, find the best model (best bandwidth) to minimize the gap (e.g, RMSE) between the predicted and actual outputs.

2. Matlab code for the algorithm

You can download all functions and demo programs from the below link.

<Download>

This program is for multidimensional inputs (of course, 1D is also OK). The most important function is Opt_Hyp_Gauss_Ker_Reg( h0,x,y ) and it requires Matlab optimization toolbox. I am attaching two demo programs and their results. I made these demo programs as much as I can. So, I believe that everybody can understand. 

<Demo 1D>

demo_1d

demo_1d_result

<Demo 2D>

demo_2d demo_2d_result

 

I wish this post can save your time and efforts in your work. If you have any question, please leave a reply.

-Mok-

—————————————————————————————————

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————

 

 

Gaussian Kernel Regression for Multidimensional Feature with Matlab code

Gaussian Kernel Regression for Multidimensional Feature with Matlab code (Gaussian Kernel or RBF Smoother)

I am sharing a Matlab code for Gaussian Kernel Regression algorithm for multidimensional input (feature).

In the previous post (link), I posted a theory of Gaussian Kernel Regression, and shared a Matlab code for one dimensional input. If you want to know about the theory, read the previous post. In the previous post, many visitors asked me for a multidimensional input version. Finally I made a Gaussian Kernel Regression Program for a general dimensional input

You can download the program from this link.

<Download>

I wrote a demo program to show how to use the code as easy as possible.

The below is the demo program, and a demo result plot. In this demo program, the dimension of input is 2 because of visualization, but it is expendable to an arbitrary dimension.

 

demo_codedemo_result

 

For the optimization of kernel bandwidth, see my other article <Link>.

 

I wish this program can save your time and effort for your work.

If you have any question, please leave a reply.

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

 

 

 

Gaussian kernel regression with Matlab code (Gaussian Kernel or RBF Smoother)

Gaussian kernel regression with Matlab code

In this article, I will explain Gaussian Kernel Regression (or Gaussian Kernel Smoother, or Gaussian Kernel-based linear regression, RBF kernel regression)  algorithm. Plus I will share my Matlab code for this algorithm. 

If you already know the theory. Just download  from here.  <Download>

You can see how to use this function from the below. It is super easy.

main figure

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

From here, I will explain the theory.

Basically, this algorithm is a kernel based linear smoother algorithm and just the kernel is the Gaussian kernel. With this smoothing method, we can find a nonlinear regression function.

The linear smoother is expressed with the below equation

y^* = \frac{\sum^N_{i=1}K(x^*,x_i)y_i}{\sum^N_{i=1}K(x^*,x_i)}

here x_i is the i_th training data input, y_i is the i_th training data output, K is a kernel function. x^* is a query point, y^* is the predicted output.

In this algorithm, we use the Gaussian Kernel which is expressed with the below equation. Another name of this functions is Radial Basis Function (RBF) because it is not exactly same with the Gaussian function.

K(x^*,x_i)=\exp\left(-\frac{ (x^*-x_i)^2}{2b^2}\right)

With these equation, we can smooth the training data outputs, thus we can find a regression function.

This program <Download> was made for one-dimensional inputs. If you need multi-dimension, please leave a reply, see this article. I recently made a new version for multidimensional input.

For the optimization of kernel bandwidth, see my other article <Link>.

 

Then good luck.

 

-Mok-

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

Simple C++ Code for Multi Dynamixel Servo Motor Control

Simple C++ Code for Multi Dynamixel Servo Motor Control

 

// this post is updated to eliminate the dependency on QT, 2015.06.26

 

In this post, I am sharing my C++ class to control multi Dynamixel servo motors.

In my case, I am using MX-24f, but most of dynamixel servo motors are using the same protocol. Thus, you can use this code directly. I am attaching all files (dynamixel library and others) to execute a demo program.

<Download>

If you don’t need common files, just use “multi_dnmx_motor.h” and “multi_dnmx_motor.cpp”. For the usage, see “main.cpp” file. It is super easy.  Demo program was made for Linux environment, but Windows uses still can use “multi_dnmx_motor” class

If you don’t know anything about Dynamixel program. Just follow the below.

 

0. Before following this instruction, check if the connection and settings are correct with “RoboPlus Manager” which can be downloaded in Robotis website, >> Support >> Download.

1. Go to “Dynamixel_src” folder.

2. “Make” : It will generate a library and automatically copy the lib. to the library folder.

3. Go to “Multi_Dynamixel_Motor_Control” folder.

4. Open “multi_dnmx_motor.h” file

5. Change “user setting”. You have to set the baud_rate, number of motors, and their motor IDs. Just change defines. (If you don’t know motors’ these values, use “RoboPlus”, a super easy program, and available in the Robotis website, http://support.robotis.com/en/ )

// User setting
#define BAUD_NUM 34 // 1: 1Mbps 34:57142bps
#define NUM_OF_MOTORS 4 // Number of motors

#define MOTOR_ID_1 1 // Motor 1 ID
#define MOTOR_ID_2 2 // Motor 2 ID
#define MOTOR_ID_3 3 // Motor 3 ID
#define MOTOR_ID_4 4 // Motor 4 ID

6.  compile it with this command

” g++ main.cpp multi_dnmx_motor.cpp -I../include -L../lib -ldxl  ”

7. Execute the program.

 

Good luck ^_^

<Download>

C++ code for reading unknown size matrix from text file

C++ code for reading unknown size matrix from text file

In this post, I upload my program {C++ code for reading unknown size matrix from text file } <download> which imports an arbitrary size matrix (or 2D array) from text (txt) file. This is c++ code.

The below is an execution result. The first figure is a text file including a matrix, the next figure is an execution screen. It’s very easy to use.

xtxt

text file to import. It has an arbitrary size matrix

screen

execution screen image, it imported the matrix from the above text fileSometimes, we want to import a matrix that we don’t know its size, especially when the file was generated from other programs.

I had googled for long time, and I realized that it is not easy to find. So, I made.

You don’t need to indicate the size of matrix, (cols and rows). It automatically imports and outputs, double type vector,  column and row size. It is very easy to use, and you can download the source code from here  <download>.

The only thing you should do is just download the source code, and import “x.txt” or “y.txt” which includes an arbitrary matrix.

Enjoy my program { C++ code for reading unknown size matrix from text file } Then good luck.

I referred partially this site.

http://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

Very good source code for serial communication in QT Linux envirionment

< Source code for serial communication in QT Linux envirionment>

The below site has pretty good source code for serial communication in Linux QT environment.

You can download and learn how to use it.

http://www.embedded4fun.com/serial-port-interfacing-in-qt/

 

The below is how to install the code.  ( from the upper site)

  • cd <any folder (preferrably to the QtSDK installation folder)>
  • git clone https://code.google.com/p/qextserialport/
  • cd qextserialport
  • qmake
  • make
  • make install

<download>