Sabtu, 06 Januari 2018

Main-main dengan AG


  Tdata      1x26              208  double             
  Vdata      1x26              208  double   

Tdata =

   -40   -35   -30   -25   -20   -15   -10    -5     0     5    10    15    20    25    30    35    40    45    50    55

    60    65    70    75    80    85

Vdata=
    0.1251    0.1203    0.1161    0.1124    0.1094    0.1068    0.1049    0.1034    0.1026    0.1023    0.1026    0.1034

    0.1049    0.1068    0.1093    0.1124    0.1161    0.1203    0.1251    0.1304    0.1364    0.1428    0.1499    0.1574

    0.1656    0.1743



%% Temperature and Voltage Data
Tdata = -40:5:85;
Vdata = 1.026E-1 + -1.125E-4 * Tdata + 1.125E-5 * Tdata.^2;

%% Plot the Desired Curve
plot(Tdata,Vdata,'-*');
title('Target Curve','FontSize',12);
xlabel('Temperature (^oC)'); ylabel('Voltage (V)')


load('StandardComponentValues.mat')

  Res         70x1               560  double              
  ThBeta       9x1                72  double              
  ThVal        9x1                72  double              


ThBeta =
        2750
        3680
        3560
        3620
        3528
        3930
        3960
        4090
        3740

ThVal =
          50
         220
        1000
        2200
        3300
        4700
       10000
       22000
       33000

Res =
         300
         330
         360
         390
         430
         470
         510
         560
         620
         680
         750
         820
         910
        1000
        1100
        1200
        1300
        1500
        1600
        1800
        2000
        2200
        2400
        2700
        3000
        3300
        3600
        3900
        4300
        4700
        5100
        5600
        6200
        6800
        7500
        8200
        9100
       10000
       11000
       12000
       13000
       15000
       16000
       18000
       20000
       22000
       24000
       27000
       30000
       33000
       36000
       39000
       43000
       47000
       51000
       56000
       62000
       68000
       75000
       82000
       91000
      100000
      110000
      120000
      130000
      150000
      160000
      180000
      200000
      220000

Vnew = voltageCurve(Tdata, [2 2 2 2 1 1], Res, ThVal, ThBeta);
%% Add New Curve to Plot
hold on; plot(Tdata,Vnew,'-or');

Vnew =1x26  double
    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500    0.5500




%%
% We would like to find the optimal indices that result in a temperature
% curve closest to our desired curve.  Our initial guess of [2 2 2 2 1 1]
% did not yield a good fit, and there are 70*70*70*70*9*9 = 1944810000
% different possible combinations for this circuit.  We will use an
% optimization routine, the Genetic Algorithm, to find the optimal indices
% for our problem.  The Genetic Algorithm allows us to constrain values to
% be integers, which is necessary for this problem since indices into a
% vector must be integers.  

%% Bounds on our Vector of Indices
lb = [1 1 1 1 1 1];
ub = [70 70 70 70 9 9];

%% Constrain All 6 Variables to be Integers
intCon = 1:6;

custOutput = @(a1,a2,a3)ThOptimPlot(a1,a2,a3,Tdata,Vdata,Res,ThVal,ThBeta);


options = gaoptimset('CrossoverFrac',0.5,'PopulationSize',100,...
    'StallGen',125,'Generations',150,...
    'PlotFcns',@gaplotbestf,'OutputFcns',custOutput);

%% Run the Genetic Algorithm
% Note: The Genetic Algorithm is based on stochastic methods, meaning that
% we are not guaranteed to find the same solution every time.  To reproduce
% the exact results from the video, run "rng(45)" at the Command Line to 
% set the same seed for the random number generator.

[xOpt,fVal] = ga(@(x)objectiveFunction(x,Res,ThVal,ThBeta,Tdata,Vdata),...
    6,[],[],[],[],lb,ub,[],intCon,options);

%% Inspect the Solution Vector to see that All Values are Integers
disp('Integer Solution Returned by GA:')
disp(xOpt)