필터 지우기
필터 지우기

Specific integral of a vector

조회 수: 1 (최근 30일)
graadiggrei
graadiggrei 2015년 3월 25일
댓글: graadiggrei 2015년 3월 26일
I have a graph that is plotted from two vectors, val_x and x. it is plotted below as plot(x,val_x). I want to integrate this plot from a to b and get the shaded area under the graph, even when a and b are different from the values in x (example: x is increasing from 0 to 1 with steps of 0.1 and b=0.15 a=0.25). I have tried to represent the vector as a function through polynomial regression, but the function could not catch the accurate shape of the plot.

답변 (1개)

Christiaan
Christiaan 2015년 3월 25일
편집: Christiaan 2015년 3월 25일
Dear Ser/Madame,
To create more points in your function, you could use an interpolation function like pchip or spline to create more points. A example how it works is:
clc; clear all; close all;
a = 0.5;
b = 0.7;
%%PART 1: not enough points
h1 = subplot(2,1,1);
x = linspace(0,1,20);
y = 1/2*sin(x*pi).*exp(x*pi);
xpoints=x(find(x>=a & x<=b));
ypoints=y(find(x>=a & x<=b));
plot(h1,x,y);axes(h1); hold on;
area(xpoints,ypoints); hold on;
line([a a],get(h1,'YLim'),'Color',[1 0 0]);
line([b b],get(h1,'YLim'),'Color',[1 0 1]);
title(h1,'title 1');
%%PART 2: more points
h2 = subplot(2,1,2);
xnew = linspace(min(x),max(x),length(x)*50);
ynew = pchip(x,y,xnew);
xpoints_new=xnew(find(xnew>=a & xnew<=b));
ypoints_new=ynew(find(xnew>=a & xnew<=b));
plot(h2,xnew,ynew);axes(h2); hold on;
area(xpoints_new,ypoints_new); hold on;
line([a a],get(h1,'YLim'),'Color',[1 0 0]);
line([b b],get(h1,'YLim'),'Color',[1 0 1]);
title(h2,'title 2');
If you like you can increase the number 50 to any number you like.
Good luck! Christiaan
  댓글 수: 1
graadiggrei
graadiggrei 2015년 3월 26일
Thank you very much! I chose the xnew values to go from "a" to "b" with a certain step, and then integrating it normally with trapz to get the answer I was looking for.
Somehow Matlab central has disconnected the question from my user, so I am unable to accept this as the answer to the question, but it solved the problem!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by