differential evalution code Error using * Inner matrix dimensions must agree.
Vous suivez désormais cette question
- Les mises à jour seront visibles dans votre flux de contenu suivi.
- Selon vos préférences en matière de communication il est possible que vous receviez des e-mails.
Une erreur s'est produite
Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.
0 votes
Partagez un lien vers cette question
- 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
Réponse acceptée
1 vote
19 commentaires
- 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


Plus de réponses (0)
Catégories
En savoir plus sur Gamma Functions dans Centre d'aide et File Exchange
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
