フィルターのクリア

How to plot rastrigin function in matlab

11 ビュー (過去 30 日間)
Atinesh Singh
Atinesh Singh 2016 年 11 月 13 日
編集済み: PRIYANSHI 2023 年 1 月 23 日
I'm trying to plot rastrigin function this way
clear; clc; close all;
limits = repmat([-5 5], 2, 1);
[X,Y] = meshgrid(linspace(limits(1,1),limits(1,2),100),...
linspace(limits(2,1),limits(2,2),100));
Z = reshape(rastrigin([X(:)'; Y(:)']), 100, 100);
surf(X,Y,Z);
axis([-5 5 -5 5 0 90]);
shading interp
rastrigin.m
function [y] = rastrigin(x)
d = length(x);
sq = x.^2;
y = 10*d + sum(sq - 10*cos(2*pi*x));
end
But I'm getting empty figure
  1 件のコメント
Mahmoud ABURUB
Mahmoud ABURUB 2020 年 2 月 22 日
編集済み: Mahmoud ABURUB 2020 年 2 月 22 日
Rastrigin's Function has only one global minima at point [0,0]
the coden for ploting the function in 3D (X,Y,Z) is:
function [y]=rastringis(x1,x2)
dx1=length(x1);
dx2=length(x2);
for i=1:dx1
for j=1:dx2
y(i,j)=(20+x1(i).^2+x2(j).^2)-10*(cos(2*pi*x1(i))+cos(2*pi*x2(j)));
end
end
end
x1=[-5:0.1:5];
x2=[-5:0.1:5];
y=rastringis(x1,x2)
meshc(x1,x2,y);
the figure is attached to this message.

サインインしてコメントする。

採用された回答

dpb
dpb 2016 年 11 月 13 日
Your scaling seems to be out of whack--adding 10+length(x) is creating a Z of
>> [min(Z(:)) max(Z(:))]
ans =
1.0e+03 *
0.9810 1.0604
>>
Hence your axis command puts the Z axis range quite a long distance below where the data values are.
It appears from the functional definition, the offset factor you're looking for would be 20; 2X the scale factor of the cosine term. If I do that and then
zlim([0 90])
get an interesting figure, indeed. Hadn't heard of the rastrigin function...
  2 件のコメント
Atinesh Singh
Atinesh Singh 2016 年 11 月 13 日
I didn't get you
dpb
dpb 2016 年 11 月 13 日
Did you look at min/max of your Z array???? Apparently not...
Try removing the axis command entirely and see what happens. The combination of those should result in enlightenment.

サインインしてコメントする。

その他の回答 (1 件)

PRIYANSHI
PRIYANSHI 2023 年 1 月 23 日
編集済み: PRIYANSHI 2023 年 1 月 23 日
click on step button till you see a figure (till 6-7 line ) u will get the figure

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by