воскресенье, 26 марта 2017 г.

Legend Mean Value + Text Signs

R=5;C=1;
dat = rand(50,1);

subplot(R,C,1);
plot(dat)
m = mean(dat);
ax = gca;
xlimits = ax.XLim;
h = line([xlimits(1),xlimits(2)],[m,m],'Color','k','LineStyle','--');

legend(h,'mean of data');

subplot(R,C,2);

g1 = hggroup;
g2 = hggroup;
t = linspace(0,2*pi,100);
plot(t,sin(t),'b','Parent',g1)
hold on
plot(t,sin(t+1/7),'b','Parent',g1)
plot(t,sin(t+2/7),'b','Parent',g1)
plot(t,sin(t+3/7),'b','Parent',g1)
plot(t,cos(t),'g','Parent',g2)
plot(t,cos(t+1/7),'g','Parent',g2)
plot(t,cos(t+2/7),'g','Parent',g2)
plot(t,cos(t+3/7),'g','Parent',g2)
hold off % reset hold state to off

legend([g1,g2],'sine','cosine')

subplot(R,C,3);
x = linspace(0,2*pi,100);
y1 = sin(x);
p1 = plot(x,y1,'DisplayName','sin(x)');
hold on
y2 = sin(x) + pi/2;
p2 = plot(x,y2,'DisplayName','sin(x) + \pi/2');
y3 = sin(x) + pi;
p3 = plot(x,y3,'DisplayName','sin(x) + \pi');
hold off

legend([p1 p2 p3])


subplot(R,C,4);

t = linspace(0,2*pi,50);
y = sin(t);
plot(t,y);

x1 = pi;
y1 = sin(pi);
str1 = '\leftarrow sin(\pi) = 0';
text(x1,y1,str1);

x2 = 3*pi/4;
y2 = sin(3*pi/4);
str2 = '\leftarrow sin(3\pi/4) = 0.71';
text(x2,y2,str2)
x3 = 5*pi/4;
y3 = sin(5*pi/4);
str3 = 'sin(5\pi/4) = -0.71 \rightarrow';

text(x3,y3,str3,'HorizontalAlignment','right');

subplot(R,C,5);

x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
plot(x,y);

indexmin = find(min(y) == y);
xmin = x(indexmin);
ymin = y(indexmin);
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);

strmin = ['Minimum = ',num2str(ymin)];
text(xmin,ymin,strmin,'HorizontalAlignment','left');
strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');



Graph with Two y-Axes

1.
R = 5; C = 1;

subplot(R,C,1);
x = linspace(0,2*pi,25);

y1 = sin(x);
% y2 = 0.5*sin(x);
y2 = exp(-1/3*x).*sin(x);
plot(x,y1);
grid on

hold on

stem(x,y2);

hold off

subplot(R,C,2);

A = 1000;
a = 0.005;
b = 0.005;
t = 0:900;
z1 = A*exp(-a*t);
z2 = sin(b*t);

[ax,p1,p2] = plotyy(t,z1,t,z2,'semilogy','plot');
ylabel(ax(1),'Semilog Plot') % label left y-axis
ylabel(ax(2),'Linear Plot') % label right y-axis
xlabel(ax(2),'Time') % label x-axis

p1.LineStyle = '--';
p1.LineWidth = 2;
p2.LineWidth = 2;

grid(ax(1),'on')

пятница, 24 марта 2017 г.

Call .Net Methods from ML

1.
R = 3;
C=1;
asmpath = 'D:\VC\1305\gs.trade\GS.Matlab\bin\Debug\';
asmname = 'GS.Matlab.dll';

asm = NET.addAssembly(fullfile(asmpath,asmname));

obj = GS.Matlab.MyGraph;

mlData = cell(obj.getNewData);
%objArr = cell(obj.getObjectArray);
objNewDataArr = obj.getNewDataProp;

figure('Name',char(mlData{1}));
subplot(R,C,1);
% figure('Name',char(mlData{1}));
plot(double(mlData{2}(2)));
xlabel(char(mlData{2}(1)));

subplot(R,C,2);
objArr = obj.getObjectArray();

doubles1 = double(objArr(1));
doubles2 = double(objArr(2));

plot([doubles1 doubles2]);

subplot(R,C,3);
objDouble = obj.getDoubleArray;
doubles = double(objDouble); 
plot(doubles);

webread matlab and Net classes

webRead

https://www.mathworks.com/help/matlab/ref/webread.html

Convert .NET Arrays to Cell Arrays

https://www.mathworks.com/help/matlab/matlab_external/net-arrays-to-cell-arrays.html

Pass Cell Arrays of .NET Data

https://www.mathworks.com/help/matlab/matlab_external/tips-for-working-with-cell-arrays-of-net-data.html

Handle Data Returned from .NET Objects

https://www.mathworks.com/help/matlab/matlab_external/handling-net-data-in-matlab_bte9owt-1.html#bte9paq-1

Access a Simple .NET Class

https://www.mathworks.com/help/matlab/matlab_external/access-a-simple-net-class.html

Calling .NET Methods

matlab_external/calling-net-methods.html?s_tid=gn_loc_drop

Use .NET methods in MATLAB®, method signatures, arguments by reference, optional arguments

https://www.mathworks.com/help/matlab/methods-.html


понедельник, 27 февраля 2017 г.

MWArray Example

Multistep Neural Network Prediction example

Multistep Neural Network Prediction

https://www.mathworks.com/help/nnet/ug/multistep-neural-network-prediction.html


% Multistep Neural Network Prediction

% Set Up in Open-Loop Mode

[X,T] = maglev_dataset;
net = narxnet(1:2,1:2,10);
[x,xi,ai,t] = preparets(net,X,{},T);
net = train(net,x,t,xi,ai);
y = net(x,xi,ai);
view(net)

% Multistep Closed-Loop Prediction From Initial Conditions

% Close loop
netc = closeloop(net);
view(netc);

[x,xi,ai,t] = preparets(netc,X,{},T);
yc = netc(x,xi,ai);

plot(1:3999, cell2mat(t), 'b', 1: 3999, cell2mat(yc),'r');

% Multistep Closed-Loop Prediction Following Known Sequence

x1 = x(1:20);
t1 = t(1:20);
x2 = x(21:40);

% The open-loop neural network is then simulated on this data.

[x,xi,ai,t] = preparets(net,x1,{},t1);
[y1,xf,af] = net(x,xi,ai);

%Now the final input and layer states returned by the network are converted to closed-loop form along with the network.
% The final input states xf and layer states af of the open-loop network become the initial input states xi and layer states ai of the closed-loop network.

[netc,xi,ai] = closeloop(net,xf,af);

%Typically use preparets to define initial input and layer states. 
% Since these have already been obtained from the end of the open-loop simulation,
% you do not need preparets to continue with the 20 step predictions of the closed-loop network

[y2,xf,af] = netc(x2,xi,ai);

% Note that you can set x2 to different sequences of inputs to test different scenarios for however many time steps you would like to make predictions.
% For example, to predict the magnetic levitation system's behavior if 10 random inputs are used:

x2 = num2cell(rand(1,10));
[y2,xf,af] = netc(x2,xi,ai);

% Following Closed-Loop Simulation with Open-Loop Simulation
[~,xi,ai] = openloop(netc,xf,af);

% Now you can define continuations of the external input and open-loop
% feedback, and simulate the open-loop network 

x3 = num2cell(rand(2,10));
y3 = net(x3,xi,ai);

Data Generator function

%% Data generator function
function [X,Xtrain,Ytrain,fig] = data_generator()
% data generator
X = 0.01:.01:10;
f = abs(besselj(2,X*7).*asind(X/2) + (X.^1.95)) + 2;
fig = figure;
plot(X,f,'b-')
hold on
grid on
% available data points
Ytrain = f + 5*(rand(1,length(f))-.5);
Xtrain = X([181:450 601:830]);
Ytrain = Ytrain([181:450 601:830]);
plot(Xtrain,Ytrain,'kx')
xlabel('x')
ylabel('y')
ylim([0 100])
legend('original function','available data','location','northwest')