Problems with integral in fsolve

조회 수: 14 (최근 30일)
Oeer
Oeer 2019년 4월 9일
댓글: Walter Roberson 2019년 4월 9일
Hey
I have three functions with three unknown variables, but I can't find a solution for them. the features look like this:
functions.PNG
I've tried to do the following code:
m = 0.31;
r = 0.05;
delta = 0.02;
y_bar = 2;
w_1 = 5;
w_2_bar = 5;
sigma = 1;
b_bar = 3;
z_0=[0.5,3,4];
sol=fsolve(@(z)([
w_1*(1-z(1))-z(2)-((1+delta)/((1-m)*(1+r)))*int((b_bar+(1-z(1))*(1-m)*x+(1-m)*(1+r)*z(2))*(1/(2*pi*sigma^2))*exp(-((x-w_2_bar)^2)/(2*sigma^2)),x,0,z(3))
+((1+delta)/(1+r))*int(((1-z(1))*k+(1+r)*z(2))*(1/(2*pi*sigma^2))*exp(-((k-w_2_bar)^2)/(2*sigma^2)),k,z(3),inf);
z(1)*w_1+int(z(1)*h*(1/(2*pi*sigma^2))*exp(-((h-w_2_bar)^2)/(2*sigma^2)),h,0,z(3))
-int((b_bar-m*((1-z(1))*y+(1+r)*z(2)))*(1/(2*pi*sigma^2))*exp(-((y-w_2_bar)^2)/(2*sigma^2)),y,0,z(3));
z(3)-(1/(1-z(1))*y_bar+((1+r)/(1-z(1)))*z(2))]),z_0);
Hope there is one who can correct my code so it works, thank you very much if it is you. :)

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 9일
Code attached.
It is not fast code. You could improve the performance by changing the symbolic integrations into numeric integrations.
Or, since you are using symbolic integration, you could process your function once with symbolic z variables, and simplify. vpasolve() does a good job on what is left, or you can matlabFunction() and fsolve()
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 4월 9일
Having the int() inside the fsolve() is slow, and the int() have closed form representations if you use symbolic z. The performance gains from executing once symbolically and using the result with vpasolve() or fsolve() is substantial.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Torsten
Torsten 2019년 4월 9일
편집: Torsten 2019년 4월 9일
function main
z_0 = [0.5,3,4];
options = optimset('TolFun',1e-8,'TolX',1e-8);
sol = fminsearch(@fun,z_0,options)
end
function res = fun(z)
m = 0.31;
r = 0.05;
delta = 0.02;
y_bar = 2;
w_1 = 5;
w_2_bar = 5;
sigma = 1;
b_bar = 3;
re(1) = w_1*(1-z(1))-z(2)-((1+delta)/((1-m)*(1+r)))*integral(@(x)(b_bar+(1-z(1))*(1-m)*x+(1-m)*(1+r)*z(2))*(1/(2*pi*sigma^2))*exp(-((x-w_2_bar).^2)/(2*sigma^2)),0,z(3),'ArrayValued',1)...
+((1+delta)/(1+r))*integral(@(k)((1-z(1))*k+(1+r)*z(2))*(1/(2*pi*sigma^2)).*exp(-((k-w_2_bar).^2)/(2*sigma^2)),z(3),inf,'ArrayValued',1);
re(2) = z(1)*w_1+integral(@(h)z(1)*h*(1/(2*pi*sigma^2)).*exp(-((h-w_2_bar).^2)/(2*sigma^2)),0,z(3),'ArrayValued',1)...
-integral(@(y)(b_bar-m*((1-z(1))*y+(1+r)*z(2)))*(1/(2*pi*sigma^2)).*exp(-((y-w_2_bar).^2)/(2*sigma^2)),0,z(3),'ArrayValued',1);
re(3) = z(3)-(1/(1-z(1))*y_bar+((1+r)/(1-z(1)))*z(2));
res = norm(re)
end

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by