Thread: Matlab solutions? View Single Post
#3
05-17-2012, 06:02 PM
 elkka Invited Guest Join Date: Apr 2012 Posts: 57
Re: Matlab solutions?

kurts, I am no expert in programming, and I only started to learn Octave (I understand it is fully compatible with Matlab) less than two months ago. My programs are not optimal and not elegant. But here is my code, if you want it.

The functions:
Code:
```function f = GenerateF(a,b)
% a,b - to points in [-1,1]*[-1,1]
%f - linear function defined be these points, f = [x0,x1,x2]

f(1) = a(2)*b(1)-a(1)*b(2);
f(2) = b(2)-a(2);
f(3) = -b(1)+a(1);
end;

function X = GenerateX(f,n)
% f is the target linear function in X^2 form [a_0,a_1,a_2]
%X is an array (3*n) of point data, consisting of uniform independent r.v. in [-1,1]*[-1,1]
%first two colomns are the coordinates, third - classification
% in a hyperplane defined by f

X = unifrnd(-1,1,n,2);
X(:,3) = sign([ones(n,1),X]*f');
end;

function p = Difference(f,g,n)
% f, g - two linear functions in [-1,1]^2, in form [a_0,a_1,a_2]
% p = approximate P(sifference in classification b/n f and g)
% n - number of test points to approximate probability

X = unifrnd(-1,1,n,2);
X =[ones(n,1),X];
p = 0;
for i = 1:n
p = p+0.5*abs(sign(X(i,:)*f')-sign(X(i,:)*g'));
i = i+1;
end;
p = p/n;
end;

function [p, itNum] = perceptron(DataSet,p0)
% DataSet is a n * 3 array of data, where each row corresponds
%to a data point x, the first value being the first coordinate,
%the second being the second coordinate, and the third being the %classification
% p0 = initial value

k=1;
n = length(DataSet(:,1));
X = [ones(n,1),DataSet];
m = length(X(1,:));
p = p0;
k=1;
i=1;

MaxDiff = 2;
while (MaxDiff>0)&(k<30000)
[Diff,ind] = sort(abs(sign(X*p')-X(:,m)));
[MaxDiff,iMax] = max(Diff);
if (MaxDiff>0)
i = ceil(unifrnd(iMax-1,n,1,1));
p = p+X(ind(i),m)*X(ind(i),:);
k = k+1;
%if (mod(k,10000)==0);k end;
end;
end;

itNum=k-1;
end;```
Body for homework 1
Code:
```a = unifrnd(-1,1,2,2);
%a = [0,0;1,1];
f = GenerateF(a(1,:),a(2,:))

n = 10000; % number of trials for the algorithm
m = 100; % number of points to run the algorithm on
k = 1000; % number of points to evaluate probability
Test = zeros(n,4);

%hw1 q6-10

for i = 1:n;
X = GenerateX(f,m);
[g ,Test(i,1)]= perceptron(X,[0,0,0]);
Test(i,2) = Difference(f,g,k);
i = i+1
end;
iterations = mean(Test(:,1))
diff = mean(Test(:,2))```