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.

 Accepted Answer

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 Comments

You would need
h(t)=282.09.*b
to return all of the values.
@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.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 5 Nov 2017

Edited:

on 7 Nov 2017

Community Treasure Hunt

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

Start Hunting!