# How to integrate using while loop?

33 views (last 30 days)
Andrejus on 21 Oct 2014
Commented: Torsten on 3 Jan 2017
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.

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.
Andrejus on 23 Oct 2014
Yey it works just as I wanted. Thank you very much!

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.
Mike Hosea on 22 Oct 2014
Let me say at the outset that you're probably right that this is a calculus homework exercise and not a MATLAB programming homework exercise. As a numerical analyst whose use of MATLAB goes back to the FORTRAN version, I do marvel that the Symbolic Toolbox is considered "purely MATLAB" around here. Whether it is or isn't is semantics. When I say "MATLAB", however, it means numerical. I've engineered plenty of test cases for numerical algorithms starting from a solution and then working "backwards" to arrive at a test problem. It's a way of engineering problems that I have an exact answer to for comparison purposes.

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.
Andrejus on 23 Oct 2014
Thank you for you attempt to help me out. I am sure that your approach is just as good as Mike's but this time I could get his idea faster. Thank you once again!

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 CommentsShowHide 1 older comment
Torsten on 3 Jan 2017
A=integral(PM,0,k)
A=@(k)integral(PM,0,k)
Best wishes
Torsten.