# How to write a code for finding a definite integral for ( 1/x(t).^2 ) please?

6 views (last 30 days)
Avan Al-Saffar on 13 Nov 2014
Commented: Roger Stafford on 13 Nov 2014
My system is :
dx/dt = 2*x
1- I solved numerically using ode45.
2- Depending on the values that I got from ode45, I want to find this integral :
Integral ( 1/ x(t).^2 ) dt from 0 to 10
This is my code but I do not know if my result is correct or not,, how could I check that please?
function RunAvan
x0=0.1;
tspan=[0:0.1:10];
A=1;
[t,x]=ode45(@Avan,tspan,x0);
figure(1)
plot(t,x)
f = @(x) (A./x.^2)
% I5=integral(f,1,101,'ArrayValued',true)
Q=[Q1./20 Q2./40 Q3./60 Q4./80 Q5./101]
B=[20 40 60 80 101]
figure(2)
plot(B,Q)
X = @(T) interp1(t,x,T);
f1 = @(T) (A./X(T).^2)
J1=integral(f1,0,2,'ArrayValued',true)
J2=integral(f1,0,4,'ArrayValued',true)
J3=integral(f1,0,6,'ArrayValued',true)
J4=integral(f1,0,8,'ArrayValued',true)
J5=integral(f1,0,10,'ArrayValued',true)
J=[J1./2 J2./4 J3./6 J4./8 J5./10]
R=[2 4 6 8 10]
figure(4)
plot(R,J)
1;
Note : The first time I integrated with respect to x and the second time with respect to t.
Torsten on 13 Nov 2014
Are the functions involved only test functions ?
Or why do you use ode45, quad and integral instead of the analytical solutions in all cases ?
Best wishes
Torsten.

Roger Stafford on 13 Nov 2014
You can check it by comparing its results with the exact mathematical solution to your differential equation. The solution to dx/dt = 2*x is
x = K*exp(2*t)
where K can be determined by the initial condition. In your case that would be K = 0.1 . Taking the integral of 1/x^2 from 0 to u would give you
int(1/(K*exp(2*t))^2,'t',0,u) =
1/K^2*int(exp(-4*t),'t',0,u) =
100*(-exp(-4*u)/4+exp(-4*0)/4) =
25*(1-exp(-4*u))
where u is whatever upper limit you wish to use. Note that for the larger values of u your answer will be very close to 25.
Roger Stafford on 13 Nov 2014
My use of 'int' was meant only in place of the integral symbol and not to be used with matlab. For example
int(1/(K*exp(2*t))^2,'t',0,u)
meant only the integral of (K*exp(2*t))^2 with respect to t from t = 0 to t = u.