How to integrate a plot over specified range?

56 ビュー (過去 30 日間)
behzad
behzad 2019 年 9 月 22 日
コメント済み: Rik 2019 年 9 月 22 日
Hi everyone.
I want to integrate a plot over specified range. i have two seperate arrays i.e. x(t) and y(x) which i already have in a plot. now i want to integrate y(x) over x(t) from x(t1) to x(t2), the area under y(x) over this specified range.
To get a better view, let me elaborate with an example:
t = 0:0.1:2*pi;
x = sin(t);
y = cos(x);
Now the question is how to calculate ∫y.dx from x(t=1) to x(t=2).
best regards

採用された回答

MS
MS 2019 年 9 月 22 日
編集済み: MS 2019 年 9 月 22 日
You can use the integral function to numerically integrate a function from xmin to xmax. Example:
% define functions
fun_x = @(t) sin(t);
fun_y = @(x) cos(x);
% assign the time interval
t1 = 1;
t2 = 2;
% evaluate function x
xmin = feval(fun_x,t1);
xmax = feval(fun_x,t2);
% integrate
q = integral(fun_y,xmin,xmax);
disp (q);
>>> Output:
0.0434
Hope this helps.
  4 件のコメント
behzad
behzad 2019 年 9 月 22 日
I get the idea. It is nice and clean, very basic too. I think this solves the problem.
Very helpfull, thanks MS.
Rik
Rik 2019 年 9 月 22 日
It should be possible to replace your last loop with trapz.

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

その他の回答 (1 件)

Rik
Rik 2019 年 9 月 22 日
編集済み: Rik 2019 年 9 月 22 日
As you indicated with the tags, using trapz is also an option.
fun_x_of_t=@(t) sin(t);
fun_y_of_x=@(x) cos(x);
fun_y_of_t=@(t) fun_y_of_x(fun_x_of_t(t));
t=linspace(1,2,100);
x=fun_x_of_t(t);
y=fun_y_of_t(t);
trapz(x,y)
  2 件のコメント
behzad
behzad 2019 年 9 月 22 日
Thank you Rik.
I know i stated in second part of my question the functions are available as sin and cos. But what we can do in a situation that we have just data points in a plot and they are not known as common functions?
I appreciate you considering the question.
Rik
Rik 2019 年 9 月 22 日
As long as you have the xy paired data, you can use trapz. If you don't have x (or it is impossible to derive x from t) what you want is imposible.

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

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by