Find integral of a function where upper limit changes inside the function
显示 更早的评论
I want to calculate integral of in equation 12 in image.

When t=30,D=250, x=40., I have found it with following code
t = 30
a=@(g) ((t-g).^(-1.5)).*exp(-4./(t-g));
b=integral(a,0,t);
h=282.09.*b
But I am unable to do it when t is a vector say t = 1:30, it means I want to find out h at every t from 1 to 30.
采纳的回答
for t = 1:1:30
a=@(g) ((t-g).^(-1.5)).*exp(-4./(t-g));
b=integral(a,0,t);
h(t)=282.09.*b
end
12 个评论
You would need
h(t)=282.09.*b
to return all of the values.
Exactly, I forgot to mention it.
@cvklpstunc thank you for answer. As I also want to change g for every step. I have written following code for that..
p = 30; % number of steps
t = 1:30;
gt = sin(linspace(0,20,p));
D = 100; x = 40;
out_of_integral = sqrt(1/D)*x / (2*sqrt(pi));
fun = @(tau,gt,t) gt*(t-tau).^(-1.5).*exp(-x^2./(4*D*(t-tau)));
h = zeros(1,p);
for i = 1:p
q = integral(@(tau)fun(tau,gt(i),t(i)),0,t(i));
h(i) = q*out_of_integral;
end
Can you please see if it is correct? I have no means to validate my code when both g and t change for every timestep.
There seems nothing wrong with indexing or anything else.
@cvklpstunc The red line in image shows the result from this code. It shows temperture at x=30, with time.

What I am unable to understand is, why output amplitude keeps on increasing while the input amplidue is constant. I expect output amplitude to be more or less constant as seen from the result of finite difference code here (yellow line).
The red line shows the result from above code.
In your integral, for given time t(i), gt is constant over [0,t(i)], namely gt(i). That's wrong because gt varies with tau.
Best wishes
Torsten.
@Torsten Can you please make the correction i.e how can I make g as a function of tau?
D = 100;
x = 40;
t = 1:30;
g = @(tau) sin(tau);
fun = @(tau,t) g(tau).*(t-tau).^(-1.5).*exp(-x^2./(4*D*(t-tau)));
for i=1:numel(t)
tscal=t(i);
q(i) = integral(@(tau)fun(tau,tscal),0,tscal);
end
q = sqrt(1/D)*x/(2*sqrt(pi))*q;
plot(t,q)
Best wishes
Torsten.
@Torsten I fear the solution is stil not correct. Have you looked at the output from your code, it looks like this

This is the solution of 1D heat equation and the result should similar to as yellow line I pasted above. If the input is sine wave at x=0, then I should have a sine wave with constant but lower amplitude at x=40 and a shifted phase as compared to sine wave at x=0.
I don't see anything wrong in the code.
Maybe you could try plotting q on a finer and longer t-grid (till t=60, e.g.) and strengthen the tolerances RelTol and AbsTol for the integrator.
I get a reasonable result this way.
Best wishes
Torsten.
@Torsten may be I am getting nothing at all. How can I come from the graph from your code to the target graph which I mentioned before where I have input sine wave g(t) (black line) and output (yellow line)?
Change
x = 40;
t = 1:30;
g = @(tau) sin(tau);
to
x = 30;
t = linspace(0.05,100,2000);
g = @(tau) sin(tau/2);
in the above code.
Best wishes
Torsten.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
另请参阅
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)
