現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)
