symbolic integral inside a numerical one. How to program it?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
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 件のコメント
a1 = 1; b2 = 2; s = .16; u = .07; T=1;
syms u; syms s;
matlabfirst=@ (u) first(u, a1)*exp(-int(matlabsecond,s,0,u)); matlabsecond=@ (s) second(s, b1);
tau=integral(matlabfirst, 0, T);
function y=first(u,a1) y=a1*u;
function z=second(s,b1) z=b1*s;
What do you think?
You can convert your symbolic function to matlab function using the function in the link I provided. Please try it first and post here if it didn't work for you. There are examples at the bottom on how to use it.
function [Put]=Putprice()
a1=1; a2=2; b1=3; b2=2; s=.16; u=.07; T=1;
syms s; syms u; b=b1*s+b2; bfunc=matlabfunction(b); bfunc=@(s)b1.*s+b2;
a=a1*u+a2; afunc=matlabfunction((a1*u+a2)); afunc = @(u)(a1.*u+a2);
Put=integral(afunc*int(bfunc,s,0,u),0,T); disp(Put);
end
Somehow this doesn't work for me :(
Shashank Prasanna
2013 年 7 月 4 日
編集済み: Shashank Prasanna
2013 年 7 月 4 日
MATLAB is case sensitive, matlabfunction is not same as matlabFunction. Lets try once more, this time give more details on what you see on the command line. 'doesn't work for me', is not enough information to help.
Also:
bfunc=matlabfunction(b);
bfunc=@(s)b1.*s+b2;
Running those to successively means assigning bfunc twice in a row, it doesn't make much sense.
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?
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 件)
カテゴリ
ヘルプ センター および File Exchange で Calculus についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
