MATLAB Answers

how find exact value q for integral on partition interval?

10 views (last 30 days)
work wolf
work wolf on 4 May 2016
Edited: work wolf on 6 May 2016
i want find exact value for q(i)
let f(x,y)
x = s : h : p ;
i = 2 : n-1 ;
q ( i ) = integral ( f( x , y ( i ) ) , x( i-1 ), x( i+1 ) ).
for example:
q=integral(x)dx on interval 0:2:8
output
q1=2,
q2=6,
q3=10,
q4=14. please i attached it

Accepted Answer

John BG
John BG on 6 May 2016
This question is simple, but not as simple as it looks at first sight.
the function x you have defined
x=[0:2:8]
has a reference vector
nx=[1:5]
your attempt to integrate beyond 5 does not work unless the reference vector of the function covers such interval.
To get the integration results q1=2 q2=6 q3=10 q4=14 the function you should integrate could be
x=2*ceil([1:8]/2)
and reference vector
xn=0:1:8
the problem here is that MATLAB interpolates the following way
stair01
despite showing an apparently staired stem plot
stair02
and the integration results along the intervals are biased with constant 2:
quad(x,0,2)
=
4.00
quad(x,2,4)
=
8.00
quad(x,4,6)
=
12.00
quad(x,6,8)
=
16.00
to solve the excessive interpolation that MATLAB performs by default, here linear interpolation is excessive, you may want to do the following:
1.- increase resolution
nx2=0:0.01:8
2.- be aware that if you define the following function
function y=func1(n)
% generate stair with even integers only
n2=floor(n)
if n2<1
y=0
return
end
if(~mod(n2,2)) % even
y=n2
else % odd
y=n2+1
end
end
it seems to work but it does not. If you try
func1([1:1:8])
n2 =
1 2 3 4 5 6 7 8
y =
2 3 4 5 6 7 8 9
ans =
2 3 4 5 6 7 8 9
despite one by one
func1(0.3)
=
0
func1(1)
=
2
func1(2.1)
=
2
func(3.1)
=
2
func(3.9)
=
2
func(4.6)
=
4
..
func1 seems to work fine with scalars but not with vectors. You need custom defined functions to work correctly with vectors to pass the handle to the integrating function quad or integral.
if you try quad(@func1,a,b) with the previous function func1 on the sought intervals the results are wrong.
4.- So, to generate the values of the function to integrate correctly you have to introduce the following loop in the function that calculates the stair:
for k=1:1:length(nx)
y(k)=func1(nx(k));
end
now if you plot y you get the right function:
just did it, use the following function
function yn=func1(n)
n2=floor(n)
yn=zeros(1,length(n))
for k=1:1:length(n)
if(~mod(n2(k),2)) % even
yn(k)=n2(k)
else
yn(k)=n2(k)+1
end
end
end
now you get the right results:
n=[0:.1:8]
quad(@func1,0,2)
=
2.000008674804688
quad(@func1,2,4)
=
6.000008674804688
quad(@func1,4,6)
=
10.000008674804688
quad(@func1,6,8)
=
14.000008674804688
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by