MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# code for computing area by varying the value of y in matrix?

Asked by S on 14 May 2013
```For computation of area the value of y1,y2 and y3 are coming from three different same dimension matrix and so I want to compute area of using this code for same matrix dimension as y1, y2 and y3?
```
```    x=[7.8 8.25 8.55];
y=[0.96 0.99 0.94];
p=polyfit(x,y,2);
t=linspace(x(1),x(3));
yhat=polyval(p,t);
a1=trapz(t,yhat);```

How can I improve code for all 3*3 matrix of given y1,y2 and y3, result will be area a1 of 3*3 matrix.

## Products

No products are associated with this question.

## 1 Answer

Answer by Roger Stafford on 14 May 2013

This general formula in terms of x1, x2, x3, y1, y2, and y3 gives the exact area under your 'polyfit' curve without the trapezoidal approximation errors introduced by 'trapz'. That is, it is the exact integral from x = x1 to x = x3 of the unique quadratic function which passes through all three points (x1,y1), (x2,y2), and (x3,y3).

` a = (x3-x1)*((y1+y2+y3)/3+((x3-x2)*(y2-y1)/(x2-x1)-(x2-x1)*(y3-y2)/(x3-x2))/6);`

## 4 Comments

S on 16 May 2013

and I also want to know how can I read y1, y2 and y3 from three different files keeping x1,x2 and x3 as

```x=[7.8 8.25 8.55];
```
S on 18 May 2013

The value of x1, x2, x3 are coming from different matrices how can I read?

S on 26 May 2013

How can I read value of several x point from different matrix which are from different matrix?

Contact us