How can I use the command "Y = integral(function, x_min, x_max)" properly?
1 view (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
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)
0 Comments
More Answers (1)
See Also
Categories
Find more on Adding custom doc in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!