# Matlab Integration problem

• MATLAB
Gold Member
Here's what I have to do:

1) I arbitrary give a first value to the variable ##x_b##. Let's say ##x^{(0)}_b = 0.3##
2) I find ##x_D## by evaluating this integral:

$$ln(\frac {52.32} {100}) = \int_{0.5}^{x^{(0)}_b} \frac {dx} {x^{(0)}_D - x}$$

3) I use the value I got for ## x^{(0)}_D## to evaluate ##y^{(0)}_n##

$$y^{(0)}_n = f(x^{(0)}_D, x^{(0)}_b)$$

4) I check if ##x^{(0)}_D - y^{(0)}_n = 0## then I stop, otherwise I have to choose an other ##x^{(1)}## and start iterating again.

In matlab:

Code:
function out = my_int(xD, xB)

fun = @(xD) 1./(xD - x)
out = log(52.32/100) - integral(fun, 0.5, xB);

end

function out = system(xb)

find_xD = @(xD_) my_int(xD_, xb);
xD = fzero(find_xD, 0.7);

% other lines of the code
% where I calculate y_n

out = y_n - xD;

end

and I call this with:

Code:
fsolve(@system, 0.3);

I keep getting a warning that the integral my not exist and fsolve won't start...

Any help?

Last edited:

tnich
Homework Helper
Some questions to consider:
Why are you integrating numerically when you can get an analytic solution for your integral?
Is the function you want to integrate the one you have coded?

Gold Member
Why are you integrating numerically when you can get an analytic solution for your integral?

At first I just integrated but the result was slightly off (0.2 instead of 0.25). Then I realized that ##x_D## depends on the value of ##x_b##... so I guess I have to integrate numerically.

Is the function you want to integrate the one you have coded?

yes. The function "my_int" represents the integral minus ##ln(52.32/100)##. I call "my_int" with fzero in order to get the value of ##x_D## then I procede with the algorithm inside the function "system".

tnich
Homework Helper
yes. The function "my_int" represents the integral minus ##ln(52.32/100)##. I call "my_int" with fzero in order to get the value of ##x_D## then I procede with the algorithm inside the function "system".
ln(\frac {52.32} {100})
- \int_{0.5}^{x^{(0)}_b} \frac x {x^{(0)}_D - x} dx## does not look like the same thing as
fun = @(xD) 1./(xD - xB)
out = log(52.32/100) - integral(fun, 0.5, xB);

Gold Member
Sorry, my mistake. I edited the original post

tnich
Homework Helper
Sorry, my mistake. I edited the original post
The two versions of the integral still don't look the same.

Gold Member
Sorry 2.0 :-D

(this errors are only in my post, but not in the real program. I apologize)

tnich
Homework Helper
Sorry 2.0 :-D

(this errors are only in my post, but not in the real program. I apologize)
Once again I ask, why not just use the calculus to do your integration?

Gold Member
At first I just integrated but the result was slightly off (0.2 instead of 0.25). Then I realized that xDxDx_D depends on the value of xbxbx_b... so I guess I have to integrate numerically.

It's kind hard to explain but I think it is not possible because ##x_D## depends on ##x_b## i a very complicated way

tnich
Homework Helper
It's kind hard to explain but I think it is not possible because ##x_D## depends on ##x_b## i a very complicated way
I think that for your own benefit you really need to be clear on what problem you are trying to solve. Your code does not work. It does not work because it is incorrect. It is incorrect, at least in part, because you are not clear on what problem you are trying to solve.

I suggest that you try writing out a description of your problem. Define the inputs (what do ##\frac {52.32} {100}##, 0.5, and ##x_b## represent?), the outputs (what does ##x_D## represent?), and write out the equations that relate the inputs to the outputs. Until you do that, you have no way of knowing if your code is correct, or if the answer it gives you is the answer you are looking for.

I suspect that if you could write out a clear statement of your problem, you could solve it with pencil and paper and a calculator in a few minutes.

Gold Member
I have to solve this set of equations (##\alpha = 2.499##):

## y_4 = x_D ##

## x_4 = \frac {y_4} {\alpha-y_4*(\alpha-1)}##
## y_3 = \frac R {R+1} x_4 + \frac {x_D} {R+1}##
## x_3 = \frac {y_3} {\alpha-y_3*(\alpha-1)}##
## y_2 = \frac R {R+1} x_3 + \frac {x_D} {R+1}##
## x_2 = \frac {y_2} {\alpha-y_2*(\alpha-1)}##
## y_1 = \frac R {R+1} x_2 + \frac {x_D} {R+1}##
## x_1 = \frac {y_1} {\alpha-y_1*(\alpha-1)}##

##x_b = x_1##

##ln(\frac {52.32}{100}) = \int^{x_b}_{0.5}\frac {dx} {x_D - x}##

In particular I need the value of ##x_b## and ##x_D##.

In order to do that I wrote this code (I made some corrections to the one I posted earlier):

Code:
function out = my_int(xD, xB)

fun = @(xD) 1./(xD - x)
out = log(52.32/100) - integral(fun, 0.5, xB);

end

function out = system(xb)

find_xD = @(xD_) my_int(xD_, xb);
xD = fzero(find_xD, 0.7);

y4=xD;
x4=y4/(alpha-y4*(alpha-1));
y3=(R/(R+1))*x4+xD/(R+1);
x3=y3/(alpha-y3*(alpha-1));
y2=(R/(R+1))*x3+xD/(R+1);
x2=y2/(alpha-y2*(alpha-1));
y1=(R/(R+1))*x2+xD/(R+1);
x1=y1/(alpha-y1*(alpha-1)));

out = x1-xb;

end

%**********************  Calling the function "system" with
fsolve(@system, 0.3);

##x_b## should be around ##0.25## and ##x_D≈0.7##

tnich
Homework Helper
I have to solve this set of equations (##\alpha = 2.499##):

## y_4 = x_D ##

## x_4 = \frac {y_4} {\alpha-y_4*(\alpha-1)}##
## y_3 = \frac R {R+1} x_4 + \frac {x_D} {R+1}##
## x_3 = \frac {y_3} {\alpha-y_3*(\alpha-1)}##
## y_2 = \frac R {R+1} x_3 + \frac {x_D} {R+1}##
## x_2 = \frac {y_2} {\alpha-y_2*(\alpha-1)}##
## y_1 = \frac R {R+1} x_2 + \frac {x_D} {R+1}##
## x_1 = \frac {y_1} {\alpha-y_1*(\alpha-1)}##

##x_b = x_1##

##ln(\frac {52.32}{100}) = \int^{x_b}_{0.5}\frac {dx} {x_D - x}##

In particular I need the value of ##x_b## and ##x_D##.
It looks like you want to find a value of ##x_D## such that when you set ##y = x_D## and iterate these two equations
## x = \frac {y} {\alpha-y*(\alpha-1)}##
## y = \frac R {R+1} x + \frac {x_D} {R+1}##

several times to converge on a value of ##x_b##, your values of ##x_D## and ##x_b## satisfy
##ln(\frac {52.32}{100}) = \int^{x_b}_{0.5}\frac {dx} {x_D - x}##

May I ask why you do the four iterations of the first two equations? Is it to get a good approximate value of x_b, or is it the value after exactly four iterations that you want?

What does the final equation represent? It looks like you want the ratio of ##\frac {x_D -0.5} {x_D - x_b}## to be 52.32%.

It looks like you will need to have a value for R also.

Gold Member
Sorry for the late reply, I'm super busy right now.

is it the value after exactly four iterations that you want?

yes

It looks like you will need to have a value for R also.

yes, I forgot. I know the value for R.

What does the final equation represent?/QUOTE]
I really don't know how to explain (unless you are familiar with chemical distillation columns )