### Tags: astriangular, calculate, firstly, friends, function, integral, integration, matlab, piecewise, plz, programming, rectangular

# problems about piecewise function and integration

On Programmer » Matlab

6,141 words with 5 Comments; publish: Sat, 10 May 2008 01:48:00 GMT; (20046.88, « »)

Hello my friends,

I am trying to calculate the integral of piecewise function, such as

triangular function and rectangular function. Firstly, plz tell me

whether have any functions in matlab can define piecewise expression

directly' it means no if-end, for-end command

triangular function: when |x|>=1 y=0; when |x|<1 y=1-|x|

my function : 1-min(abs(x),1) % I am not sure it is a valid expression

or equation

when it move, 1-min(abs(x+1),1)

x=linspace(-5,5,500);

plot(x,1-min(abs(x),1)) % get a perfect triangular function graphics

ans =

1.0000

ans =

1.0000

####################################[col

or=darkred]

ans =

0

ans =

1.0000

ans =

0

ans =

0.0208

anyone can tell me what wrong with quad and my function ' or how to

calculate tir function,rect function or integral of

f(x)*f(x+T)*exp(-i*pi*f) dx from -inf to inf

thanks a looooot!!

*http://matlab.todaysummary.com/q_matlab_48222.html*

All Comments

Leave a comment...

- 5 Comments
- badaonanhai wrote:
>

> Hello my friends,

> I am trying to calculate the integral of piecewise function, such

> as

> triangular function and rectangular function. Firstly, plz tell me

> whether have any functions in matlab can define piecewise

> expression

> directly' it means no if-end, for-end command

> triangular function: when |x|>=1 y=0; when |x|<1 y=1-|x|

> my function : 1-min(abs(x),1) % I am not sure it is a valid

> expression

> or equation

> when it move, 1-min(abs(x+1),1)

> x=linspace(-5,5,500);

> plot(x,1-min(abs(x),1)) % get a perfect triangular function

> graphics

>

Numerical quadrature tools do not actually

understand anything about your function.

They sample it at some selected points

and use those values to predict a value

for the integral.

Now, what do you think will happen if

at EVERY single point that is sampled,

the function has a value of zero? Can

you guess which value will be chosen

for the integral? At SOME point, one

must decide that the function is itself

identically zero if at every value that

you sample, you get zero.

Next, beware integrating functions that

are not differentiable using am adaptive

rule like quad. They will spend a bit

of extra time just finding those points

where the function has those nasty

little slope discontiuities. Try this:

fun = .matlab.todaysummary.com.(x) 1-min(abs(x),1);

quad(fun,[-5,4],[],1)

Note all the points where fun is sampled

near the break points.

Finally, while your code for the

piecewise function will work, a more

general function will not be as easy

to define. You can define a fully

general piecewise function using my

piecewise_eval.

<http://www.mathworks.com/matlabcent...>

fun = .matlab.todaysummary.com.(x) piecewise_eval(x,[-1 0 1],...

{0, '1+x', '1-x', 0});

quad(fun,-5,4)

ans =

1

HTH,

John D'Errico

#1; Sat, 10 May 2008 01:49:00 GMT

- badaonanhai wrote:
- "John D'Errico" <woodchips.matlab.todaysummary.com.rochester.rr.com> wrote in message
news:ef44a5f.0.matlab.todaysummary.com.webcrossing.raydaftYaTP...

> badaonanhai wrote:

*snip*

> Finally, while your code for the

> piecewise function will work, a more

> general function will not be as easy

> to define. You can define a fully

> general piecewise function using my

> piecewise_eval.

> <http://www.mathworks.com/matlabcent...

> fun = .matlab.todaysummary.com.(x) piecewise_eval(x,[-1 0 1],...

> {0, '1+x', '1-x', 0});

In addition to John's piecewise_eval, you can do this using logical

indexing, a powerful programming technique that should serve you well in

other applications, not just piecewise function evaluation.

function y = myfunction(x)

% Preallocate y

y = zeros(size(x));

% Find where abs(x) < 1

C1 = abs(x) < 1;

% C1 is a logical array; we can use it to index into x and y

y(C1) = 1-abs(x(C1));

% This next step is unnecessary in this example, given the fact that we

preallocated

% y to contain zeros and anywhere that abs(x) > 1, y should be zero

% However, for the sake of the example, I'll include it.

C2 = abs(x) >= 1;

y(C2) = 0; % using scalar expansion

Now try it.

x = -2:0.1:2;

y = myfunction(x);

plot(x, y);

Steve Lord

slord.matlab.todaysummary.com.mathworks.com

#2; Sat, 10 May 2008 01:50:00 GMT

- "John D'Errico" <woodchips.matlab.todaysummary.com.rochester.rr.com> wrote in message
- IMHO this is the most convenient way of defining piecewises:
f = .matlab.todaysummary.com.(x) (-1<x&x<0).*(1+x)+(0<=x&x<1).*(1-x)

#3; Sat, 10 May 2008 01:52:00 GMT

- IMHO this is the most convenient way of defining piecewises:
- Yuryi Gribov wrote:
>

> IMHO this is the most convenient way of defining piecewises:

> f = .matlab.todaysummary.com.(x) (-1<x&x<0).*(1+x)+(0<=x&x<1).*(1-x)

This solution has one serious flaw.

Suppose you have a function piece that

generates an inf or a NaN for negative

numbers.

The expression evaluates all function

pieces for all values of the input,

then combines them additively. BUT,

the NaNs propagate.

Its just as bad if your function piece

evaluates as an inf for some segment,

because inf*0 becomes a NaN.

So define your function as

f(x) = NaN for x<0,

f(x) = x for x>=0

See what happens using the style

recommended above:

f = .matlab.todaysummary.com.(x) (x<0).*(NaN)+(0<=x).*(x);

f(1)

ans =

NaN

Or for the inf case,

g = .matlab.todaysummary.com.(x) (x<0).*(inf)+(0<=x).*(x);

g(1)

ans =

NaN

Gosh, I wanted f(1) = g(1) = 1 for

both functions.

Finally, the style of evaluating all

pieces for all values will be less

efficient when some of the pieces are

rather expensive to evaluate.

John

#4; Sat, 10 May 2008 01:53:00 GMT

- Yuryi Gribov wrote:
- Agreed. Of course I was talking about smth like _/\_#5; Sat, 10 May 2008 01:54:00 GMT

- Agreed. Of course I was talking about smth like _/\_