이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
how to plot the graph for the following result
조회 수: 1 (최근 30일)
이전 댓글 표시
jaah navi
2019년 2월 5일
I am getting the following result in the command window.
iter = 1
1. global fitness is 1.9669
iter = 2
2. global fitness is 1.9669
iter = 3
3. global fitness is 1.9669
iter = 4
4. global fitness is 1.2878
I want to plot the graph in a manner iter should be on x axis and global fitness on y axis.
I tried with the command plot((global_fitness,:),'-*b') but unable to get the result.
Could anyone please help to fix the issue
채택된 답변
KSSV
2019년 2월 5일
figure
hold on
for i = 1:100
global_fitness = rand ;
plot(i,global_fitness,'*r')
end
Or, save all the values of a loop and plot at the end.
y = zeros([],1) ;
for i = 1:100
global_fitness = rand ;
y(i) = global_fitness ;
end
plot(1:100,y,'*r')
댓글 수: 13
jaah navi
2019년 2월 5일
ok.I can get the graph.But I want to connect all the points.
I tried with the command
figure
hold on
for i = 1:4
global_fitness = rand ;
plot(i,global_fitness,'*-r')
end
But still the points are not getting connected.
Could you please help me on this.
Torsten
2019년 2월 5일
Collect your data inside the loop and plot outside the loop (KSSV's second option).
jaah navi
2019년 2월 5일
I tried but unabe to get the result with respect to the following code:
for iter = 1:iterations
y(iter) = global_fitness
end
plot(1:iterations,y(iter),'*-r').
Could you please help me on this.
Torsten
2019년 2월 5일
Did KSSV write anything like "y(iter)" in the plot command ? I can only see "y" ...
jaah navi
2019년 2월 5일
as i am unable to get the result i tried with the above command.
so far i can able to plot only the last value of the global fitness with respect to last iteration.
what i actually need is i want to plot the graph for each global_fitness correponds to that particular iteration.
As mentioned before i am getting the following result in the command window
iter = 1
1. global fitness is 1.9669
iter = 2
2. global fitness is 1.9669
iter = 3
3. global fitness is 1.9669
iter = 4
4. global fitness is 1.2878
I want to plot iteration in xaxis and global fitness in yaxis by connecting all the points.Could you help me to fix it.
jaah navi
2019년 2월 5일
clear;
close all;
centroids = 2; % == clusters here (aka centroids)
dimensions = 2; % how many dimensions in each centroid x and y axis
particles = 2; % how many particles in the swarm, aka how many solutions
iterations = 50; % iterations of the optimization alg.
simtime=0.801; % simulation delay btw each iteration
dataset_subset = 2; % for the IRIS dataset, dataset_subset and dimensions are dependent on one another
write_video = false; % enable to grab the output picture and save a video
hybrid_pso = true; % enable/disable hybrid_pso
manual_init = false; % enable/disable manual initialization (only for dimensions={2,3})
% VIDEO GRUB STUFF...
if write_video
writerObj = VideoWriter('PSO.avi');
writerObj.Quality=100;
% writerObj.FrameRate=30;
open(writerObj);
end
% LOAD DEFAULT CLUSTER (IRIS DATASET); USE WITH CARE!
% load fisheriris.mat
load fisheriris.mat
meas = meas(:,1+dataset_subset:dimensions+dataset_subset); %RESIZE THE DATASET WITH CURRENT DIMENSIONS; USE WITH CARE!
dataset_size = size (meas);
% GLOBAL PARAMETERS
w = 0.72; %INERTIA
c1 = 1.49; %COGNITIVE
c2 = 1.49; %SOCIAL
% PLOT STUFF... HANDLERS AND COLORS
pc = []; txt = [];
cluster_colors_vector = rand(particles, 3);
% PLOT DATASET
fh=figure(1);
hold on;
if dimensions == 3
plot3(meas(:,1),meas(:,2),meas(:,3),'k*');
view(3);
elseif dimensions == 2
plot(meas(:,1),meas(:,2),'k*');
end
% PLOT STUFF .. SETTING UP AXIS IN THE FIGURE
axis equal;
axis(reshape([min(meas)-2; max(meas)+2],1,[]));
hold off;
% SETTING UP PSO DATA STRUCTURES
swarm_vel = rand(centroids,dimensions,particles)*0.1;
swarm_pos = rand(centroids,dimensions,particles);
swarm_best = zeros(centroids,dimensions);
c = zeros(dataset_size(1),particles);
ranges = max(meas)-min(meas); %%scale
swarm_pos = swarm_pos .* repmat(ranges,centroids,1,particles) + repmat(min(meas),centroids,1,particles)% with respect to dataset
swarm_fitness(1:particles)=Inf;
% MANUAL INITIALIZATION (only for dimension 2 and 3)
if manual_init
if dimensions == 3
% MANUAL INIT ONLY FOR THE FIRST PARTICLE
swarm_pos(:,:,1) = [6 3 4; 5 3 1];
elseif dimensions == 2
% KEYBOARD INIT ONLY FOR THE FIRST PARTICLE
swarm_pos(:,:,1) = ginput(2)
end
end
for iteration=1:iterations
%CALCULATE EUCLIDEAN DISTANCES TO ALL CENTROIDS
distances=zeros(dataset_size(1),centroids,particles);
for particle=1:particles
for centroid=1:centroids
distance=zeros(dataset_size(1),1);
for data_vector=1:dataset_size(1)
%meas(data_vector,:)
distance(data_vector,1)=norm(swarm_pos(centroid,:,particle)-meas(data_vector,:));
end
distances(:,centroid,particle)=distance;
end
end
%ASSIGN MEASURES with CLUSTERS
for particle=1:particles
[value, index] = min(distances(:,:,particle),[],2);
c(:,particle) = index;
end
% PLOT STUFF... CLEAR HANDLERS
delete(pc); delete(txt);
pc = []; txt = [];
% PLOT STUFF...
hold on;
for particle=1:particles
for centroid=1:centroids
if any(c(:,particle) == centroid)
if dimensions == 3
pc = [pc plot3(swarm_pos(centroid,1,particle),swarm_pos(centroid,2,particle),swarm_pos(centroid,3,particle),'*','color',cluster_colors_vector(particle,:))];
elseif dimensions == 2
pc = [pc plot(swarm_pos(centroid,1,particle),swarm_pos(centroid,2,particle),'*','color',cluster_colors_vector(particle,:))];
end
end
end
end
set(pc,{'MarkerSize'},{12})
hold off;
%CALCULATE GLOBAL FITNESS and LOCAL FITNESS:=swarm_fitness
average_fitness = zeros(particles,1)
for particle=1:particles
for centroid = 1 : centroids
if any(c(:,particle) == centroid)
local_fitness=mean(distances(c(:,particle)==centroid,centroid,particle));
average_fitness(particle,1) = average_fitness(particle,1) + local_fitness;
end
end
average_fitness(particle,1) = average_fitness(particle,1) / centroids;
if (average_fitness(particle,1) < swarm_fitness(particle))
swarm_fitness(particle) = average_fitness(particle,1);
swarm_best(:,:,particle) = swarm_pos(:,:,particle); %LOCAL BEST FITNESS
end
end
[global_fitness, index] = min(swarm_fitness); %GLOBAL BEST FITNESS
swarm_overall_pose = swarm_pos(:,:,index); %GLOBAL BEST POSITION
------------------------------------------------------------------------------------------------------
% SOME INFO ON THE COMMAND WINDOW
fprintf('%3d. global fitness is %5.4f\n',iteration,global_fitness);
pause(simtime);
---------------------------------------------------------------------------------------------
% VIDEO GRUB STUFF...
if write_video
frame = getframe(fh);
writeVideo(writerObj,frame);
end
% SAMPLE r1 AND r2 FROM UNIFORM DISTRIBUTION [0..1]
r1 = rand;
r2 = rand;
% UPDATE CLUSTER CENTROIDS
for particle=1:particles
inertia = w * swarm_vel(:,:,particle);
cognitive = c1 * r1 * (swarm_best(:,:,particle)-swarm_pos(:,:,particle));
social = c2 * r2 * (swarm_overall_pose-swarm_pos(:,:,particle));
vel = inertia+cognitive+social;
swarm_pos(:,:,particle) = swarm_pos(:,:,particle) + vel ; % UPDATE PARTICLE POSE
swarm_vel(:,:,particle) = vel; % UPDATE PARTICLE VEL
end
end
% PLOT THE ASSOCIATIONS WITH RESPECT TO THE CLUSTER
hold on;
particle=index; %select the best particle (with best fitness)
cluster_colors = ['m','g','y','b','r','c','g'];
for centroid=1:centroids
if any(c(:,particle) == centroid)
if dimensions == 3
plot3(meas(c(:,particle)==centroid,1),meas(c(:,particle)==centroid,2),meas(c(:,particle)==centroid,3),'-','color',cluster_colors(centroid));
elseif dimensions == 2
plot(meas(c(:,particle)==centroid,1),meas(c(:,particle)==centroid,2),'o','color',cluster_colors(centroid));
end
end
end
hold off;
% VIDEO GRUB STUFF...
if write_video
frame = getframe(fh);
writeVideo(writerObj,frame);
close(writerObj);
end
% SAY GOODBYE
fprintf('\nEnd, global fitness is %5.4f\n',global_fitness);
With respect to the code i want to plot the graph for convergence by having iteration on the xaxis and global fitness on y axis with respect to the command line
fprintf('%3d. global fitness is %5.4f\n',iteration,global_fitness); on the code.
jaah navi
2019년 2월 6일
As you have told to copy the entire code I have copied it.Could you please help me on this.
KSSV
2019년 2월 6일
I forgot....I have seen the code last night itself....but as it was huge..I was kind of disappointed ;). By the way what you are trying to do with this big code..? I am just curious and want to know more......get the attched code to plot what you want.
jaah navi
2019년 2월 7일
With resepect to the codingi need to find on which iteration the fitness function is getting converged.And to get the centroid location with respect to the data sets.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
