matlab question In this exercise, you will be writing a code that can approximat
ID: 3885873 • Letter: M
Question
matlab 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 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 variable: 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. Potentially Useful Functions: for, length, end Function Template: function Area = trough_area(x, y, y theta) %insert answer here end function Area = trough_area (x, y, y theta) %insert answer here endExplanation / Answer
function Area = trough_area(x, y, y0)
Area = 0;pi
for i = 1:(numel(x) - 1)
trapArea = ((y0(i) - y(i)) + (y0(i+1) - y(i+1)))/2 * abs(x(i) - x(i+1));
Area = Area + trapArea;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.