fminsearch anonymous use question

hello!
i want to know the information about how to use fminsearch function
i cant add anonymous functions.
in my situation i have to use loop to add the functions
for cnt=1:count(i)
cost=cost+@(x)((P(conAM(i,cnt))-5*b*b*log10((x(1)-AM(conAM(i,cnt),1))^2+(x(2)-AM(conAM(i,cnt),2))^2))^2);
end
fminsearch(cost,[7 7]);
i made this function but it doesn't work
and i need to fminsearch that function. (i don't have x(1),x(2))
++ i cant find information about anonymous function when f=@(x)(about(x(1),x(2)) how to insert variable ??? what is difference of y=@(x,y)(any x y funtion)???
please help me
this made me crazy

 Accepted Answer

Matt J
Matt J on 31 May 2019
Edited: Matt J on 31 May 2019
For this, it would be better to use a nested function. That way, you have the freedom to break up the cost evaluation across multiple commands, and to use loops.
function myOptimization
conAM=___; %Assuming these are variables, and not functions
AM=___;
P=____;
result=fminsearch(@objective,x0)
function cost=objective(x)
cost=0;
for cnt=1:count(i)
t=conAM(i,cnt); %pre-extract for efficiency
cost=cost + ((P(t)-5*b*b*log10((x(1)-AM(t,1))^2+(x(2)-AM(t,2))^2))^2);
end
end
end

2 Comments

thank you answering my question :)
but nested function does not defined in the loop(like for, while)
so this answer can't use my case
ps. i'm not good at English. please appreicate me if i was rude
seong gyu wi's comment moved here:
thank you so much
your alright sorry I'm not good at using MATlab so i use wrong way
% % SNR정의 signal power/noise power
AM=15*rand(17,2);
AMcopy=AM;
R=4;
b=2;
a=2;
jn=3; % 람다
Pj=40;
BM=15*rand(120,2);
BMcopy=BM;
conAM=zeros(120,137);
find=0;
for i=1:120-find
for j=1:17+find
path(i,j)=-10*b*log10(dist(BM(i,:),AM(j,:)));
end
end % %AM BM 사이의 path loss측정
noise=a*randn(120-find,17+find);
for i=1:120-find
for j=1:17+find
Pij(i,j)=Pj+path(i,j)+noise(i,j);
SNR(i,j)=Pij(i,j)/(a^2);
end
end % %송수신 에너지 차이,SNR구하기
for i=1:120-find % %연결가능한 AM의 개수 찾기와 어떤 AM과 연결되는지 찾기, 자기 자신 제외,SNR의 특정값보다 커야함
% %실제 위치를 이용해야 한다(연결 가능한 경우를 찾기 위해)
dst=0;
count=0;
for j=1:17+find
dst=dist(BM(i,:),AM(j,:));
if dst<R && SNR(i,j)>jn
count=count+1;
conAM(i,count)=j; % %연결가능한 AM의 위치 기록
end
end
end
% %연결 가능한 BM의 위치를 추정한다. 상대위치를 이용해야 한다.(실제 위치는 모르기 때문에)
for i=1:120+find
j=1;
while conAM(i,j)~=0
j=j+1;
end
P=Pij-Pj;
count(i)=j-1; % %연결된 AM의 개수
if count(i)>2
ff=@(x)path_loss(x,i,AM,conAM,P,count,b);
fminsearch(ff,[7 7])
end
end
% %연결한 BM AM을 수정한다.
this is my code thank you!!

Sign in to comment.

More Answers (0)

Asked:

on 31 May 2019

Edited:

on 31 May 2019

Community Treasure Hunt

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

Start Hunting!