Question: In this exercise, you will be writing a code that can approximate the area between two vectors that define the cross-section of the trough. Input

In this exercise, you will be writing a code that can approximate the area between two vectors that define the cross-section of the trough.

Input variables:

x a vector representing the x co-ordinates for the outline of the trough.
y a vector representing the y co-ordinates for the bottom of the trough.
y0 a vector representing the y co-ordinates for the top of the trough.

Output variables:

Area The area between y0 and y approximated with the trapezoid rule.

Solution Process

The code should approximate the area by applying the trapezoid rule. You should assume that y0 is always above y in your solution code, and that y0 is a constant.

The trapezoid rule works by splitting an area into thin trapezoids. The area of each of these trapezoids is StepSize*(f(x+StepSize) + f(x))/2, where StepSize is the spacing in the x vector (functional notation, not MATLAB's vector notation). Your code should be a for loop that evaluates the sum of these trapezoids under f, where f is y0-y.

The in-built functions trapz and sum cannot be submitted for this activity, although you may use them to check your answer when testing your code in MATLAB.

Notes:

Recall that vector indexes must be whole numbers.

You might want to consider setting an initial value for Area before the for loop begins.

You can use trapz(x,y0-y) to test whether your for loop is correct. This trapz function will not work if you try to submit it for this question.

Potentially Useful Functions: for, length, end

Function Template:

function Area = trough_area(x,y,y0) %insert answer here end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!