Answered You can hire a professional tutor to get the answer.

QUESTION

MAT 262 - Estimating the Area Under a Curve with MatLab Part 1 The first new idea here is how to use subscripts within MatLab.

MAT 262 - Estimating the Area Under a Curve with MatLab

Part 1

The first new idea here is how to use subscripts within MatLab. Outside of the MatLab environment, a row vector x with n components would look like

x = [ x1x2 ... xn -1xn ]

where each entry in the vector would be a number. In MatLab, however, we write x(1), x(2), ..., x(n - 1), x(n) to represent subscript notation, which allows us to manipulate entries individually instead of the vector as a whole.

We may know what this vector looks like entry by entry, but it may not be very practical to do monotonous calculations with each entry within the script with respect to some function  over and over again. Instead, we can compute what the function would look like for each component by setting up a pattern for MatLab to follow inside of what is called a for loop. A for loop is a way to essentially tell MatLab to do the same operation for a predetermined amount of calculations to a set of values until all of the calculations are done, and then MatLab stops. The example below demonstrates both of these ideas together. I have left notes as comments.

% the function in this code is the standard Normal curve from x = -5 to x = 5; we are going to discretize the horizontal axis, then evaluate the value of each value on the standard Normal curve

xmin = -5; % this is the smallest value in our domain; it is suppressed so it won't output to the CW

h = 1; this is our step size for this example; h or  are common variables to use for step size

xmax = 5; % this is the largest value in our domain, also suppressed

n = (xmax - xmin)/h; this generates how many entries there are in our vector, which is 10

x(1) = xmin; % our first entry is -5, as mentioned earlier, so this is x1, but written so MatLab understands

y(1) = (1/(sqrt(2*pi)))*exp(-((xmin).^2)/2); % the value of  written into MatLab

for i=1:n % don't suppress the condition for the for loop

           x(i+1) = x(i) + h; % this is the pattern for going from one x entry to the next; suppress it

           y(i+1) = (1/(sqrt(2*pi)))*exp(-((x(i+1)).^2)/2); % same deal as the line above

end % once MatLab goes through all of the entries, you have to tell it to stop operating in the for loop

One of the reasons we are working with subscripts and for loops is to enable us to estimate areas under curves, which is the purpose of this second MatLab project. As such, it would be nice to visualize how these values in the for loop are advantageous to estimating the area under a given curve. One way to do this is to create the histogram that is associated with the left-hand and right-hand estimates. As seen in class, depending upon which estimate we use, we could be using an overestimate or an underestimate.

Even though there are built-in histogram functions in MatLab, none of them work quite as easily as the line command. The line command allows the user to dictate exactly where the line starts and ends. In particular, it creates a line between two points in the xy-plane using the pattern below.

line([starting x coordinate; ending x coordinate],[starting y coordinate, ending y coordinate])

This lends itself to creating either vertical, horizontal, or diagonal lines. We are only really concerned with creating the former two, since we are mimicking a histogram. These can be manipulated inside of a for loop to create a collection of lines as needed (hint for the next part).

Two final commands that need to be mentioned are the sum command and the trapz command.

The sum command is exactly what it sounds like it is: a cumulative sum of a collection of quantities, which could be a collection of subscripted quantities (hint for the next part). If, for example, you wanted to add up all of the areas of the individual bars of a histogram, a single equation in a for loop could collect all of the areas and a short, unsuppressed command after the end of the for loop of the form sum( ) could generate that sum.

Lastly, the trapz command is a method to finding the "exact" area under a given curve. It is displayed and executed in the same format as the plot command: trapz(x vector, y equation). Leave it unsuppressed so the output is displayed in the CW.

Part 2 - Estimating the Area Under a Curve (30 pts)

Consider a simple LRC series electrical circuit connected to a 9V battery. This circuit has an inductance of 0.2 H (henries), a resistance of 5  (ohms), and capacitance 0.0 F (farads). If this circuit stays closed indefinitely (so current runs through the circuit all the time), starting at time t = 0 seconds, with an initial charge of 0 C (coulombs), then the function that gives the current for all nonnegative times t is

i(t) = .

2A) (4 pts) Plot the current function above from t = 0 seconds to t = 1 seconds with an adequately small step size. Label each of the axes as to what they represent and title the graph. Make it look nice.

Now, this function above, i(t), gives us the current throughout the circuit, but it does not explicitly tell us much about the charge in the circuit. The relationship between distance, velocity, and time is very similar to the relationship between charge, current, and time. Using this knowledge, we are going to utilize subscripts and for loops to estimate the area under the curve generated by the current function, i(t) in a similar fashion as the example provided earlier.

2B) (10 pts) Use a for loop to calculate the left-hand estimate for the area under the curve. Use a step size of either 0.1 or 0.2 in your code. Create vertical and horizontal lines mimicking what the left-hand estimate would look when drawn over the top of the original function (like examples from class). Create these lines within the for loop. Lastly, calculate the area of each column for the estimate, and sum the results outside of the for loop using a sum command.

Hint: Set up the first subscripted entry before the for loop like in the example above. It may be useful to draw a picture by hand first to help with how line commands should work.

2C) (10 pts) Do the same thing as in 2B, but for the right-hand estimate for the area under the curve.

Hint: Once you get 2B, you only need a few minor modifications to knock this one out.

2D) (6 pts) Compare the estimated results from parts 2B and 2C. Are they close? Do they make sense based on the original plot? Now, compute the exact area under the curve with the trapz command with the original vector and function. Are your estimates close to the actual area under the curve? Finally, what does the area under the curve represent for this problem? Provide a careful, thoughtful answer within the context of the problem.

MY ANSWER SO FAR:

% Part 2A

%sets the range

%this is the equation

%stops from listing the variables

%creates the graph

%Part 2B

%defines the for loop

%coordinates into the loop

%this part is supposed to split the graph into sections

%finds left sum

%Part 2C

%Part 2D

%finds the exact area under a curve

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question