Code covered by the BSD License

# Files for the 2012 Webinar "Tips and Tricks - Getting Started Using Optimization with MATLAB"

### Seth DeLand (view profile)

These are the files that were used for the demonstrations in the webinar.

ThOptimPlotCircuit(options,state,flag,Tdata,VData,Res,ThVal,ThBeta)
```function [state,options,optchanged] = ThOptimPlotCircuit(options,state,flag,Tdata,VData,Res,ThVal,ThBeta)
%THOPTIMPLOT plots the best temperature curve at each iteration, and
%displays the current best values for the components.

% Copyright (c) 2012, The MathWorks, Inc.

persistent ann

optchanged = false;

switch flag
case 'init'
figure('Position',[27         542        1200         520]);

% Left Plot
subplot(1,2,1);
set(gca,'Tag','ax1');
set(gca,'YLim',[0.1 0.2]);
grid on;
hold on;
plot(Tdata,VData,'-*b');
xlabel('Temperature (^oC)','FontSize',12,'FontWeight','bold');
ylabel('Voltage at Point B (V)','FontSize',12,'FontWeight','bold');
title('Thermistor Network Temperature Curve','FontSize',14,'FontWeight','bold');
[~,loc] = min(state.Score); % Find location of best
bestV = voltageCurve(Tdata,state.Population(loc,:),Res,ThVal,ThBeta);
plotBest = plot(Tdata,bestV,'-or');
set(plotBest,'Tag','bestVLine'); % Update voltage curve
lgnd = legend('Ideal Curve','GA Solution','Location','southeast');
set(lgnd,'FontSize',12);

% Right Plot
subplot(1,2,2);
imshow('circuitDrawing.PNG');
title('Circuit Diagram','FontSize',14,'FontWeight','bold');

ann.txt.r1 = annotation('textbox',...
[0.6043 0.7153 0.0506 0.0788],'String',{'R1','300\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.r2 = annotation('textbox',...
[0.6593 0.5511 0.0506 0.07884],'String',{'R2','300\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.r3 = annotation('textbox',...
[0.6043 0.3530 0.0506 0.07884],'String',{'R3','300\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.r4 = annotation('textbox',...
[0.6593 0.2107 0.0506 0.0788],'String',{'R4','300\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.rn1 = annotation('textbox',...
[0.7901 0.7130 0.0506 0.0788],'String',{'RN1','50\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.b1 = annotation('textbox',...
[0.8385 0.7123 0.0506 0.0788],'String',{'B1','2750'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.rn2 = annotation('textbox',...
[0.7901 0.3523 0.0506 0.0788],'String',{'RN2','50\Omega'},...
'FitBoxToText','off','LineStyle','none');

ann.txt.b2 = annotation('textbox',...
[0.8385 0.3519 0.0506 0.0788],'String',{'B2','2750'},...
'FitBoxToText','off','LineStyle','none');

drawnow;

pause;

case 'iter'
% Left Plot
[~,loc] = min(state.Score); % Find location of best
bestV = voltageCurve(Tdata,state.Population(loc,:),Res,ThVal,ThBeta);
ax1 = findobj(get(gcf,'Children'),'Tag','ax1');
plotBest = findobj(get(ax1,'Children'),'Tag','bestVLine');
set(plotBest, 'Ydata', bestV); % Update voltage curve

% Right Plot
bX = state.Population(loc,:);

% New Values
ann.newval.r1 = Res(bX(1)); ann.newval.r2 = Res(bX(2));
ann.newval.r3 = Res(bX(3)); ann.newval.r4 = Res(bX(4));
ann.newval.rn1 = ThVal(bX(5)); ann.newval.b1 = ThBeta(bX(5));
ann.newval.rn2 = ThVal(bX(6)); ann.newval.b2 = ThBeta(bX(6));

fnms = fieldnames(ann.txt);

% Initialize haschanged field (will be used for checking if value
% has changed)
for ii = 1:length(fnms)
ann.haschanged.(fnms{ii}) = 0;
end

if isfield(ann,'oldval') % Check to see if resistor value has changed
for ii = 1:length(fnms)
if all(isequal(get(ann.txt.(fnms{ii}),'Color'),[1 0 0])) % Change color back to black
set(ann.txt.(fnms{ii}),'Color','k');
end
if ann.newval.(fnms{ii})~=ann.oldval.(fnms{ii}) % Value has changed, set color to red
ann.haschanged.(fnms{ii}) = 1;
set(ann.txt.(fnms{ii}),'Color','r');
end
end
end

% Old Values
ann.oldval.r1 = Res(bX(1)); ann.oldval.r2 = Res(bX(2));
ann.oldval.r3 = Res(bX(3)); ann.oldval.r4 = Res(bX(4));
ann.oldval.rn1 = ThVal(bX(5)); ann.oldval.b1 = ThBeta(bX(5));
ann.oldval.rn2 = ThVal(bX(6)); ann.oldval.b2 = ThBeta(bX(6));

% Change the value of the resistor on the plot if it has changed
if ann.haschanged.(fnms{1})
set(ann.txt.r1,'String',{'R1',[num2str(ann.oldval.r1) '\Omega']});
end
if ann.haschanged.(fnms{2})
set(ann.txt.r2,'String',{'R2',[num2str(ann.oldval.r2) '\Omega']});
end
if ann.haschanged.(fnms{3})
set(ann.txt.r3,'String',{'R3',[num2str(ann.oldval.r3) '\Omega']});
end
if ann.haschanged.(fnms{4})
set(ann.txt.r4,'String',{'R4',[num2str(ann.oldval.r4) '\Omega']});
end
if ann.haschanged.(fnms{5})
set(ann.txt.rn1,'String',{'RN1',[num2str(ann.oldval.rn1) '\Omega']});
end
if ann.haschanged.(fnms{6})
set(ann.txt.b1,'String',{'B1',num2str(ann.oldval.b1)});
end
if ann.haschanged.(fnms{7})
set(ann.txt.rn2,'String',{'RN2',[num2str(ann.oldval.rn2) '\Omega']});
end
if ann.haschanged.(fnms{8})
set(ann.txt.b2,'String',{'B2',num2str(ann.oldval.b2)});
end

% Update graphics
drawnow;

% If any resistors have changed, pause for 0.5 seconds
counter = 0;
for ii = 1:length(fnms)
counter = counter + ann.haschanged.(fnms{ii});
end
if counter
pause(0.5);
end

case 'done'
% Left Plot
[~,loc] = min(state.Score);
xOpt = state.Population(loc,:);
s{1} = sprintf('Optimal solution found by Mixed Integer GA solver: \n');
s{2} = sprintf('R1 = %6.0f ohms \n', Res(xOpt(1)));
s{3} = sprintf('R2 = %6.0f ohms \n', Res(xOpt(2)));
s{4} = sprintf('R3 = %6.0f ohms \n', Res(xOpt(3)));
s{5} = sprintf('R4 = %6.0f ohms \n', Res(xOpt(4)));
s{6} = sprintf('TH1 = %6.0f ohms, %6.0f beta \n', ...
ThVal(xOpt(5)), ThBeta(xOpt(5)));
s{7} = sprintf('TH2 = %6.0f ohms, %6.0f beta \n', ...
ThVal(xOpt(6)), ThBeta(xOpt(6)));

% Display the text in "s" in an annotation object on the Genetic Algorithm
% figure.  The four-element vector is used to specify the location.
annotation(gcf, 'textbox', [0.15 0.42 0.22 0.48], 'String', s,...
'BackGroundColor','w','FontSize',10);
hold off;

% Right Plot
bX = state.Population(loc,:);
R1 = Res(bX(1)); R2 = Res(bX(2)); R3 = Res(bX(3)); R4 = Res(bX(4));
RN1 = ThVal(bX(5)); B1 = ThBeta(bX(5));
RN2 = ThVal(bX(6)); B2 = ThBeta(bX(6));

% Display final values
set(ann.txt.r1,'String',{'R1',[num2str(R1) '\Omega']});
set(ann.txt.r2,'String',{'R2',[num2str(R2) '\Omega']});
set(ann.txt.r3,'String',{'R3',[num2str(R3) '\Omega']});
set(ann.txt.r4,'String',{'R4',[num2str(R4) '\Omega']});
set(ann.txt.rn1,'String',{'RN1',[num2str(RN1) '\Omega']});
set(ann.txt.b1,'String',{'B1',num2str(B1)});
set(ann.txt.rn2,'String',{'RN2',[num2str(RN2) '\Omega']});
set(ann.txt.b2,'String',{'B2',num2str(B2)});
end
```