# Genetic Algorithm to Find desired parameters.

28 views (last 30 days)
Hi, I am trying to get matlab to use the randomly generated values( parameters) to create a 7 parents with each 6 Capacitor Voltage values corresponding to its amount at the time, and a 7th for a cost function. This is to repeat it 100 times, for the first generation of a 6 by 100 population (In less confusing terms we program matlab randomly chose some parameters to use until it produces something closs to satisfying the desired values)
Using the Ga Solver, repeat the creation of new children( which are the parameters I think) in the population until the cost function gets down to 0.001 or a very small value which indicates that its getting close to the desired values.
According to my proffesor, once the cost function gets close to zero is when the Genetic algorithm should stop which indicates the correct selection of parameters have been met.
He has not shown or shared any examples of how ga solvers would work, also, there is no assigned reading for this application so as a class we are completely lost. The only part that is randomly generated is the parameters. My code is a mess. Im not sure what to do to get it to work. The only part that plots is the desired values that need to be met at the specific charge and discharge times. Attached is my proffesor's psuedo code, but with noing available in our book, this was as far as I could get. I believe he did his dissertation on it so its mostlikely far beyond the scope of this undergrad computations class. Any help is greatly appreciated. The code he gave us for the charging and discharging also seems to be purposefully broken.
%% the 6 Desired values the optimization needs to reach
t=[0,2,4,6,8,10];
Vdesired=[0,3.27,5.79,7.7,6.64,5.09];
plot(t,Vdesired,'ro')
grid
xlabel('time (s)')
ylabel('voltage of capacitor')
title('RC Circuit')
hold on;
%% The code below is supposed to Generate the 7 new random resistor capacitor, supply voltage and time constant values to compute to get the outputs of A(1),Vc(0),Vc(2),Vc(4),Vc(6),Vc(8),Vc(10) for each generation to populate an entire 7 by 100 array. This should then be used in a ga solver, and be randomized until A is very small
for i=1:100
R = 20000 + (50000-20000)*rand(1);
C= 0.0001 +(0.001-0.0001)*rand(1);
Vs= 5 +(20-5)*rand(1);
tc= 2+(8-2)*rand(1);
%% 4 Euler's Charging and Discharging
dt=0.01;
t=0:dt:10;n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
%% EULER SOLUTION
% Charge
limit = round(tc/dt);
for i=2:limit+1
Vc(i)=Vc(i-1)+(eps/(R*C)-Vc(i-1)/(R*C))*dt;
end
%Discharge
for i=(limit+2):n(2)
Vc(i)=Vc(i-1)+(-Vc(i-1)/(R*C))*dt;
end
A(i)= (Vc@(t=2)-3.27)^2 + (Vc(@t=4)-5.79)^2 + (Vc(@t=6)-7.70)^2 + (Vc(@t=8)-6.64)^2 + (Vc(@t=10)-5.09)^2;
end

Alan Weiss on 11 Mar 2021
You might want to spend a little time reading up on the genetic algorithm:
And look at the functtion syntax along with some very simple examples:
I hope that these links answer most of your questions. There are also several more complex examples in Genetic Algorithm.
Alan Weiss
MATLAB mathematical toolbox documentation
Christopher Jarrett on 11 Mar 2021
Thanks for responding. Sorry for the confusing question I downloaded one of the examples and am going to try to model my code after it

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!