-1

I want to plot the area above and below a particular value in x axis.

The problem i am facing is with discrete values. The code below for instance has an explicit X=10 so i have written it in such a way that i can find the index and calculate the values above and below that particular value but if i want to find the area under the curve above and below 4 this program will now work.

Though in the plot matlab does a spline fitting(or some sort of fitting for connecting discrete values) there is a value for y corresponding to x=4 that matlab computes i cant seem to store or access it.

%Example for Area under the curve and partial area under the curve using Trapezoidal rule of integration
clc;
close all;
clear all;
x=[0,5,10,15,20];% domain
y=[0,25,50,25,0];% Values

LP=log2(y);
plot(x,y);

full = trapz(x,y);% plot of the total area

I=find(x==10);% in our case will be the distance value up to which we want 
half = trapz(x(1:I),y(1:I));%Plot of the partial area

How can we find the area under the curve for a value of ie x = 2 or 3 or 4 or 6 or 7 or ...

Bharath S
  • 161
  • 1
  • 12
  • I must admit that I do not know what you mean in this question. What I know matlab just draws a straight line between the points in `plot`. Regarding what happens in `traps` I do not really remember, but if I recall correctly it is just the 2 point trapezoidal method. That is however possible to check since `trapz` is written in matlab. – patrik Apr 01 '15 at 16:21
  • to put it simply if i want to calculate the area under the curve from X=0 till X=4 and then from X=4 to X=20. How to do it. – Bharath S Apr 01 '15 at 16:25
  • 1
    Well, that depends on your ambition. Do you want to use linear interpolation? In general I would say that you first interpolate and then integrate though. – patrik Apr 01 '15 at 16:27
  • The reason that I ask is that there is no "how do I do it" for this question. The question is rather "How can I calculate this accurately enough?". Since the data you have is really inaccurate, the accuracy requirement is of vital importance here. The values between your samples is unknown and what I ask is, "How I am I supposed to do to get an accurate estimation?" Only one person can know this and it is you. For me it is only samples with far too low density for anyone to do a proper estimation. – patrik Apr 01 '15 at 16:41
  • Thank you. Now i understand what you mean. The original data set i have is very low on sampling density too. interpolation and then integration is good enough for my situation thank you for the suggestion. – Bharath S Apr 01 '15 at 19:31
  • Sorry for not being clear at the beginning. – Bharath S Apr 01 '15 at 19:37

2 Answers2

1

This is an elaboration of patrik's comment, "first interpolate and then integrate".

For the purpose of this answer I'll assume that the area in question is the area that can be seen in the plot, and since plot connects points by straight lines I assume that linear interpolation is adequate. Moreover, since the trapezoidal rule itself is based on linear interpolation, we only need interpolated values at the beginning and end of the interval.

Starting from the given points

x = [0, 5, 10, 15, 20];
y = [0, 25, 50, 25, 0];

and the integration interval limits, say

xa = 4;
xb = 20;

we first select the data points within the limits

ind = (x > xa) & (x < xb);
xw = x(ind);
yw = y(ind);

and then complete them by interpolation values at the edges:

ya = interp1(x, y, xa);
yb = interp1(x, y, xb);
xw = [xa, xw, xb];
yw = [ya, yw, yb];

Now we can simply apply trapezoidal integration:

area = trapz(xw, yw);
A. Donda
  • 8,381
  • 2
  • 20
  • 49
  • Still, interpolation is a fairly cheap operation. I will post a suggestion that do not care that the data is pyramid shaped. – patrik Apr 01 '15 at 20:30
  • @patrik, your solution will give the same result as mine no matter how the data are shaped. As I wrote, `trapz` does the linear interpolation implicitly. You could only get another result if you used some other kind of interpolation. – A. Donda Apr 02 '15 at 01:08
0

I think that you either need more samples, or to interpolate the data. Another alternative is to use a function handle. Then you need to know the function though. Example using linear interpolation follows.

x0 = [0;5;10;15;20];
y0 = [0,25,50,25,0];
x1 = 0:20;
y1 = interp1(x0,y0,x1,'linear');
xMax = 4;
partInt = trapz(x1(x1<=xMax),y1(x1<=xMax));

Some other kind of interpolation may be suitable, but that is hard to say without more information. Also, this interpolates from the beginning to x. However, I guess figuring out how to change the limits should be easy from here. This solution is different than the former, since it is less depending on the pyramid shape of the data. So to say, it is more general.

patrik
  • 4,506
  • 6
  • 24
  • 48