differential evalution code Error using * Inner matrix dimensions must agree.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Error in @(x)sum((minus((x(1)),V)*sin(2.*pi.*x(2).*t+x(3)).^2))/n Error in noise_de (line 56) pop(i).Cost=CostFunction(pop(i).Position);
- clear all; close all; clc
- fs=200; %sampling freq.
- dt =1/fs;
- n=fs/3 %number of samples/cycle
- m=3 %no. of cycles
- fi=50;
- t = dt*(0:400); %data window
- ww=wgn(201,1,-40);
- size(transpose(ww))
- t =dt*(0:200);
- y=sin(2*pi*fi*t + 0.3);
- for j=0:200/(n*m)
- t =dt*(j*m*n:(j+1)*m*n);
- x=sin(2*pi*fi*t + 0.3)+transpose(wgn(1+n*m,1,-40));
- V=x
- tmax=0.01;
- lastreported=0;
- %% Problem Definition
- t_est=[];
- f_est=[];
- dt=1/fs;
- i_max=tmax*fs
- for ii=0:i_max
- if(ii/i_max*100-lastreported>=1)
- lastreported=ii/i_max*100;
- fprintf('%5.2f%%\n',lastreported);
- end
- t=(ii:ii+n-1)*dt;
- CostFunction=@(x) sum((minus((x(1)),V)*sin(2*pi.*x(2).*t+x(3)).^2))/n; % Cost Function
- nVar=3; % Number of Decision Variables
- VarSize=[1 nVar]; % Decision Variables Matrix Size
- VarMin=[0,48,0]; % Lower Bound of Decision Variables
- VarMax=[1000,52,2*pi]; % Upper Bound of Decision Variables
- %% DE Parameters
- MaxIt=200; % Maximum Number of Iterations
- nPop=50; % Population Size
- beta=0.5; % Scaling Factor
- pCR=0.2; % Crossover Probability
- minCost=1e-10;
- %% Initialization
- empty_individual.Position=[];
- empty_individual.Cost=[];
- BestSol.Cost=inf;
- pop=repmat(empty_individual,nPop,1);
- for i=1:nPop
- pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
- pop(i).Cost=CostFunction(pop(i).Position);
- if pop(i).Cost<BestSol.Cost
- BestSol=pop(i);
- end
- end
- BestCost=zeros(MaxIt,1);
- %% DE Main Loop
- for it=1:MaxIt
- for i=1:nPop
- x=pop(i).Position;
- A=randperm(nPop);
- A(A==i)=[];
- a=A(1);
- b=A(2);
- c=A(3);
- % Mutation
- %beta=unifrnd(beta_min,beta_max);
- y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
- y = max(y, VarMin);
- y = min(y, VarMax);
- % Crossover
- z=zeros(size(x));
- j0=randi([1 numel(x)]);
- for j=1:numel(x)
- if j==j0 || rand<=pCR
- z(j)=y(j);
- else
- z(j)=x(j);
- end
- end
- NewSol.Position=z;
- NewSol.Cost=CostFunction(NewSol.Position);
- if NewSol.Cost<pop(i).Cost
- pop(i)=NewSol;
- if pop(i).Cost<BestSol.Cost
- BestSol=pop(i);
- end
- end
- end
- % Update Best Cost
- BestCost(it)=BestSol.Cost;
- % Show Iteration Information
- %disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
- if(minCost>BestSol.Cost)
- break;
- ErrorTarget=0.00000001;
- EvalMax=10000*n;
- end
- end
- %% Show Results
- % disp(['Iteration ' num2str(ii) ': Best Cost = ' num2str(BestSol.Position(2))]);
- t_est=[t_est;(ii)*dt];
- f_est=[f_est;BestSol.Position(2)];
- if(minCost>BestSol.Cost)
- %break;
- ErrorTarget=0.00000001;
- EvalMax=10000*n;
- end
- end
- end
- t_est
- f_est
- plot (t_est,f_est,'red')
- hold on
- xlabel('time')
- ylabel('frequency')
- title('DE white noise ')
- c=vpa(rms(fi(t_est)-f_est))
- plot (t_est,fi*ones(size(t_est)))
- hold off
채택된 답변
Stephan
2020년 12월 2일
@(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
% ^
% |
% ------- Elementwise multiplication
댓글 수: 19
what can I do
Did you replace your function with my? I provided the corrected version.
the error :
Error using +
Matrix dimensions must agree.
Error in @(t)(sin(2*pi*fi*t+0.3)+transpose(wgn(1+n*m,1,-40)))
Error in noise_de (line 23)
V=v(t);
- clear all; close all; clc
- fs=200; %sampling freq.
- dt =1/fs;
- n=fs/3 %number of samples/cycle
- m=3 %no. of cycles
- fi=50;
- t = dt*(0:400); %data window
- v=@(t) (sin(2*pi*fi*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
- tmax=1;
- t_est=[];
- f_est=[];
- dt=1/fs;
- i_max=tmax*fs;
- for ii=0:i_max
- t=(ii:ii+n-1)*dt;
- V=v(t);
- CostFunction=@(x) sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n; % Cost Function
- nVar=3; % Number of Decision Variables
- VarSize=[1 nVar]; % Decision Variables Matrix Size
- VarMin=[0,48,0]; % Lower Bound of Decision Variables
- VarMax=[1000,52,2*pi]; % Upper Bound of Decision Variables
- %% DE Parameters
- MaxIt=1000; % Maximum Number of Iterations
- nPop=50; % Population Size
- beta=0.5; % Scaling Factor
- pCR=0.2; % Crossover Probability
- minCost=1e-10;
- %% Initialization
- empty_individual.Position=[];
- empty_individual.Cost=[];
- BestSol.Cost=inf;
- pop=repmat(empty_individual,nPop,1);
- for i=1:nPop
- pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
- pop(i).Cost=CostFunction(pop(i).Position);
- if pop(i).Cost<BestSol.Cost
- BestSol=pop(i);
- end
- end
- BestCost=zeros(MaxIt,1);
- %% DE Main Loop
- for it=1:MaxIt
- for i=1:nPop
- x=pop(i).Position;
- A=randperm(nPop);
- A(A==i)=[];
- a=A(1);
- b=A(2);
- c=A(3);
- % Mutation
- %beta=unifrnd(beta_min,beta_max);
- y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
- y = max(y, VarMin);
- y = min(y, VarMax);
- % Crossover
- z=zeros(size(x));
- j0=randi([1 numel(x)]);
- for j=1:numel(x)
- if j==j0 || rand<=pCR
- z(j)=y(j);
- else
- z(j)=x(j);
- end
- end
- NewSol.Position=z;
- NewSol.Cost=CostFunction(NewSol.Position);
- if NewSol.Cost<pop(i).Cost
- pop(i)=NewSol;
- if pop(i).Cost<BestSol.Cost
- BestSol=pop(i);
- end
- end
- end
- % Update Best Cost
- BestCost(it)=BestSol.Cost;
- % Show Iteration Information
- %disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
- if(minCost>BestSol.Cost)
- break;
- end
- end
- %% Show Results
- disp(['Iteration ' num2str(ii) ': Best Cost = ' num2str(BestSol.Position(2))]);
- t_est=[t_est;(ii+n-1)*dt];
- f_est=[f_est;BestSol.Position(2)];
- end
I did it but I discover that I am not work in the first my code
so I resend it with intial error appear
please resend it without the line numbers in front - and format it as code by using the button. We can not copy this and insert it to Matlab.
common fernando
2020년 12월 2일
편집: Stephan
2020년 12월 2일
clear all; close all; clc
fs=200; %sampling freq.
dt =1/fs;
n=fs/3 %number of samples/cycle
m=3 %no. of cycles
fi=50;
t = dt*(0:200); %data window
v=@(t) (sin(2*pi*fi*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
tmax=1;
t_est=[];
f_est=[];
dt=1/fs;
i_max=tmax*fs;
for ii=0:i_max
t=(ii:ii+n-1)*dt;
V=v(t);
CostFunction=@(x) sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n; % Cost Function
nVar=3; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=[0,48,0]; % Lower Bound of Decision Variables
VarMax=[1000,52,2*pi]; % Upper Bound of Decision Variables
%% DE Parameters
MaxIt=1000; % Maximum Number of Iterations
nPop=50; % Population Size
beta=0.5; % Scaling Factor
pCR=0.2; % Crossover Probability
minCost=1e-10;
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
BestSol.Cost=inf;
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
pop(i).Cost=CostFunction(pop(i).Position);
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
BestCost=zeros(MaxIt,1);
%% DE Main Loop
for it=1:MaxIt
for i=1:nPop
x=pop(i).Position;
A=randperm(nPop);
A(A==i)=[];
a=A(1);
b=A(2);
c=A(3);
% Mutation
%beta=unifrnd(beta_min,beta_max);
y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
y = max(y, VarMin);
y = min(y, VarMax);
% Crossover
z=zeros(size(x));
j0=randi([1 numel(x)]);
for j=1:numel(x)
if j==j0 || rand<=pCR
z(j)=y(j);
else
z(j)=x(j);
end
end
NewSol.Position=z;
NewSol.Cost=CostFunction(NewSol.Position);
if NewSol.Cost<pop(i).Cost
pop(i)=NewSol;
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
end
% Update Best Cost
BestCost(it)=BestSol.Cost;
% Show Iteration Information
%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
if(minCost>BestSol.Cost)
break;
end
end
%% Show Results
disp(['Iteration ' num2str(ii) ': Best Cost = ' num2str(BestSol.Position(2))]);
t_est=[t_est;(ii+n-1)*dt];
f_est=[f_est;BestSol.Position(2)];
end
t_est
f_est
RMSE = sqrt(mean((f_est-fi).^2))
plot(t_est,f_est,'red')
hold on
xlabel('time')
ylabel('frequency')
title('DE white noise')
plot (t,fi)
hold off
I do not have communications toolbox - but you should also use elementwise multiplication here:
v=@(t) (sin(2*pi*fi.*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
% ^
% |
% ------- Fixed here
Due to the missing toolbox i cant check if this is the only problem...
the error in +
when write ur code give me
Undefined function 'minus' for input arguments of type 'function_handle'.
Error in @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
Error in finaldenoise (line 46)
pop(i).Cost=CostFunction(pop(i).Position);
v=@(t) (sin(2*pi*fi.*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
No minus here. Is it again the wrong function like before?
minus is here:
Error in @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
thw wrong is :
Undefined function 'minus' for input arguments of type 'function_handle'.
Error in @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
Error in finaldenoise (line 46)
pop(i).Cost=CostFunction(pop(i).Position);
>>
minus is here:
Error in @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
thw wrong is :
Undefined function 'minus' for input arguments of type 'function_handle'.
Error in @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
Error in finaldenoise (line 46)
pop(i).Cost=CostFunction(pop(i).Position);
>>
the orign code or the line contain minus :
CostFunction=@(x) sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n; % Cost Function
give me the error:
Undefined function 'minus' for input arguments of type 'function_handle'.
Error in @(x)sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n
Error in finaldenoise (line 46)
pop(i).Cost=CostFunction(pop(i).Position);
>>
i changed it by : @(x)sum((minus((x(1)),V).*sin(2.*pi.*x(2).*t+x(3)).^2))/n
but the same error
what do you get if you enter this in the command line:
test = @(x,y) minus(x,y) * 4 + x.*y
test(2,3)
result should be:
test =
function_handle with value:
@(x,y)minus(x,y)*4+x.*y
ans =
2
yes answer =2
test =
@(x,y)minus(x,y)*4+x.*y
ans =
2
what do you want to tell me?
This works for me:
clear all; close all; clc
fs=200; %sampling freq.
dt =1/fs;
n=fs/3 %number of samples/cycle
m=3 %no. of cycles
fi=50;
t = linspace(0,200,1+n*m); %data window
v=@(t) (sin(2*pi*fi.*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
tmax=1;
t_est=[];
f_est=[];
dt=1/fs;
i_max=tmax*fs;
for ii=0:i_max
V=v(t);
CostFunction=@(x) sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n; % Cost Function
nVar=3; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=[0,48,0]; % Lower Bound of Decision Variables
VarMax=[1000,52,2*pi]; % Upper Bound of Decision Variables
%% DE Parameters
MaxIt=1000; % Maximum Number of Iterations
nPop=50; % Population Size
beta=0.5; % Scaling Factor
pCR=0.2; % Crossover Probability
minCost=1e-10;
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
BestSol.Cost=inf;
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
pop(i).Cost=CostFunction(pop(i).Position);
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
BestCost=zeros(MaxIt,1);
%% DE Main Loop
for it=1:MaxIt
for i=1:nPop
x=pop(i).Position;
A=randperm(nPop);
A(A==i)=[];
a=A(1);
b=A(2);
c=A(3);
% Mutation
%beta=unifrnd(beta_min,beta_max);
y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
y = max(y, VarMin);
y = min(y, VarMax);
% Crossover
z=zeros(size(x));
j0=randi([1 numel(x)]);
for j=1:numel(x)
if j==j0 || rand<=pCR
z(j)=y(j);
else
z(j)=x(j);
end
end
NewSol.Position=z;
NewSol.Cost=CostFunction(NewSol.Position);
if NewSol.Cost<pop(i).Cost
pop(i)=NewSol;
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
end
% Update Best Cost
BestCost(it)=BestSol.Cost;
% Show Iteration Information
%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
if(minCost>BestSol.Cost)
break;
end
end
%% Show Results
disp(['Iteration ' num2str(ii) ': Best Cost = ' num2str(BestSol.Position(2))]);
t_est=[t_est;(ii+n-1)*dt];
f_est=[f_est;BestSol.Position(2)];
end
t_est
f_est
RMSE = sqrt(mean((f_est-fi).^2))
plot(t_est,f_est,'red')
hold on
xlabel('time')
ylabel('frequency')
title('DE white noise')
plot (t,fi)
hold off
The problem is here:
t_est=[t_est;(ii+n-1)*dt];
this leads to values of t_est ranging from 0...1 - not 0...200 as expected. If you change it to:
t_est=[t_est; (ii-1)*dt*fs];
The values are scaled 0...200. But due to missing insight in the problem i dont know if this is correct. At least the plot seems to be ok.
Since fi is ascalar, it wont plot - i suggest to use yline instead:
plot(t_est,f_est,'red')
xlim([0 200])
yline(fi)
xlabel('time')
ylabel('frequency')
title('DE white noise')

So once again the whole code:
clear all; close all; clc
fs=200; %sampling freq.
dt =1/fs;
n=fs/3 %number of samples/cycle
m=3 %no. of cycles
fi=50;
t = linspace(0,200,1+n*m); %data window
v=@(t) (sin(2*pi*fi.*t + 0.3)+ transpose(wgn(1+n*m,1,-40)));
tmax=1;
t_est=[];
f_est=[];
dt=1/fs;
i_max=tmax*fs;
for ii=0:i_max
V=v(t);
CostFunction=@(x) sum((V-x(1)*sin(2*pi*x(2)*t+x(3))).^2)/n; % Cost Function
nVar=3; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=[0,48,0]; % Lower Bound of Decision Variables
VarMax=[1000,52,2*pi]; % Upper Bound of Decision Variables
%% DE Parameters
MaxIt=1000; % Maximum Number of Iterations
nPop=50; % Population Size
beta=0.5; % Scaling Factor
pCR=0.2; % Crossover Probability
minCost=1e-10;
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
BestSol.Cost=inf;
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
pop(i).Cost=CostFunction(pop(i).Position);
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
BestCost=zeros(MaxIt,1);
%% DE Main Loop
for it=1:MaxIt
for i=1:nPop
x=pop(i).Position;
A=randperm(nPop);
A(A==i)=[];
a=A(1);
b=A(2);
c=A(3);
% Mutation
%beta=unifrnd(beta_min,beta_max);
y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
y = max(y, VarMin);
y = min(y, VarMax);
% Crossover
z=zeros(size(x));
j0=randi([1 numel(x)]);
for j=1:numel(x)
if j==j0 || rand<=pCR
z(j)=y(j);
else
z(j)=x(j);
end
end
NewSol.Position=z;
NewSol.Cost=CostFunction(NewSol.Position);
if NewSol.Cost<pop(i).Cost
pop(i)=NewSol;
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
end
% Update Best Cost
BestCost(it)=BestSol.Cost;
% Show Iteration Information
%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
if(minCost>BestSol.Cost)
break;
end
end
%% Show Results
disp(['Iteration ' num2str(ii) ': Best Cost = ' num2str(BestSol.Position(2))]);
t_est=[t_est; (ii-1)*dt*fs];
f_est=[f_est;BestSol.Position(2)];
end
t_est
f_est
RMSE = sqrt(mean((f_est-fi).^2))
plot(t_est,f_est,'red')
xlim([0 200])
yline(fi)
xlabel('time')
ylabel('frequency')
title('DE white noise')
I'll try it , thanks at all
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Gamma Functions에 대해 자세히 알아보기
참고 항목
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)

