http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143
MATLAB Central Newsreader  Integrate this function
Feed for thread: Integrate this function
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 18 Oct 2010 09:31:05 +0000
Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#788598
Mostafa ElHosseini
u1,u2,u3,q is a constant<br>
and i want to integrate y from t = 0 to .2 <br>
and x is a seven coulmn vectors.<br>
<br>
y = 5.8*(q*x(:,1)6)3.7*u14.1*u25*u3^2+q*(23*x(:,4)+11*x(:,5)...<br>
+28*x(:,6)+35*x(:,7))0.099;<br>
<br>
pls. help

Mon, 18 Oct 2010 10:28:05 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#788612
John D'Errico
"Mostafa ElHosseini" <melhosseini@yahoo.com> wrote in message <i9h44p$2pr$1@fred.mathworks.com>...<br>
> u1,u2,u3,q is a constant<br>
> and i want to integrate y from t = 0 to .2 <br>
> and x is a seven coulmn vectors.<br>
> <br>
> y = 5.8*(q*x(:,1)6)3.7*u14.1*u25*u3^2+q*(23*x(:,4)+11*x(:,5)...<br>
> +28*x(:,6)+35*x(:,7))0.099;<br>
> <br>
> pls. help<br>
<br>
Y is not a function of t. So the integral is trivial.<br>
To integrate a constant, multiply by the length<br>
of the interval, here 0.2. <br>
<br>
I have no idea what you are asking, but perhaps<br>
some thought on your part would help. Better<br>
explanation would surely do so.<br>
<br>
John

Mon, 18 Oct 2010 18:39:04 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#788751
Roger Stafford
"Mostafa ElHosseini" <melhosseini@yahoo.com> wrote in message <i9h44p$2pr$1@fred.mathworks.com>...<br>
> u1,u2,u3,q is a constant<br>
> and i want to integrate y from t = 0 to .2 <br>
> and x is a seven coulmn vectors.<br>
> <br>
> y = 5.8*(q*x(:,1)6)3.7*u14.1*u25*u3^2+q*(23*x(:,4)+11*x(:,5)...<br>
> +28*x(:,6)+35*x(:,7))0.099;<br>
> <br>
> pls. help<br>
         <br>
I agree with John that your integration problem is not welldefined as you have stated it.<br>
<br>
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.<br>
<br>
Roger Stafford

Mon, 18 Oct 2010 22:09:04 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#788807
Mostafa ElHosseini
sorry for that mistake<br>
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, <br>
<br>
 i have 7 1st order differential equations x1'  x2' .... x7' as a function of x1,x2,....., u1,u2,u3,q<br>
and i used the ode45 solver to solve these differential equation<br>
by using <br>
[t,x] = ode45(...........)<br>
 so as i think here , x will be 7 coulmn evaluted at some t<br>
 now, and that is the problem, i have a performance index that wanted to be maximized<br>
I = integrate (from 0 to 0.2) some function of x and u1,u2,u3 ,q<br>
<br>
how can i integrate using quad<br>
<br>
Note: i make it with trapz<br>
note: if some thing confused, pls. give me your mail and i can further explain my problem<br>
<br>
thank you

Tue, 19 Oct 2010 03:06:03 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#788841
Roger Stafford
"Mostafa ElHosseini" <melhosseini@yahoo.com> wrote in message <i9igi0$ski$1@fred.mathworks.com>...<br>
> sorry for that mistake<br>
> 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, <br>
> <br>
>  i have 7 1st order differential equations x1'  x2' .... x7' as a function of x1,x2,....., u1,u2,u3,q<br>
> and i used the ode45 solver to solve these differential equation<br>
> by using <br>
> [t,x] = ode45(...........)<br>
>  so as i think here , x will be 7 coulmn evaluted at some t<br>
>  now, and that is the problem, i have a performance index that wanted to be maximized<br>
> I = integrate (from 0 to 0.2) some function of x and u1,u2,u3 ,q<br>
> <br>
> how can i integrate using quad<br>
> <br>
> Note: i make it with trapz<br>
> note: if some thing confused, pls. give me your mail and i can further explain my problem<br>
> <br>
> thank you<br>
         <br>
Mostafa, there is a fundamental difference between the 'trapz' quadrature function and the 'quad' type of quadrature functions.<br>
<br>
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.<br>
<br>
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.<br>
<br>
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 equationsolving.<br>
<br>
Roger Stafford

Tue, 19 Oct 2010 22:55:07 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#789037
Mostafa ElHosseini
Dear Roger, <br>
<br>
thank you again for your patience. Ok. I gree with you now that quad does not fit.<br>
<br>
regarding incorporating the eighth equation into the solver, how can i make that especially i knew that ode45 solve differential equation not integral one.<br>
really i have no idea about that, can you pls. help<br>
<br>
i can even send you the .m file if you wish, <br>
<br>
anyway i so grateful for your contribution <br>
<br>
 mostafa

Wed, 20 Oct 2010 00:46:04 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#789048
Roger Stafford
"Mostafa ElHosseini" <melhosseini@yahoo.com> wrote in message <i9l7kb$i24$1@fred.mathworks.com>...<br>
> Dear Roger, <br>
> <br>
> thank you again for your patience. Ok. I gree with you now that quad does not fit.<br>
> <br>
> regarding incorporating the eighth equation into the solver, how can i make that especially i knew that ode45 solve differential equation not integral one.<br>
> really i have no idea about that, can you pls. help<br>
> <br>
> i can even send you the .m file if you wish, <br>
> <br>
> anyway i so grateful for your contribution <br>
> <br>
>  mostafa<br>
        <br>
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:<br>
<br>
dx1/dt = f1(x1,x2,x3,x4,x5,x6,x7)<br>
dx2/dt = f2(x1,x2,x3,x4,x5,x6,x7)<br>
.......<br>
dx7/dt = f7(x1,x2,x3,x4,x5,x6,x7)<br>
<br>
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.<br>
<br>
My proposal is that you extend this set of differential equations by adding one more equation which would be:<br>
<br>
dI/dt = y = 5.8*(q*x(:,1)6)3.7*u14.1*u25*u3^2+... etc.<br>
<br>
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.)<br>
<br>
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<br>
<br>
y = int(sin(t),'t',0,x); %< integral of sin(t) w.r. to t, from t = 0 to t = x<br>
<br>
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.<br>
<br>
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.<br>
<br>
Roger Stafford

Wed, 20 Oct 2010 21:48:03 +0000
Re: Integrate this function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294143#789309
Mostafa ElHosseini
Ok. I code it now. and it works as just you said. regarding the accuracy<br>
is it enough to write <br>
options = odeset('AbsTol',1e12,'RelTol',1e12,'MaxStep',0.0001);<br>
<br>
 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.<br>
<br>
now i have the same accuracy that i got before using trapz. in your opinion, making AbsTol, and RelTol as above is enough. <br>
<br>
<br>
thank you