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

2 次查看(过去 30 天)
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);

采纳的回答

Sean de Wolski
Sean de Wolski 2014-7-17
编辑:Sean de Wolski 2014-7-17
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)

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Adding custom doc 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by