Reference to non-existent field '.

4 views (last 30 days)
sura naji
sura naji on 14 Nov 2020
Answered: Walter Roberson on 14 Nov 2020
clear
close all
% Problem Statement
Npar = 10;
VarLow=0.1;
VarHigh =35.2;
%BBBC parameters
N=50; %number of candidates
MaxIter=100; %number of iterations
% initialize a random value as best value
XBest = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
[FBest]=fitnessFunc(XBest);
GB=FBest;
t = cputime;
%intialize solutions and memory
X = zeros(N, Npar);
F = zeros(N, 1);
for ii = 1:N
X(ii,:) = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
% calculate the fitness of solutions
F(ii) = fitnessFunc(X(ii,:));
end
%Main Loop
for it=1:MaxIter
%Find the centre of mass
%-----------------------
%numerator term
num=zeros(1,Npar);
for ii=1:N
for jj=1:Npar
num(jj)=num(jj)+(X(ii,jj)/F(ii));
end
end
%denominator term
den=sum(1./F);
%centre of mass
Xc=num/den;
%generate new solutions
%----------------------
for ii=1:N
%new solution from centre of mass
for jj=1:Npar
New=X(ii,:);
New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);
end
%boundary constraints
NewP=limiter(New,VarHigh,VarLow);
%new fitness
newFit=fitnessFunc(New);
%check whether the solution is better than previous solution
if newFit<F(ii)
X(ii,:)=New;
F(ii)=newFit;
if F(ii)<FBest
XBest=X(ii,:);
FBest=F(ii);
end
end
end
% store the best value in each iteration
GB=[GB FBest];
end
t1=cputime;
fprintf('The time taken is %3.2f seconds \n',t1-t);
fprintf('The best value is :');
XBest
FBest
figure(1)
plot(0:MaxIter,GB, 'linewidth',1.2);
title('Convergence');
xlabel('Iterations');
ylabel('Objective Function (Cost)');
grid('on')
function D=Data10
Coord=360*[2 1 0;2 0 0;1 1 0;1 0 0;0 1 0;0 0 0];
Con=[5 3;1 3;6 4;4 2;3 4;1 2;6 3;5 4;4 1;3 2];
Re=[0 0 1;0 0 1;0 0 1;0 0 1;1 1 1;1 1 1];
Load=zeros(size(Coord));Load(2,:)=[0 -1e5 0];Load(4,:)=[0 -1e5 0];
E=ones(1,size(Con,1))*1e7;
A=ones(1,10);
% Available sections
AV=[1.62, 1.8, 1.99, 2.13, 2.38, 2.62, 2.88, 2.93, 3.09, 3.13, 3.38, 3.47, 3.55, 3.63, 3.84,...
3.87, 3.88, 4.18, 4.22, 4.49, 4.59, 4.80, 4.97, 5.12, 5.94, 7.22, 7.97, 11.5, 13.50,...
13.90, 14.2, 15.5, 16.0, 16.9, 18.8, 19.9, 22.0, 22.9, 28.5, 30.0, 33.5];%in^2
%Allowable Stress
TM=25000;%psi
%Allowable Displacement
DM=2;%inch
%WEIGHT PER UNIT of VOLUME
RO=.1;%lb/in^3
D=struct('Coord',Coord','Con',Con','Re',Re','Load',Load','E',E','A',A','AV',AV','TM',TM','DM',DM','RO',RO');
end
function [WE,FBest]=fitnessFunc(In,D)
D.A=In';
w=size(D.Re); *******the problem here
S=zeros(3*w(2));
U=1-D.Re;
f=find(U);
WE=0;
for i=1:size(D.Con,2)
H=D.Con(:,i);
C=D.Coord(:,H(2))-D.Coord(:,H(1));
Le=norm(C);
T=C/Le;s=T*T';
G=D.E(i)*D.A(i)/Le;
Tj(:,i)=G*T;
e=[3*H(1)-2:3*H(1),3*H(2)-2:3*H(2)];
S(e,e)=S(e,e)+G*[s -s;-s s];
WE=WE+Le*D.A(i)*D.RO;
end
U(f)=S(f,f)\D.Load(f);
F=sum(Tj.*(U(:,D.Con(2,:))-U(:,D.Con(1,:))));
R=reshape(S*U(:),w);
R(f)=0;
TS=(((abs(F'))./D.A)/D.TM)-1;%Tension
US=abs(U')/D.DM-1;%Displacement
PS=sum(TS.*(TS>0));
PD=sum(sum(US.*(US>0)));
FBest=WE*(1+PS+PD)^2;% Penalty function
end
please can any one guide me
Reference to non-existent field 'Re'. the error here how can l solved?

Answers (1)

Walter Roberson
Walter Roberson on 14 Nov 2020
function [WE,FBest]=fitnessFunc(In,D)
fitnessFunc expects a structure as a second parameter. In every instance you call it with only one parameter. You also never call the function than constructs the structure.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!