symbolic integral inside a numerical one. How to program it?

조회 수: 2 (최근 30일)
Daniel
Daniel 2013년 7월 2일
Hi guys,
I need to compute an integral
tau(T)=integral(a(u)*exp(-int(b(s),s,0,u),0,T);
so, I need to declare two functions a(u) and b(s) first and then compute the integral for specified T.
Somehow I always encounter problems when using this method. Please, help me!
Daniel.

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 7월 3일
If you are deriving a(u) and b(s) symbolically then you will need to convert them into matlab functions before you use them for numerical computations:
Here's an example:
syms x
y = x^2
yFun = matlabFunction(y)
integral(yFun,0,1)
Here's the documentation for matlabFunction:
  댓글 수: 6
Daniel
Daniel 2013년 7월 5일
편집: Daniel 2013년 7월 6일
a1=0; a2=1; b1=0; b2=0.02; r1=0; r2=0.05; q1=0; q2=0; syms u;
%%%%%%%%%%%%%%%%%specify r(t) and q(t)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=r1*u+r2;
q=q1*u+q2;
rfunc=@(u)r1*u+r2;
qfunc=@(u)q1*u+q2;
%%%%%%%%%%%%%%%%%specify a(t) and b(t) functions %%%%%%%%%%%%%%%%%%%%%
a=a1*u+a2;
b=b1*u+b2;
alpha=r-q+b;
bfunc=@(u)b1*u+b2;
taufunc=@(u)a^2*exp(-2*abs(beta)*int(alpha,u,0,u));
rbfunc=@(u) r1*u+r2+b1*u+b2;
alphafunc=@(u) r1*u+r2+b1*u+b2-q1*u-q2;
res=taufunc(u);
resfunc=matlabFunction(res);
%%%%%%%%%%%%%%%%%compute integral for tau!%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tau=integral(@(u)resfunc(u),0,T);
That's what I wrote. It is working. However it doesn't work for optimization via fmincon. Could you help me simplify the code?
Is it possible to introduce vectors here? Can matlab calculate vector of integrals?
Daniel
Daniel 2013년 7월 6일
편집: Daniel 2013년 7월 6일
When I try to use this code for calibration it gives the following error:
Error using symengine>@()1.0e2
Too many input arguments.
Error in Putprice>@(u)resfunc(u) (line 35)
tau=integral(@(u)resfunc(u),0,T);
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in Putprice (line 35)
tau=integral(@(u)resfunc(u),0,T);
Error in diff_sum (line 18)
Diff(n)= (mktprice(n)-Putprice(S,K(n),T,x(1),x(2))).^2;
Error in nlconst (line 744)
f = feval(funfcn{3},x,varargin{:});
Error in fmincon (line 837)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in calibration_main (line 29)
x = fmincon(@diff_sum,x0,[],[],[],[],lb,ub);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by