integral - Matlab partial area under the curve -
i want plot area above , below particular value in x axis.
the problem facing discrete values. code below instance has explicit x=10 have written in such way can find index , calculate values above , below particular value if want find area under curve above , below 4 program work.
though in plot matlab spline fitting(or sort of fitting connecting discrete values) there value y corresponding x=4 matlab computes cant seem store or access it.
%example area under curve , partial area under 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 total area i=find(x==10);% in our case distance value want half = trapz(x(1:i),y(1:i));%plot of partial area
how can find area under curve value of ie x = 2 or 3 or 4 or 6 or 7 or ...
this elaboration of patrik's comment, "first interpolate , integrate".
for purpose of answer i'll assume area in question area can seen in plot, , since plot
connects points straight lines assume linear interpolation adequate. moreover, since trapezoidal rule based on linear interpolation, need interpolated values @ beginning , end of interval.
starting given points
x = [0, 5, 10, 15, 20]; y = [0, 25, 50, 25, 0];
and integration interval limits, say
xa = 4; xb = 20;
we first select data points within limits
ind = (x > xa) & (x < xb); xw = x(ind); yw = y(ind);
and complete them interpolation values @ edges:
ya = interp1(x, y, xa); yb = interp1(x, y, xb); xw = [xa, xw, xb]; yw = [ya, yw, yb];
now can apply trapezoidal integration:
area = trapz(xw, yw);
Comments
Post a Comment