How can I use the command "Y = integral(function, x_min, x_max)" properly?

1 view (last 30 days)
Hey there,
within some calculations I tried integrating an equation using the "integral" command.
Unfortunately this did not work out as I hoped it would!
Would be great if someone here can help me and show me what I made wrong or teach me a better more accurate way to solve the integral.
Thanks a lot and good luck for what ever you try to calculate...
________________
the code:
A = 2548.9320;
B = 3.5248;
C = -0.6366;
D = -3.4281;
E = 49.8238;
F = -120.3466;
G = 98.8658;
M = 28.9586;
t_1 = 290.;
t_2 = 545.;
fun = @(T) (B + (C-B) * (T/(A+T))^2 * ( 1 - (A/(A+T)) * ( D + E * (T/(A+T)) + F * (T/(A+T))^2 + G * (T/(A+T))^3) ))*R/T;
cp = integral(fun, t_1, t_2);

Accepted Answer

Sean de Wolski
Sean de Wolski on 17 Jul 2014
Edited: Sean de Wolski on 17 Jul 2014
First, you're missing R so you'll need to define it. (I defined it as 1 to get it to solve).
The problem is that your function is doing matrix multiplication and division and integral will call it with vectors not just scalars. You want the operations to work elementwise so you'll need to add a '.' before some of them (like /^*). Fortunately there's a convenience function called vectorize to do this for you:
vectorize '@(T) (B + (C-B) * (T/(A+T))^2 * ( 1 - (A/(A+T)) * ( D + E * (T/(A+T)) + F * (T/(A+T))^2 + G * (T/(A+T))^3) ))*R/T;'
ans =
@(T) (B + (C-B) .* (T./(A+T)).^2 .* ( 1 - (A./(A+T)) .* ( D + E .* (T./(A+T)) + F .* (T./(A+T)).^2 + G .* (T./(A+T)).^3) )).*R./T;
Now paste that in as your fun (and define R!) and it will solve:
fun = @(T) (B + (C-B) .* (T./(A+T)).^2 .* ( 1 - (A./(A+T)) .* ( D + E .* (T./(A+T)) + F .* (T./(A+T)).^2 + G .* (T./(A+T)).^3) )).*R./T;
cp = integral(fun, t_1, t_2)

More Answers (1)

Christoph
Christoph on 17 Jul 2014
Hey Sean,
thank you very much for the fast answer!
You helped me a lot!
__________________
ps: nice dogs!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!