If condition inside integration
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi,
I am trying to keep if condition inside integration of exponential function and solve the integral in Matlab. c(x) is 4 at x =1, 4, 7, 10, 13 otherwise zero. Any help would be appreciated.
採用された回答
Walter Roberson
2021 年 10 月 30 日
I am pretty sure you do not want that definition of c(x)
syms x t
c(x) = piecewise(x == 1 | x == 4 | x == 7 | x == 10 | x == 13, 4, 0)
c(x) =
inner = c(x) * exp(-2*(t-x))
inner =
y(t) = simplify(int(inner, x, 0, t))
y(t) = 

string(y)
ans = "piecewise(in(t, 'real'), 0)"
fplot(y, [-20 20])

This happens because your c(x) definition is discontinuous, and the width of the event x == 4 (or each of the other values) is 0, so the integral at those points is 0.
Compare:
c2(x) = (dirac(x-1) + dirac(x-4) + dirac(x-7) + dirac(x-10) + dirac(x-13)) * 4
c2(x) = 

inner2 = c2(x) * exp(-2*(t-x));
y2(t) = simplify(int(inner2, x, 0, t))
y2(t) = 

string(y2)
ans = "4*heaviside(t - 1)*exp(-2*t)*exp(2) + 4*heaviside(t - 4)*exp(-2*t)*exp(8) + 4*heaviside(t - 7)*exp(-2*t)*exp(14) + 4*heaviside(t - 10)*exp(-2*t)*exp(20) + 4*heaviside(t - 13)*exp(-2*t)*exp(26)"
fplot(y2, [-20 20])

This defines c(x) in terms of a distribution rather than in terms of points, giving meaning to the integral at those values.
6 件のコメント
Dharma Khatiwada
2021 年 10 月 30 日
編集済み: Walter Roberson
2021 年 10 月 30 日
Thank you so much Walter! Your lower answer is something I was trying to do.
I need to insert the value of y2(t) in the loop below. How do I do that? lambda, b, d, e are all constant. Your suggestion will be greatly appreciated!
t=zeros(N,1);
V=zeros(N,1);
K=zeros(N,1);
%Run loop
% for j=1:N
t(1)=0;
V(1)=200;
K(1)=625;
for i=1:N
for j=1:N
t(i+1)=t(i)+dt;
%Using Euler forward methodfor dV/dt
V(i+1)=V(i)-lambda*dt*V(i)*log(V(i)/K(i));
%Using Euler forward method for dK/dt
K(i+1)=K(i)+b*dt*V(i)-d*dt*K(i)*(V(i).^(2/3))-e*dt*y2(t)*K(i);
end
end
Walter Roberson
2021 年 10 月 30 日
編集済み: Walter Roberson
2021 年 10 月 30 日
syms x t
c2(x) = (dirac(x-1) + dirac(x-4) + dirac(x-7) + dirac(x-10) + dirac(x-13)) * 4
inner2 = c2(x) * exp(-2*(t-x));
y2 = matlabFunction(simplify(int(inner2, x, 0, t)));
t=zeros(N,1);
V=zeros(N,1);
K=zeros(N,1);
%Run loop
% for j=1:N
t(1)=0;
V(1)=200;
K(1)=625;
for i=1:N
for j=1:N
t(i+1)=t(i)+dt;
%Using Euler forward methodfor dV/dt
V(i+1)=V(i)-lambda*dt*V(i)*log(V(i)/K(i));
%Using Euler forward method for dK/dt
K(i+1)=K(i)+b*dt*V(i)-d*dt*K(i)*(V(i).^(2/3))-e*dt*y2(t)*K(i);
end
end
Dharma Khatiwada
2021 年 10 月 30 日
Thank you again, Walter. Just a confusion with y2(t) in this line. It says 'left and right sides have different number of elements. Should I try y2(t(i))?
K(i+1)=K(i)+b*dt*V(i)-d*dt*K(i)*(V(i).^(2/3))-e*dt*y2(t)*K(i);
Walter Roberson
2021 年 10 月 30 日
Yes, t(i) would make sense in that context.
Dharma Khatiwada
2021 年 10 月 30 日
Hi Walter,
Just one more quick question with the value of c2(x) in the line:
c2(x) = (dirac(x-1) + dirac(x-4) + dirac(x-7) + dirac(x-10) + dirac(x-13)) * 4
Normally, we should expect dirac function giving the output i.e. infinity if for example, when x=1 otherwise zero. In my context, I am expecting that to be 4 i.e. c2(x)=4. I am not sure whether it is returning to that value. My concern is only on the value of c2(x).
I found something similar in other Matlab posting. Please suggest if I also need to normalize like below.
Thank you again!
x = -1:0.1:1;
y = dirac(x);
idx = y == Inf; % find Inf
y(idx) = 1; % set Inf to finite value
Walter Roberson
2021 年 10 月 31 日
dirac(0)
ans = Inf
syms x
int(dirac(x), x, -1, 1)
ans =
1
The Dirac Delta is not really a function. dirac(0) is not really inf . dirac() is defined such that the integral across 0 is 1. So what happens with int() of dirac is correct, and the Inf is not really correct.
There are different ways of defining Dirac. One of the ways is as the limit of a rectangle n units high and 1/n wide, as n approaches infinity: the area is fixed, but as the width approaches 0, the height approaches infinity. Saying that it is infinity such as dirac(0) shows, is a short-hand that is not really true.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
参考
2021 年 10 月 30 日
2021 年 10 月 31 日
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)
