-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrundata.m
144 lines (115 loc) · 3.71 KB
/
rundata.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
clear all
clc
k=1;
maxsize=0;
% Generate input and output dataset
dmin=0.5;
dmax=2.0;
maxBoundaryNodes=0;
for i = dmin:0.5:dmax
% inp(k,1)=0;
% inp(k,2)=1;
% inp(k,3)=i;
% inp(k,4)=0;
% inp(k,5)=0;
% inp(k,6)=0;
% inp(k,7)=1;
% inp(k,8)=1;
% singleinput(k)=i;
% scale the input
% singleinput(k)=-1+2*(i-dmin)/(dmax-dmin);
[nodes elems msh gm nBNodes] = getMesh(i);
nodeIndexIncrement=1;
for nodeIndex=1:nBNodes
% inp(k,nodeIndexIncrement)=nodes(1,nodeIndex);
% inp(k,nodeIndexIncrement+1)=nodes(2,nodeIndex);
inp{k}(nodeIndexIncrement)= scaledata(nodes(1,nodeIndex),0,dmax);
inp{k}(nodeIndexIncrement+1)=scaledata(nodes(2,nodeIndex),0,dmax);
nodeIndexIncrement=nodeIndexIncrement+2;
end
%Determine max number of boundary nodes in all sets
maxBoundaryNodes=max(maxBoundaryNodes,nBNodes);
% Truncated outputs, i.e. points excluding Bpoints
zi=1;
for z = nBNodes+1:size(nodes,2)
%zi=2*z-1;
out{k}(zi) = nodes(1,z);
out{k}(zi+1) = nodes(2,z);
%el{k} = elems;
zi=zi+2;
end
% These are the full outputs- will be used later on if required
zi=1;
for z = 1:size(nodes,2)
%zi=2*z-1;
fullOut{k}(zi) = nodes(1,z);
fullOut{k}(zi+1) = nodes(2,z);
el{k} = elems;
zi=zi+2;
end
%out{k} = [nodes(1,:) nodes(2,:)];
%out(k,:)=out1;
if size(out{k},2) > maxsize
maxsize = size(out{k},2);
end
k=k+1;
end
kmax=k-1;
% Since there are variable number of inputs, I'm going to pad the exta
% inputs with zeros
newInput(1:kmax,1:maxBoundaryNodes*2)=0;
for i = 1:kmax
actualSize=size(inp{i},2);
if actualSize < 2*maxBoundaryNodes
newInput(i,1:actualSize)=inp{i};
newInput(i,actualSize+1:maxBoundaryNodes)=0;
else
newInput(i,:)=inp{i};
end
end
newOutput(1:kmax,1:maxsize)=0;
% Pad with zeros?
for i = 1:kmax
actualSize= size(out{i},2);
if actualSize < maxsize
nElementsToPad=maxsize-actualSize;
newOutput(i,1:actualSize)=out{i};
for k = 1:nElementsToPad/2
%for k = size(el{i},2):-1:size(el{i},2)-nElementsToPad/2
% Pick these vertices vTp
vTp = el{i}(:,k);
newOutput(i,actualSize+2*k-1)= (1/3)*( fullOut{i}(2*vTp(1)-1) +...
fullOut{i}(2*vTp(2)-1) +...
fullOut{i}(2*vTp(3)-1));
newOutput(i,actualSize+2*k) = (1/3)*( fullOut{i}(2*vTp(1)) +...
fullOut{i}(2*vTp(2)) +...
fullOut{i}(2*vTp(3)));
end
% newOutput(i,actualSize+1:end)=10*ones(1,nElementsToPad);
else
newOutput(i,:)=out{i};
end
end
% x = newOutput(3,1:2:end);
% y = newOutput(3,2:2:end);
% tri = delaunay(x,y);
% triplot(tri,x,y);
% axis equal
% Train data here (lower neurons seems better!)
net=feedforwardnet(20);
net.trainFcn='trainlm';
net.trainParam.goal=1e-9;
net.trainParam.epochs=1000;
net.trainParam.min_grad=1e-9;
net.trainParam.max_fail=50;
net.divideParam.trainRatio=1;
net.divideParam.valRatio=0;
net.divideParam.testRatio=0;
%net.performFcn='msereg'; %net.performParam.ratio=0.5;
%[net, tr]= train(net, singleinput, newOutput');
[net, tr]= train(net, newInput', newOutput');
%desiredInput=0.51;
%scaledDesiredInput=-1+2*(desiredInput-dmin)/(dmax-dmin)
%testnet(net,scaledDesiredInput)
desiredInput=1.35;
testnetboundary(net,maxBoundaryNodes,desiredInput);