fminsearch with an integral function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello!
I want to minimize a function with an integral using fminsearch. The question is that I dont know how to define it properly.
The function i want to minimize is:
I want to minimize it respective to θ being A,
given by the user before any calculation.
given by the user before any calculation.How can i define the function and the fminsearch procedure?
Thanks!
채택된 답변
Ameer Hamza
2020년 4월 15일
See this code
A = 1;
sigma0 = 2;
int_term = @(theta) integral(@(phi) 1/(sigma0*sqrt(2*pi))*exp(-1/2*(phi/sigma0).^2).*sin(theta-phi), 0, 2*pi);
obj_fun = @(theta) -A*cos(theta)+int_term(theta);
theta_sol = fsolve(obj_fun, 0)
댓글 수: 8
I could solve some examples with this but when i try to take a step further I get some errors.
int_term = @(theta,Sigma0) integral(@(phi) 1/(Sigma0*sqrt(2*pi))*exp(-1/2*(phi/Sigma0).^2).**(sin(theta-phi)).^2,0,2*pi);
Func = @(theta,Sigma0,A) -A*cos(theta) + @(theta,Sigma0);
where then i define my variables
A=120;
Sigma0= 1;
and try to solve it
FuncAux = @(theta) Func(theta,Sigma0,A)
theta_sol = fminsearch(Func, 0)
The following error appears
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
I dont understand why it appears in this case.
The final goal is to fit this function to experimental data and extract the value of Sigma0 but first i want to try to plot the minimization with several given A values for constant Sigma 0.
There were some mistakes in the pasted code, I corrected it and i got an asnwer
A=120;
Sigma0= 1;
int_term = @(theta,Sigma0) integral(@(phi) 1/(Sigma0*sqrt(2*pi))*exp(-1/2*(phi/Sigma0).^2).*(sin(theta-phi)).^2,0,2*pi);
Func = @(theta,Sigma0,A) -A*cos(theta) + int_term(theta,Sigma0);
FuncAux = @(theta) Func(theta,Sigma0,A);
theta_sol = fminsearch(FuncAux, 0.1);
Can you exactly paste the code which cause error?
The code I used is this one: (the variables are all with different names as i tied to simplify to post here)
MultAmtoOe= 10^(4) * 4*pi /(10^7);
MultAmtoemucc = 1/(10^3);
mu0=4*pi/(10^7);
MS_AMR = 1320 / MultAmtoemucc;
t_AMR = 20 /(10^10);
KuAVG_AMR=(150 * 10 ^(-4) / mu0) * mu0 * MS_AMR /2;
Sigma_Angle = 55 * pi /180 ;
Sigma_Ku = 0.1* KuAVG_AMR ;
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR*(sin(theta-phi)).^2,0,2*pi);
Hteste=120/MultAmtoOe;
thetaH=90 * pi/180;
fundecoy = @(thetaPL)EnergyFunc1(thetaPL,Sigma_Ku,Sigma_Angle,Hteste,thetaH);
[thetaval,energyval]=fminsearch(fundecoy,0);
The multipilication should be done with .* operator. Simply using * implies matrix multipilcation. Try the following code
MultAmtoOe= 10^(4) * 4*pi /(10^7);
MultAmtoemucc = 1/(10^3);
mu0=4*pi/(10^7);
MS_AMR = 1320 / MultAmtoemucc;
t_AMR = 20 /(10^10);
KuAVG_AMR=(150 * 10 ^(-4) / mu0) * mu0 * MS_AMR /2;
Sigma_Angle = 55 * pi /180 ;
Sigma_Ku = 0.1* KuAVG_AMR ;
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR.*(sin(theta-phi)).^2,0,2*pi);
%^ added this dot
Hteste=120/MultAmtoOe;
thetaH=90 * pi/180;
fundecoy = @(thetaPL)EnergyFunc1(thetaPL,Sigma_Ku,Sigma_Angle,Hteste,thetaH);
[thetaval,energyval]=fminsearch(fundecoy,0);
Thanks for the help!
I have just a question why is it assumed that is a matrix operation?
%v why not added here
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR.*(sin(theta-phi)).^2,0,2*pi);
%^ added this dot
Why for example you didnt put the dot in the first term? and only in the last one
The reason is a bit intricate but I can give you general detail. In MATLAB (*) operator is defined for matrix multiplication and (.*) is defined for element-wise multiplication. For a scalar number it does not make any difference. For example, I evalaute following example function without dots
fun = @(x,a,b) a*b*x*x;
fun(2,1,1)
ans =
4
and with dot
fun = @(x,a,b) a*b*x.*x;
fun(2,1,1)
ans =
4
No problem!!! But suppose I want to input x which is a vector. The first one will fail
fun = @(x,a,b) a*b*x*x;
fun([1 2 3],1,1)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns
in the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.
Error in @(x,a,b)a*b*x*x
Because 1x3 matrix cannot multiply with 1x3 matrix according to the rule of matrix multiplication. But element-wise multiplication works
fun = @(x,a,b) a*b*x.*x;
fun([1 2 3],1,1)
ans =
1 4 9
Now consider the function in your question. It gives a scalar function. If you input a scalar value, it will give a scalar output. However, to make the integral() and fminsearch function computationally efficient, MATLAB calls your function with theta and phi as vectors, not scalars. If your function does not support element-wise operations then it will fail for a vector input.
I didn't use .* in the first term because all the term multipliying the cos() function -mu0*H*MS_AMR*t_AMR are themself scalar, so it does not matter wather you use (.*) or (*). You can use either.
To summarize, you should always use element-wise operators when you are trying to integrate a scalar function using integral.
Pedro Araújo
2020년 4월 15일
Thanks a lot!
I will progress much faster!!!
Ameer Hamza
2020년 4월 15일
Glad to be of help.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
참고 항목
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)
