how can I integrate this function (1/(1+x.^2)) in matlab

I used many numerical integration methods like integral simpson trapoizal int but the graph is different and not like the one already I have so can I know please how to do this in matlab the function is 1/(1+x.^2) intervals of this integration contains variable called t takes values from [-15:15] now intervals from [ -t^1/2, Inf]

Answers (3)

Hi,
use integral function for this:
fun = @(x) 1./(1+x.^2)
sol = integral(fun,-15,15)
This gives you the numeric solution in the bounds [-15, 15].
An alternative solution (if you have access to symbolic toolbox) is:
syms x
fun = 1/(1+x^2)
sol_1 = int(fun)
sol_2 = int(fun,0,inf)
sol_3 = int(fun,-15,15)
Best regards
Stephan

11 Comments

This is what I wrote on matlab
syms x
fun=1./(1+(u).^(2));
sol1=int(fun)
for t=-15:5:15
sol2=int(fun,t.^(-1/2),Inf)
end
Is there anything wrong
Use x not u:
syms x
fun=1./(1+(x).^(2));
sol1=int(fun)
for t=-15:5:15
sol2=int(fun,t.^(-1/2),Inf)
end
You are overwriting all of sol2 each iteration through the loop.
syms x fun(u)
fun(u) = 1./(1+(u).^(2));
sol1 = int(fun)
for tidx = 1 : 7
t = (tidx-4) * 5;
sol2(tidx) = int(fun, t.^(-1/2), Inf);
end
sol2
You will find that the first several entries come out 0. That is because you are trying to integrate over a complex range without having specified a path to follow. (At the moment I do not find a way to specify the path for a symbolic complex line integral; for numeric integrals with integrate() you specify it using the Waypoints option.)
Thank you Walter for the clarification.
Thanks all for your help but another question if I got the answer from this integration and want to plot it what I have to do with the complex values . for your answer I don't know what is the waypoint
vpaintegral supports waypoints:
Unrtil now i also did not hear from this topic - im highly interested too
sorry for this question but why Matlab didn't recognize vpaintegral
There is a closed form solution for t < 0, which is
-1i*atanh(1/sqrt(-t))
This will be complex valued. For t more negative than -1, this will be purely complex, with real part 0; for t greater than -1 to 0, this will have a mix of real and complex parts. At t = -1 itself, it is undefined, being -Inf*1i from the left and - Inf*1i - pi/2 from the right.
There are multiple ways of plotting complex values. Sometimes people plot two lines on the same 2D graph, one line representing the real part and the other line representing the imaginary part. Other times, people would instead switch to a 3D graph, with t on one axis, real() on a second axis, and imag() on a third axis.
@Walter did you get this solution by using matlab? How did you find it?
vpaintegral() was added to the Symbolic Toolbox in R2016b.

Sign in to comment.

Thanks for your answer but my interval is [ -t^(1/2),Inf] and (t) is a variable takes values from -15 to 15
The answer I got conatins some complex numbers so how can I treat this.

1 Comment

You cannot avoid that. You have t = -15 to +15 and you want to integrate over -t^(1/2) to infinity, but when t is negative, t^(1/2) is complex.

Sign in to comment.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!