# How to integrate using while loop?

9 views (last 30 days)

Show older comments

Hi all,

I have a function: PM =@(w) (5/16*3^2.*(0.5578^4./w.^5).*exp(-5/4.*(w./0.5578).^(-4)));

its integral: A=integral(PM, 0, 5);

I want to divide the area under the function into equal area pieces. Each beeing of a size= A/n, where n is any number desired. To achieve this I was trying to use while loop, but did not succed. Additionally, I need to keep trace at which values of w the condtion is met.

Thanks.

##### 0 Comments

### Accepted Answer

Mike Hosea
on 21 Oct 2014

Edited: Mike Hosea
on 21 Oct 2014

I'm assuming this is homework. Let me give you a couple of hints.

1. You can make a function like this

q = @(b)integral(PM,0,b);

Then, as you know,

A = q(5)

But you can make a useful function like this

g = @(b)q(b) - A/5

This function g is zero when integral(PM,0,b) is 1/5 of integral(PM,0,5). Then you might want to look at

g = @(b)q(b) - (2/5)*A.

That function is zero when integral(PM,0,b) = (2/5)*A. And so forth...

2. FZERO is a function that might interest you.

##### 3 Comments

### More Answers (3)

Roger Stafford
on 21 Oct 2014

Actually, Andrejus, if you make a change of variable from w to x according to:

w = x^(-1/4),

you will see that your problem is suddenly converted into a much simpler problem that requires neither numerical integration nor the use of 'fzero'. Ordinary calculus will do the job.

##### 3 Comments

Mike Hosea
on 22 Oct 2014

Roger Stafford
on 22 Oct 2014

In order to show that your problem need not involve time-consuming iterative methods here is the complete solution in four lines:

k1 = 5/16*3^2*(0.5578)^4;

k2 = 5/4*(0.5578)^4;

A = k1/k2/4*exp(-k2/5^4); % The total integral (area)

w = (1/5^4-log((1:n)'/n)/k2).^(-1/4); % Upper edges of equal area intervals

The justification of this code comes 1) from using 'int' (or ordinary calculus) to establish that the integral from 0 to t of k1/x^5*exp(-k2/x^4) is simply

k1/k2/4*exp(-k2/t^4)

and 2) that the solution to choosing intervals that divide this into n equal parts, that is, the w(m) such that

m/n = (k1/k2/4*exp(-k2/w(m)^4))/(k1/k2/4*exp(-k2/5^4))

is

w(m) = (1/5^4-log(m/n)/k2).^(-1/4)

by using either 'solve' or ordinary algebra.

Roger Stafford
on 24 Oct 2014

Strictly speaking, this is an answer to a question you haven't asked, Andrejus, but I thought you might be interested in the fact that your request "to divide the area under the function into equal area pieces" is almost the equivalent of asking to fill the space below the curve with random points uniformly distributed area-wise (in a statistical sense.) Because the inverse of the integral of your function is easy to solve for, this can be done with the following code, which is almost identical to the solution I gave earlier to your original request. Ignoring the y values, this is also the solution to generating a random variable, x, whose probability density function (pdf) is the integrand you gave (after being normalized by being divided by the total area.)

k1 = 5/16*3^2*(0.5578)^4; % = 0.27227425026348

k2 = 5/4*(0.5578)^4; % = 0.12101077789488

n = 5000;

x = (1/5^4-log(rand(n,1))/k2).^(-1/4); % <-- The random (x,y) points

y = k1./x.^5.*exp(-k2./x.^4).*rand(n,1);

X = linspace(1e-10,5,500); % <-- The (X,Y) points in the curve above

Y = k1./X.^5.*exp(-k2./X.^4);

plot(X,Y,'r-',x,y,'y.')

##### 2 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!