Plotting multiple times in a function
7 views (last 30 days)
Show older comments
Hi , I amd new using matlab and I would want to know how can I plot every generation of child ( xh) and see how has improve each generation in each iteration of jj. Total of generations are 200.
Code is below.
clear all
close all
clc
f = @(x,y) x*exp(-x^2 -y^2);
Ge = 200; %generations
N = 50;
D = 2;
xl = [-2 ; -2];
xu = [2 ; 2];
aptitud = zeros(1,N);
x = zeros(2,N); %parents
for i=1:N
x(:,i) = xl+(xu - xl).*rand(2,1);
end
for jj=1:Ge
for i=1:N
fx = f(x(1,i),x(2,i));
if fx >= 0
aptitud(i) = 1/(1+fx);
else
aptitud(i) = 1+abs(fx);
end
end
xh = zeros(2,N); % children
for k=1:2:N
p1 = seleccion(aptitud);
p2 = p1;
while p2 == p1
p2 = seleccion(aptitud);
end
[h1,h2] = cruza(x(:,p1),x(:,p2));
xh(:,k) = h1;
xh(:,k+1) = h2;
end
% disp(xh);
pm = 0.3;
for i=1:N
for j=1:D
if rand > pm
else
xh(j,i) = xl(j) + (xu(j) - xl(j))*rand();
end
end
end
x = xh;
end
1 Comment
Yasasvi Harish Kumar
on 18 Feb 2019
Use the plot command to plot.
Syntax: plot(x,y)
Use the hold on command after the plot command to plot over the previous plot or use subplot command to have multiple plots.
Regards
Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!