Calculate integral with an external formula

1 回表示 (過去 30 日間)
Orongo
Orongo 2017 年 10 月 11 日
コメント済み: Orongo 2017 年 10 月 15 日
I calculating an integral over a function that is defined in seperate m-file. My program looks like:
% main.m
mu_x=@(t) lx(t);
l_x = exp(-integral(@(t) mu_x(t),0,98));
% lx.m
function res=lx(x)
a=0.006782872;
b=5.44781E-08;
c=0.137849468;
if x>97
res = a+b*exp(c*97)+(x-97)*0.001;
else
res=a+b*exp(c*x);
end
I expect lx(x)=lx(98) => res=a+b*exp(c*97)+(x-97)*0.001; to be calculated but instead res=a+b*exp(c*x) is done. I want to keep this if-function in lx.m, is there anyway I can do this? Also, what is going wrong??

採用された回答

David Goodmanson
David Goodmanson 2017 年 10 月 11 日
編集済み: David Goodmanson 2017 年 10 月 11 日
Hi Lenovo,
Your 'if' check is not working correctly, because the res function takes vector input for x. One way to do the task is to create an index that shows whether or not x > 97:
x = 0:.01:120;
plot(x,lx(x)) % demo
function res=lx(x)
a=0.006782872;
b=5.44781E-08;
c=0.137849468;
res = zeros(size(x));
ind = x>97;
res(ind) = a+b*exp(c*97)+(x(ind)-97)*0.001;
res(~ind) =a+b*exp(c*x(~ind));
end
  3 件のコメント
David Goodmanson
David Goodmanson 2017 年 10 月 12 日
I just plugged your expression
0.0067828+5.447812E-08*EXP(0.137849*97)+(98-97)*0.001
verbatim into Matlab, changed EXP to exp and got 0.0427, just like the demo plot in the posted answer gives and Excel gives. So I do not know where the .385 might be coming from.
Orongo
Orongo 2017 年 10 月 15 日
Ok. Thanks. I still getting the problem and will close this discussion and open a new one because I think the problem is rippling.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by