MATLAB Answers

## How to calculate the area of the green shaded region in MATLAB

Asked by Farhan

### Farhan (view profile)

on 30 Jan 2015
Latest activity Edited by Niels

### Niels (view profile)

on 2 Feb 2015
Hello,
Could you please guide me how to calculate the area of the green shaded region (between blue and red curve) of the attached figure in MATLAB. Please see the attached pic named "how to calculate area". Blue line has the value of 1 p.u and is a straight horizontal line.
Thanks

#### 0 Comments

Sign in to comment.

## 3 Answers ### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 30 Jan 2015

Exactly what form is your data in? An image file? An equation? A list of (x,y) coordinates in a MATLAB variable? Do you have those 3 (x,y) pairs you show in the annotation? Exactly what are you starting with? If you have the three points, can't you just use the formula for a triangle: area = (1/2)*base*height???

#### 4 Comments

Show 1 older comment
Image Analyst

### Image Analyst (view profile)

on 30 Jan 2015
Where did you post the "more zoomed in view"? Can't you just use quad()? Or even polyarea()???
Farhan

### Farhan (view profile)

on 30 Jan 2015
Please find the new picture with zoomed in view.
Farhan

### Farhan (view profile)

on 2 Feb 2015
I have attached the zoomed in view in my previous message. File name "How to calculate area_zoomed"
I have tried using quad, for that it needs a function e.g. q = quad(fun,a,b)
i have a plot in the form of array in MATLAB workspace, but how to convert this array into a function?? So that it can be used to calculate area using quad?
Also please find the attached code and data file that is needed for code.

Sign in to comment.

Answer by Farhan

### Farhan (view profile)

on 2 Feb 2015

I have attached the zoomed in view in my previous message. File name "How to calculate area_zoomed"
I have tried using quad, for that it needs a function e.g. q = quad(fun,a,b)
i have a plot in the form of array in MATLAB workspace, but how to convert this array into a function?? So that it can be used to calculate area using quad?

#### 0 Comments

Sign in to comment.

Answer by Niels

on 2 Feb 2015
Edited by Niels

### Niels (view profile)

on 2 Feb 2015

Is it not possible to simply calculate the difference between your y-values and the straight line, take the mean of every 2 neigboring y-values, then multiply those values by your x-spacing and then sum all the values between x = 2166 and x = 5590? ie.
spc = 1; % spacing of x-values
x = 1:spc:20; % full x-range
x_min = 5; % lower boundary of interest
x_max = 12; % upper boundary of interest
y = 30-0.5*x + rand(1,numel(x)); % y-values for full x-range
y_ref = 30; % reference value for y
y_means = mean([y(1:end-1); y(2:end)],1) - y_ref;
y_range = y_means(x(x==x_min):x(x==x_max-1));
y_int = sum(y_range*spc);
Or more simple:
y_int = trapz(x_min:x_max,y(x_min:x_max));

#### 0 Comments

Sign in to comment.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8865838,"math_prob":0.8016573,"size":364,"snap":"2019-35-2019-39","text_gpt3_token_len":88,"char_repetition_ratio":0.108333334,"word_repetition_ratio":0.0,"special_character_ratio":0.23901099,"punctuation_ratio":0.13095239,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99212456,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,9,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-23T06:51:38Z\",\"WARC-Record-ID\":\"<urn:uuid:b701accc-a6c6-492c-91e0-b67f70af9a5b>\",\"Content-Length\":\"124021\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e7228d4c-2b87-4a99-a2a8-54399d4192d0>\",\"WARC-Concurrent-To\":\"<urn:uuid:77584161-462a-43b2-9699-cd29ef77c248>\",\"WARC-IP-Address\":\"184.25.188.167\",\"WARC-Target-URI\":\"https://it.mathworks.com/matlabcentral/answers/173488-how-to-calculate-the-area-of-the-green-shaded-region-in-matlab\",\"WARC-Payload-Digest\":\"sha1:RYLEE5OGAOCIRCRFR72KREK3BRUTCKOV\",\"WARC-Block-Digest\":\"sha1:LOTUGLMPBEZZP6JLTWEQIXY4PSOCB4MW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027318011.89_warc_CC-MAIN-20190823062005-20190823084005-00238.warc.gz\"}"}