# Specific integral of a vector

2 views (last 30 days)
Commented: graadiggrei on 26 Mar 2015
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.

Christiaan on 25 Mar 2015
Edited: Christiaan on 25 Mar 2015
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
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!

### Categories

Find more on Numerical Integration and Differentiation 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!