Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Integrate this function

Subject: Integrate this function

From: Mostafa El-Hosseini

Date: 18 Oct, 2010 09:31:05

Message: 1 of 8

u1,u2,u3,q is a constant
and i want to integrate y from t = 0 to .2
and x is a seven coulmn vectors.

y = 5.8*(q*x(:,1)-6)-3.7*u1-4.1*u2-5*u3^2+q*(23*x(:,4)+11*x(:,5)...
    +28*x(:,6)+35*x(:,7))-0.099;

pls. help

Subject: Integrate this function

From: John D'Errico

Date: 18 Oct, 2010 10:28:05

Message: 2 of 8

"Mostafa El-Hosseini" <melhosseini@yahoo.com> wrote in message <i9h44p$2pr$1@fred.mathworks.com>...
> u1,u2,u3,q is a constant
> and i want to integrate y from t = 0 to .2
> and x is a seven coulmn vectors.
>
> y = 5.8*(q*x(:,1)-6)-3.7*u1-4.1*u2-5*u3^2+q*(23*x(:,4)+11*x(:,5)...
> +28*x(:,6)+35*x(:,7))-0.099;
>
> pls. help

Y is not a function of t. So the integral is trivial.
To integrate a constant, multiply by the length
of the interval, here 0.2.

I have no idea what you are asking, but perhaps
some thought on your part would help. Better
explanation would surely do so.

John

Subject: Integrate this function

From: Roger Stafford

Date: 18 Oct, 2010 18:39:04

Message: 3 of 8

"Mostafa El-Hosseini" <melhosseini@yahoo.com> wrote in message <i9h44p$2pr$1@fred.mathworks.com>...
> u1,u2,u3,q is a constant
> and i want to integrate y from t = 0 to .2
> and x is a seven coulmn vectors.
>
> y = 5.8*(q*x(:,1)-6)-3.7*u1-4.1*u2-5*u3^2+q*(23*x(:,4)+11*x(:,5)...
> +28*x(:,6)+35*x(:,7))-0.099;
>
> pls. help
- - - - - - - - - -
  I agree with John that your integration problem is not well-defined as you have stated it.

  Your expression for y seems to imply that y is a column vector with as many elements as the columns of matrix x. However, you have stated that you want "to integrate y from t = 0 to .2". That is what is said when an integration is taken with respect to a variable t. Yet you have not made it clear how t varies (or if it varies) along the columns of x. To find an integral with respect to a variable you must have an integrand which is a known function of that variable. That is what is missing in the explanation of your problem. State explicitly how y varies as t varies from 0 to .2 and you suddenly have a problem that makes sense.

Roger Stafford

Subject: Integrate this function

From: Mostafa El-Hosseini

Date: 18 Oct, 2010 22:09:04

Message: 4 of 8

sorry for that mistake
x is function of t, but i do not have the explicit form of x as a function of t, so i have only value of x at specified time slice,

- i have 7 1st order differential equations x1' - x2' .... x7' as a function of x1,x2,....., u1,u2,u3,q
and i used the ode45 solver to solve these differential equation
by using
          [t,x] = ode45(...........)
- so as i think here , x will be 7 coulmn evaluted at some t
- now, and that is the problem, i have a performance index that wanted to be maximized
 I = integrate (from 0 to 0.2) some function of x and u1,u2,u3 ,q

how can i integrate using quad

Note: i make it with trapz
note: if some thing confused, pls. give me your mail and i can further explain my problem

thank you

Subject: Integrate this function

From: Roger Stafford

Date: 19 Oct, 2010 03:06:03

Message: 5 of 8

"Mostafa El-Hosseini" <melhosseini@yahoo.com> wrote in message <i9igi0$ski$1@fred.mathworks.com>...
> sorry for that mistake
> x is function of t, but i do not have the explicit form of x as a function of t, so i have only value of x at specified time slice,
>
> - i have 7 1st order differential equations x1' - x2' .... x7' as a function of x1,x2,....., u1,u2,u3,q
> and i used the ode45 solver to solve these differential equation
> by using
> [t,x] = ode45(...........)
> - so as i think here , x will be 7 coulmn evaluted at some t
> - now, and that is the problem, i have a performance index that wanted to be maximized
> I = integrate (from 0 to 0.2) some function of x and u1,u2,u3 ,q
>
> how can i integrate using quad
>
> Note: i make it with trapz
> note: if some thing confused, pls. give me your mail and i can further explain my problem
>
> thank you
- - - - - - - - - -
  Mostafa, there is a fundamental difference between the 'trapz' quadrature function and the 'quad' type of quadrature functions.

  With 'trapz' you use a fixed, discrete set of points that give successive values of the variable of integration along with corresponding values of the integrand. The integration accuracy is dependent on the spacings in these discrete values. This would presumably be compatible with obtaining discrete solutions from 'ode' routines for computing the integrand provided the 'MaxStep' option is given a sufficiently fine setting.

  However with 'quad', 'quadl', 'quadgk', 'quadv', and 'quad2d, you are required to define or prepare a function that can receive a vector of values of the variable of integration - in your case t - and pass back a vector of the corresponding values of the desired integrand - y in your case. These values are chosen by the 'quad' routines, not the user, in an effort to obtain highly accurate results. The results obtained from 'ode' will inevitably be located differently from those requested by 'quad' and would require some kind of interpolation process to be performed at every call from 'quad', a most inconvenient procedure. In short I would say that the use of discrete 'ode' results is quite impractical for use with the 'quad' functions and its cousins.

  What I don't understand here is why you don't incorporate this integration process along with your system of seven differential equations and let the results be produced as an eighth variable along with the other seven. Your integral from 0 to 0.2 would the be the final value of this eighth variable. After all, the derivative with respect to t of this integral you are trying to solve, taken from 0 to a variable t, is simply the expression you have written for y. That is just one more differential equation in your system. It is a natural kind of task for differential equation-solving.

Roger Stafford

Subject: Integrate this function

From: Mostafa El-Hosseini

Date: 19 Oct, 2010 22:55:07

Message: 6 of 8

Dear Roger,

thank you again for your patience. Ok. I gree with you now that quad does not fit.

regarding incorporating the eighth equation into the solver, how can i make that especially i knew that ode45 solve differential equation not integral one.
really i have no idea about that, can you pls. help

i can even send you the .m file if you wish,

anyway i so grateful for your contribution

-- mostafa

Subject: Integrate this function

From: Roger Stafford

Date: 20 Oct, 2010 00:46:04

Message: 7 of 8

"Mostafa El-Hosseini" <melhosseini@yahoo.com> wrote in message <i9l7kb$i24$1@fred.mathworks.com>...
> Dear Roger,
>
> thank you again for your patience. Ok. I gree with you now that quad does not fit.
>
> regarding incorporating the eighth equation into the solver, how can i make that especially i knew that ode45 solve differential equation not integral one.
> really i have no idea about that, can you pls. help
>
> i can even send you the .m file if you wish,
>
> anyway i so grateful for your contribution
>
> -- mostafa
- - - - - - - - -
  You stated in your second article of this thread that you have a system of seven differential equations which you solved using 'ode45'. These would be of the form:

 dx1/dt = f1(x1,x2,x3,x4,x5,x6,x7)
 dx2/dt = f2(x1,x2,x3,x4,x5,x6,x7)
 .......
 dx7/dt = f7(x1,x2,x3,x4,x5,x6,x7)

with appropriate initial conditions. Since you are attempting to integrate y from t = 0 to t = 0.2, it can be assumed that this was also the t interval (tspan) of these differential equations.

  My proposal is that you extend this set of differential equations by adding one more equation which would be:

 dI/dt = y = 5.8*(q*x(:,1)-6)-3.7*u1-4.1*u2-5*u3^2+... etc.

and add the initial condition that 'I' be equal to 0 at the start. This would be an eighth differential equation. When 'ode45' has completed its task, besides having discrete values for the seven x's from t = 0 to t = 0.2, you will also have corresponding discrete values for I for these various values of t. Each of these values will be the integral of y from t = 0 up to the corresponding discrete value of t. The final value of I at t = 0.2 will give you the integral you wish to find. (The other values of I are a kind of gratuitous cumulative integral in case you are interested.)

  As you can perhaps see from this discussion, integration is one of the tasks that solving differential equations can accomplish. For example, suppose you want to find the integral of sin(x) from x = 0 to x = pi/2 (which we all know is 1.) If you define the variable y as

 y = int(sin(t),'t',0,x); %<-- integral of sin(t) w.r. to t, from t = 0 to t = x

then it is a fundamental theorem of calculus that dy/dx = sin(x). To find the value of y at x = pi/2 you could set up that single differential equation in 'ode45' starting with y = 0 at x = 0 and set your tspan to [0,pi/2]. The final value of y when 'ode45' finishes will be the desired integral, allowing of course for computation errors. You should try this experiment to see how close you come to 1.

  That reminds me. You should take care to set acceptable error tolerances with 'RelTol' and 'AbsTol' settings using 'odeset' when using 'ode45'. Its default settings may not be as accurate as you would like.

Roger Stafford

Subject: Integrate this function

From: Mostafa El-Hosseini

Date: 20 Oct, 2010 21:48:03

Message: 8 of 8

Ok. I code it now. and it works as just you said. regarding the accuracy
is it enough to write
options = odeset('AbsTol',1e-12,'RelTol',1e-12,'MaxStep',0.0001);

- i coded my application first using trapz, but i do not satisfied with the accuracy of that function, so i think in quad. but quad need certain assumption, so are now reached to put the integral part in the ODE solver.

now i have the same accuracy that i got before using trapz. in your opinion, making AbsTol, and RelTol as above is enough.


thank you

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us