# Matlab Integration problem

• MATLAB
• dRic2
In summary, the function "system" finds the value of ##x_D## by integrating the function "fun" numerically.

#### dRic2

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:
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?

tnich said:
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.

tnich said:
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".

dRic2 said:
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);

Sorry, my mistake. I edited the original post

dRic2 said:
Sorry, my mistake. I edited the original post
The two versions of the integral still don't look the same.

Sorry 2.0 :-D

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

dRic2 said:
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?

dRic2 said:
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

dRic2 said:
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.

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##

dRic2 said:
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.

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

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

yes

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

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

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

## What is a Matlab integration problem?

A Matlab integration problem is a mathematical problem that involves calculating the area under a curve, also known as integration, using the software program Matlab. This type of problem is commonly encountered in fields such as engineering, physics, and statistics.

## How do I solve a Matlab integration problem?

To solve a Matlab integration problem, you will need to use a combination of Matlab functions and techniques. One common method is to use the built-in function trapz, which approximates the area under a curve using trapezoids. Other methods include using the integral function or creating your own custom integration algorithm.

## What are some common challenges when solving Matlab integration problems?

Some common challenges when solving Matlab integration problems include selecting the appropriate integration method, determining the correct limits of integration, and dealing with complex or multi-dimensional functions. It is also important to consider the accuracy and precision of your solution, as numerical integration methods can introduce errors.

## Can I use Matlab to solve non-numerical integration problems?

Yes, Matlab can also be used to solve non-numerical integration problems, such as finding the area under a curve represented by a symbolic function. In these cases, you will need to use the int function, which takes a symbolic expression as input and returns the symbolic integral.

## Are there any resources available to help me with Matlab integration problems?

Yes, there are many resources available to help you with Matlab integration problems. These include online tutorials, textbooks, and community forums where you can ask for help and advice. Additionally, Matlab has a comprehensive documentation and help system that can provide guidance on specific integration functions and techniques.